Android: Possibility of vertical alignment for SeekBar
This should be achieved with xml
drawable
for SeekBar
:
I have tried many different variations and this is the best I could do.
This seekbar_thumb_drawable.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="@color/accent_color"/>
<size
android:height="20dp"
android:width="20dp" />
</shape>
This seekbar_drawable.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:height="5dp"
android:top="7.5dp">
<shape
android:shape="rectangle">
<solid android:color="@color/seekbar_background" />
<corners android:radius="2.5dp" />
</shape>
</item>
<item
android:id="@android:id/progress"
android:height="10dp"
android:top="5dp">
<clip>
<shape
android:shape="rectangle">
<solid android:color="@color/seekbar_progress" />
<corners android:radius="5dp" />
</shape>
</clip>
</item>
</layer-list>
API Result 18: The problem is that the lines are not vertically centered.
API Result 24: This shows how I want it to be displayed.
Is there a possible solution to make this appear as in the first image on all devices with API 16
?
I don't want to use nine patches because I need to use color from resources. I tried to padding
, gravity
, top
, bottom
, inter-shape
, line
, rectangle
, but I could not find a solution ...
source to share
This is due to the fact that android:height
and android:width
for <item>
are only available for API 23 and above. If you use these attributes for APIs below 23 it won't give you an error, but will just ignore it.
In your case, android:height="5dp"
they android:height="10dp"
do not apply.
If you look at the relevant docs or, it says they were added in API level 23.
source to share
You are probably facing an already known issue, here is the solution for it. You must define maxHeight
and minHeight
for SeekBar
as well as height. Or you can even set maxHeight
to some big value, for example 1000dp
if you increase the height to wrap_content
as mentioned below, the suggested answer in the same thread.
<SeekBar
android:thumb="@drawable/seekbar_thumb_drawable"
android:progressDrawable="@drawable/seekbar_drawable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxHeight="1000dp"
<!-- OR -->
android:maxHeight="16dp"
android:minHeight="16dp"/>
source to share
Please update your available files.
In the seekbar_thumb_drawable.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:gravity="center">
<shape android:shape="oval">
<solid android:color="@color/accent_color" />
<size
android:width="20dp"
android:height="20dp" />
</shape>
</item>
</layer-list>
seekbar_drawable.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:height="5dp"
android:gravity="center">
<shape android:shape="rectangle">
<solid android:color="@color/seekbar_background" />
<corners android:radius="2.5dp" />
</shape>
</item>
<item
android:id="@android:id/progress"
android:height="10dp"
android:gravity="center">
<clip>
<shape android:shape="rectangle">
<solid android:color="@color/seekbar_progress" />
<corners android:radius="5dp" />
</shape>
</clip>
</item>
</layer-list>
I am using gravity on an element to draw the element in the center.
source to share
I am also facing the same problem in the Low API thumb drawable goes out of line So what I did for it.
<SeekBar
android:id="@+id/seekbar_transparency"
android:layout_width="match_parent"
android:layout_height="40dp"
android:paddingTop="@dimen/seekbar_thumb_size_space"
android:paddingBottom="@dimen/seekbar_thumb_size_space"
android:progress="100"
android:theme="@style/SeekbarTheme"
android:thumb="@drawable/thumb_drawable" />
style.xml
<!--Seekbar background change-->
<style name="SeekbarTheme" parent="Theme.AppCompat.Light">
<!-- active seekbar progress track color -->
<item name="colorControlActivated">@color/colorSeekBar</item>
<!-- inactive seekbar progress track color -->
<item name="colorControlNormal">@color/colorGray</item>
</style>
thumb_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="2.5dp"
android:left="2.5dp"
android:right="2.5dp"
android:top="2.5dp">
<shape android:shape="rectangle">
<size
android:width="@dimen/seekbar_thumb_size"
android:height="@dimen/seekbar_thumb_size" />
<corners android:radius="20dp" />
<solid android:color="#201DE9B6" />
</shape>
</item>
<item
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp">
<shape android:shape="rectangle">
<size
android:width="@dimen/seekbar_thumb_size"
android:height="@dimen/seekbar_thumb_size" />
<corners android:radius="20dp" />
<solid android:color="#AA1DE9B6" />
</shape>
</item>
</layer-list>
dimes.xml
<!--Seeckbar thumb size-->
<dimen name="seekbar_thumb_size">10dp</dimen>
<dimen name="seekbar_thumb_size_space">5dp</dimen>
source to share
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxHeight="3dp"
android:minHeight="3dp"
android:progressDrawable="@drawable/nnl_bg_seek_bar_progress"
android:splitTrack="false"
android:thumb="@drawable/nnl_sel_seek_bar_thumb"
tools:ignore="UnusedAttribute"
/>
The code above is my way. You may need to pay attention to these attributes: maxHeight / minHeight / splitTrack.
source to share