Android: reference style parent

2.7k views Asked by At

I want to write a custom style for a horizontal ProgressBar.

If you want to add a horizontal ProgressBar, it needs to reference the system style like this:

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

I wrote a custom style for it that adds some parents, but since there is already a style reference that gives it the horizontal shape, I need to inherit from this system style.

I tried it like this:

<style name="itemViewProgressBar" parent="@android:attr/progressBarStyleHorizontal">
    <item name="android:paddingLeft">@dimen/ivProgressBarPaddingLeft</item>
    <item name="android:paddingRight">@dimen/ivProgressBarPaddingRight</item>
    <item name="android:paddingTop">@dimen/ivProgressBarPaddingTop</item>
    <item name="android:paddingBottom">@dimen/ivProgressBarPaddingBottom</item>
</style>

And in the ProgressBar I referenced it like this:

<ProgressBar
    android:id="@+id/progressBar1"
    style="@style/itemViewProgressBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

The style xml doesn't give me any errors, so the reference seems to work fine. But still it doesn't have any impact on the ProgressBar. It looks the same like I left the parent attribute out (i.e. it shows the circular ProgressBar).

I can of course define all paddings in the ProgressBar element itself and reference the system style in the ProgressBar like default. But the more elegant way should be to use a custom style, especially since I need to reuse it quite often.

Any idea on how I can achieve to have both the system horizontal ProgressBar style and my custom paddings in a custom style?

2

There are 2 answers

0
Terry On BEST ANSWER

Put this as parent:

<style name="itemViewProgressBar" parent="@android:style/Widget.Holo.Light.ProgressBar.Horizontal">
    <item name="android:paddingLeft">@dimen/ivProgressBarPaddingLeft</item>
    <item name="android:paddingRight">@dimen/ivProgressBarPaddingRight</item>
    <item name="android:paddingTop">@dimen/ivProgressBarPaddingTop</item>
    <item name="android:paddingBottom">@dimen/ivProgressBarPaddingBottom</item>
</style>

It's the Holo style horizontal ProgressBar.

1
Jagadesh Seeram On

Try this..

<style name="itemViewProgressBar" parent="@android:style/Widget.ProgressBar.Horizontal">
    <item name="android:paddingLeft">@dimen/ivProgressBarPaddingLeft</item>
    <item name="android:paddingRight">@dimen/ivProgressBarPaddingRight</item>
    <item name="android:paddingTop">@dimen/ivProgressBarPaddingTop</item>
    <item name="android:paddingBottom">@dimen/ivProgressBarPaddingBottom</item>
</style>