I have SeekBar with custom drawable. Progress element is not rendered from left side exactly but it is moved to right. How can I avoid this?
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@android:id/summary"
android:background="@null"
android:progressDrawable="@drawable/seekbar"
android:thumb="@drawable/seekbar_thumb"/>
seekbar.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@android:id/background"
android:drawable="@drawable/seekbar_background"/>
<item android:id="@android:id/progress">
<clip android:drawable="@drawable/seekbar_progress" />
</item>
</layer-list>
seekbar_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">
<stroke android:width="2dp" android:color="@color/colorNeutral" />
</shape>
seekbar_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">
<stroke
android:width="4dp"
android:color="@color/colorAccentDark" />
</shape>
EDIT:
As mentioned in comments if there will be both widths same problem disappear. But I need make it with two different widths through drawable is it possible? Maybe with different shapes not just lines?
I can not explain why this is an issue when drawing strokes, I think it has something to do with the strokes width, but I did not yet find the source to verify.
To remove the issue at hand, you can just set an inset
on your left side. A value of 1dp
or 2dp
both work and the seekbar will be drawn properly.
Note: Using this approach there should be no risk that the background could be too short and not visible with low progress values, since the thumb would overlay and hide it in any case.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@android:id/background"
android:drawable="@drawable/seekbar_background"
android:left="2dp"><!-- or 1dp -->
</item>
<item android:id="@android:id/progress">
<clip android:drawable="@drawable/seekbar_progress"/>
</item>
</layer-list>
Observation
When you create a line using shape drawable it leaves the left and right padding (from the view in which this drawable is being used.)equal to the half of its stroke width.
Workaround
You need to wrap your progress_background with an inset drawable and then set it as progress background. The inset can be like the following-
inset_seekbar_background.xml
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/seekbar_progress"
android:insetLeft="2dp"
android:insetRight="2dp" />
and your final seekbar.xml will be like-
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@android:id/background"
android:drawable="@drawable/inset_seekbar_background"/>
<item android:id="@android:id/progress">
<clip android:drawable="@drawable/seekbar_progress" />
</item>
</layer-list>
Alternates
1.You can use an 9 path image for line as it is done in the default material design-
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@android:id/background"
android:drawable="@drawable/abc_scrubber_primary_mtrl_alpha" />
<item android:id="@android:id/secondaryProgress">
<scale android:scaleWidth="100%">
<selector>
<item android:state_enabled="false">
<color android:color="@android:color/transparent" />
</item>
<item android:drawable="@drawable/abc_scrubber_primary_mtrl_alpha" />
</selector>
</scale>
</item>
<item android:id="@android:id/progress">
<scale android:scaleWidth="100%">
<selector>
<item android:state_enabled="true">
<color android:color="@android:color/transparent" />
</item>
<item android:drawable="@drawable/abc_scrubber_primary_mtrl_alpha" />
</selector>
</scale>
</item>
</layer-list><!-- From: file:/usr/local/google/buildbot/src/googleplex-android/mnc-supportlib-release/frameworks/support/v7/appcompat/res/drawable/abc_seekbar_track_material.xml -->
2.You can set the theme to your view with different color in android style as-
<style name="AppTheme.SeekBar">
<item name="colorPrimary">@color/colorAccent</item>
<item name="colorPrimaryDark">@color/colorPrimary</item>
<item name="colorAccent">@color/colorPrimaryDark</item>
</style>
Here I have used all there options for Seekbars-
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.SeekBar" />
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<SeekBar
android:layerType="software"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progressDrawable="@drawable/seekbar" />
And here is the output-
As it appear the issue in progress I think they both should be the same width
but you in progress make width = 4 android:width="4dp"
and seekbar width = 2 android:width="2dp"
just match them to be 2 or 4 dont use different values
Try using a .9.png drawable of width 4dp (converted to px) .
Keep 1 dp at top and bottom transparent.
And add colour to the center 2 dp of your choice for the background.
This way both background and progress will be of same width but the background will look thinner than progress bar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With