Rounded corners on material button - android

I'm following the tips from questions like this to create a button style like suggested on Material Design.
However, I need to change the corner radius and haven't been able to do so by inheriting Widget.AppCompat.Button.Colored style and setting the radius parameter.
How can I have the same style but with rounded corners?

With the Material Components Library:.
Add the dependency to your build.gradle:
dependencies { implementation ‘com.google.android.material:material:1.3.0’ }
In this case you can use a MaterialButton in your layout file:
<com.google.android.material.button.MaterialButton
....
style="#style/Widget.MaterialComponents.Button"
app:cornerRadius=".."
app:strokeColor="#color/colorPrimary"/>
Use app:cornerRadius attribute to change the size of corner radius. This will round off the corners with specified dimensions.
You can also customize the corners using the shapeAppearanceOverlay attribute.
<style name="MyButton" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="shapeAppearanceOverlay">#style/MyShapeAppearance</item>
</style>
<style name="MyShapeAppearance">
<item name="cornerFamily">rounded</item>
<item name="cornerFamilyTopRight">cut</item>
<item name="cornerFamilyBottomRight">cut</item>
<item name="cornerSizeTopLeft">32dp</item>
<item name="cornerSizeBottomLeft">32dp</item>
</style>
The official doc is here and all the android specs here.
With Jetpack Compose 1.0.x use the shape parameter:
Button( onClick = { /* Do something! */ },
shape = RoundedCornerShape(8.dp)) {
Text("Button")
}
Button(modifier = Modifier.padding(16.dp),
onClick = { /* Do something! */ },
shape = RoundedCornerShape(
50, // topEndPercent
0, // topEndPercent
0, // bottomEndPercent
50. // bottomStartPercent
)
) {
Text("Button")
}
OLD Support Library:
With the new Support Library 28.0.0, the Design Library now contains the Material Button.
You can add this button to our layout file with:
<android.support.design.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XXXXX"
android:textSize="18sp"
android:backgroundTint="#color/colorPrimary"
app:icon="#drawable/ic_android_white_24dp" />
You can set the corner radius with this attribute:
app:cornerRadius: Used to define the radius used for the corners of the button
dependencies {
implementation 'com.android.support:design:28.0.0'
}

Update:
Answer by Gabriele Mariotti below is now better.
Old answer:
You need to inherit that style.
Add into your styles.xml:
<style name="AppTheme.RoundedCornerMaterialButton" parent="Widget.AppCompat.Button.Colored">
<item name="android:background">#drawable/rounded_shape</item>
</style>
Add file drawable/rounded_shape.xml:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="#color/colorAccent" >
</solid>
<corners
android:radius="11dp" >
</corners>
</shape>
And finally in your layout:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Text"
style="#style/AppTheme.RoundedCornerMaterialButton"/>
Edit: updated answer to use theme's color rather than hardcoded one.

Rounded Material Button with Ripple effect
Create a file in drawable folder ripple.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimary" />
<corners android:radius="20dp" />
</shape>
</item>
<item android:drawable="#drawable/rounded_shape" />
</ripple>
Create a file in drawable folder rounded_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="#color/colorPrimary" >
</solid>
<corners
android:radius="20dp" >
</corners>
</shape>
And on your Button:
<Button
android:id="#+id/button1"
android:background="#drawable/ripple"
android:text="Login"/>

Now use MaterialButton for rounded button many more thing you can do with this. please follow link
and add app:cornerRadius="8dp"for rounded corner
and don't forget to add google material libs in build.gradle
implementation "com.google.android.material:material:1.1.0"

I will tell you my exact solution for this . Inside selector tags, you can put items (functionality of the buttons)
Second item of the selector tag has the opposite behaviour. You can add as much as selector (button behaviour)
ADD THIS DRAWABLE XML AS A BACKGROUND OF THE BUTTON android:background="#drawable/this xml"
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#ffffff"> <!-- this is the ripple color(first touch color changes into this color) -->
<item>
<selector>
<item android:state_enabled="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- default button color -->
<solid android:color="#color/colorPrimary"></solid>
<corners android:radius="151dp"></corners>
</shape>
</item>
<item> //first item was state enabled so this is automatically state disabled
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- button disabled color opposite behaviour -->
<solid android:color="#e9d204"></solid>
<corners android:radius="151dp"></corners>
</shape>
</item>
</selector>
</item>
<item>
<selector>
<item android:state_pressed="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- button first touch of your finger color -->
<solid android:color="#1989fa"></solid>
<corners android:radius="151dp"></corners>
</shape>
</item>
</selector>
</item>
<item>
<selector>
<item android:state_hovered="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- hovered with a note pencil -->
<solid android:color="#4affffff"></solid>
<corners android:radius="151dp"></corners>
</shape>
</item>
</selector>
</item>
</ripple>

Also another simple way is wrap it around cardView,Remember to set the layout_width and layout_height of the cardView to wrap_content, also all the needed margin the button will need should be applied to the cardView
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="8dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="40dp"
app:elevation="10dp">
<android.support.v7.widget.AppCompatButton
android:id="#+id/login_twitter"
android:tag="login_twitter"
android:layout_width="300dp"
android:layout_height="60dp"
android:paddingLeft="10dp"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:textColor="#color/blue_grey_ligthen_5"
android:drawableLeft="#drawable/twitter"
android:background="#color/twitter"
android:text="#string/login_with_twitter" />
</android.support.v7.widget.CardView>

If you don't want to change the theme for the whole app.You can use the material theme for this view specifically:
<com.google.android.material.button.MaterialButton
android:id="#+id/fooButon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:fontFamily="sans-serif"
android:padding="8dp"
==> android:theme="#style/Theme.MaterialComponents.Light"
app:backgroundTint="#color/base_white" />

Try below code Create a drawable file called circular_button.xml and insert the below
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#008577" />
<corners android:bottomRightRadius="100dp"
android:bottomLeftRadius="100dp"
android:topRightRadius="100dp"
android:topLeftRadius="100dp"/>
</shape>
Then change the background of the button to this drawable file
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/circle_button"
android:text="Button"/>
If you want a full circle button you can use the below drawable
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#008577"/>
<size
android:width="120dp"
android:height="120dp"/>
</shape>

Related

How to implement the round border and ?selectableItemBackground in textview android?

I am trying to have two text views inside a Linear Layout with a horizontal orientation. Now I want to have the following things for each text view:
An icon at left - implemented using android:drawableLeft="#drawable/ic_lightbulb_outline_blue_24dp"
A round border around the text view - implemented using android:background="#drawable/round_border"
Also, need that ripple effect on touching a touch item
The 3rd point is implemented in other text views using the att.
android:background="?selectableItemBackground"
android:clickable="true"
But there is already a background att. for the round border. Now my question is how to achieve that ripple effect? Is using following att. only way:
android:foreground="?selectableItemBackground"
Android shows this att. is not supported in lower api versions so worried.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/vision_mission"
android:gravity="center"
android:layout_margin="8dp"
android:padding="8dp"
android:textSize="16sp"
android:onClick="openVisionMission"
android:drawableLeft="#drawable/ic_lightbulb_outline_blue_24dp"
android:background="#drawable/round_border"
android:clickable="true"
android:focusable="true"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/team"
android:textSize="16sp"
android:drawableLeft="#drawable/ic_people_blue_24dp"
android:background="#drawable/round_border"
android:clickable="true"
android:focusable="true"
android:padding="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:gravity="center"/>
</LinearLayout>
You can achieve ripple effect and text view rounded background at the same time using the following code.
Custom ripple drawable, it require API level 21+
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/grey">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<stroke
android:width="1dp"
android:color="#android:color/holo_blue_dark" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<stroke
android:width="1dp"
android:color="#android:color/holo_blue_dark" />
</shape>
</item>
</ripple>
After that set background of your text view
android:background="#drawable/custom_ripple"
For pre lollypop ie. below API level 21
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape android:shape="rectangle">
<solid android:color="#color/colorDarkGray"/>
</shape>
</item>
<item android:state_pressed="false">
<shape android:shape="rectangle">
<solid android:color="#color/colorButtonGreen"/>
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#color/colorButtonGreenFocused"/>
</shape>
</item>
</selector>
It is not exactly what you are looking for.
You can achieve easily something similar with a MaterialButton with a OutlinedButton style.
Something like:
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
app:icon="#drawable/ic_add_24px"
android:text="...."
.../>
You can customize the padding between the icon and the text, the textColor, the strokeWidth, the strokeColor and also the color selector used for the app:rippleColor
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
app:icon="#drawable/ic_add_24px"
app:strokeColor="#color/primaryDarkColor"
app:strokeWidth="2dp"
app:iconPadding="16dp"
.../>
you can change your background drawable with selector tag
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:state_hovered="true"
android:drawable="#drawable/button_focused" /> <!-- hovered -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>

android:How to make triangular layout

I want to make custom Info window for google map
I can make it but I am not able to make triangle bellow layout.
I can add image there but layout have shadow on outer line.
Anyone suggest me what to do.
how to make portion inside red area. as you can see outer layout have shadow.
You can use the Material Component Library to create custom shapes.
The library provides the TriangleEdgeTreatment which creates a triangle treatment of the given size, which faces inward or outward relative to the shape.
You can use something like:
TriangleEdgeTreatment triangleEdgeTreatment = new TriangleEdgeTreatment(radius,false);
LinearLayout linearLayout= findViewById(R.id.xxxx);
ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
.toBuilder()
.setBottomEdge(triangleEdgeTreatment)
.build();
MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
ViewCompat.setBackground(linearLayout,shapeDrawable);
Also for edge treatments the parent view must disable clipping of children by
setting android:clipChildren="false" in xml.clipToPadding may also need to be false if there is any padding on the parent that could intersect the shadow.
This can be achieved using MaterialCardView and Shape theming.
Dependency: (Use the latest stable version)
def materialVersion = '1.3.0'
implementation "com.google.android.material:material:$materialVersion"
Layout:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/backgroundWhite"
app:cardElevation="4dp"
app:cardCornerRadius="8dp">
//Content layout here
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
style="#style/PointerCardViewStyle"
android:layout_width="20dp"
android:layout_height="10dp"
app:cardBackgroundColor="#color/whiteColor"
android:layout_gravity="center"
app:cardElevation="4dp" />
</LinearLayout>
Style:
<style name="PointerCardViewStyle" parent="Widget.MaterialComponents.CardView">
<item name="shapeAppearanceOverlay">#style/ShapeAppearanceOverlayPointerCardView</item>
</style>
<style name="ShapeAppearanceOverlayPointerCardView" parent="#style/Widget.MaterialComponents.CardView">
<item name="cornerFamily">cut</item>
<item name="cornerSizeTopRight">0dp</item>
<item name="cornerSizeTopLeft">0dp</item>
<item name="cornerSizeBottomRight">10dp</item>
<item name="cornerSizeBottomLeft">10dp</item>
</style>
Result:
You can create this custom shape using <layer-list>. Below is a working example. Put custom_triangular_shape.xml into your res/drawable folder.
custom_triangular_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Transparent Rectangle -->
<item>
<shape android:shape="rectangle">
<size
android:width="300dp"
android:height="80dp" />
<solid android:color="#android:color/transparent" />
</shape>
</item>
<!-- Colored Rectangle -->
<item
android:bottom="20dp">
<shape android:shape="rectangle">
<corners
android:radius="4dp">
</corners>
<size
android:width="300dp"
android:height="80dp" />
<solid android:color="#FFFFFF" />
</shape>
</item>
<!-- Bottom Triangle -->
<item
android:left="90dp"
android:right="110dp"
android:top="0dp"
android:bottom="30dp">
<rotate android:fromDegrees="45">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
</shape>
</rotate>
</item>
</layer-list>
USE:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/custom_triangular_shape">
</LinearLayout>
OUTPUT:
Hope this will help~
Try this:
For triangle :
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="45"
android:pivotX="-40%"
android:pivotY="87%"
android:toDegrees="45">
<shape android:shape="rectangle">
<stroke
android:width="10dp"
android:color="#android:color/transparent" />
<solid android:color="#000000" />
</shape>
</rotate>
</item>
</layer-list>
For rectangle:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#B2E3FA" />
</shape>
</item>
</layer-list>
Use both xml in the Layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/rlv1"
android:layout_width="150dp"
android:layout_height="50dp"
android:background="#drawable/rectringle" />
<RelativeLayout
android:id="#+id/rlv2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="#+id/rlv1"
android:layout_marginLeft="30dp"
android:background="#drawable/tringle"
android:rotation="180" />
</LinearLayout>
You can make use of layer-list. Create a triangular shape at the bottom, then create a rectangular shape as next layer and give bottom margin that matches your triangle's height.
here is a sample to make triangle shape. https://stackoverflow.com/a/18421795/1987045
You can use a 9patch as background for your custom layout.
Using PopUpWindow you can achieve your goal. Here is a good link to start with.

appcompat-v7 custom button colors

is it possible to customize the button colors for appcompat-v7? The default buttons are gray and I would like to change them to green throughout the entire app. setting android:background to a drawable removes the curved edges.
Since you're explicity asking on how to do it with appcompat-v7 - here's the recommended way.
First of all: Creating a new drawable isn't necessary. You can simply change the color of a button by using a theme with the colorButtonNormal attribute.
Following an example:
styles.xml
<style name="ThemeButton">
<item name="colorButtonNormal">#009688</item>
</style>
layout.xml
<!-- other layout elements -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="My Button"
android:textColor="#android:color/white"
android:theme="#style/ThemeButton" />
Result:
This way you'll keep the standard elevation and ripple effect when the button is pressed which isn't the case when using a custom drawable. (at least if you're not using a selector)
For that you need to create a custom layout in your app. In your drawable folder create a xml file named as lets say, custom_button.xml.
Add the following code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<stroke
android:width="0dp"
android:color="#000000" />
<solid
android:color="ColourCode of the colour you Want to give"
/>
<corners
android:radius="0dp"
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="0dp"
android:topRightRadius="0dp"/>
</shape>
</item>
</selector>
Save the xml file and in your activity_main.xml file where you have defined the button, add custom_button as background.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/titleButton"
android:background="#drawable/custom_button"/>
It will work now
Create a new file named "shape_btn.xml" in your drawable directory like this, change the android:color to what you like:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="#dimen/abc_button_inset_horizontal_material"
android:insetTop="#dimen/abc_button_inset_vertical_material"
android:insetRight="#dimen/abc_button_inset_horizontal_material"
android:insetBottom="#dimen/abc_button_inset_vertical_material">
<shape android:shape="rectangle">
<corners android:radius="#dimen/abc_control_corner_material" />
<solid android:color="#android:color/black" />
<padding android:left="#dimen/abc_button_padding_horizontal_material"
android:top="#dimen/abc_button_padding_vertical_material"
android:right="#dimen/abc_button_padding_horizontal_material"
android:bottom="#dimen/abc_button_padding_vertical_material" />
</shape>
</inset>
and set to button background:
<Button
...
android:background="#drawable/shape_btn"
/>
If you want to apply it to all buttons in your app, change your theme like this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:buttonStyle">#style/MyButtonStyle</item>
</style>
<style name="MyButtonStyle" parent="Widget.AppCompat.Button" >
<item name="android:background">#drawable/shape_btn</item>
</style>

Switch-Button thumb gets skewed?

The thumb for my Switch-button seems to get skewed(for on&off state). There were similar problems on github, but those were for people making libraries to support Switch button in API 4.0-
main contains the switch-button, and there is a thumb and track drawable applied to it
This is what is happening:
This is how its suppose to loook:
switch_track_on.png
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Switch
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="112dp"
android:thumb="#drawable/switch_thumb_selector"
android:track="#drawable/switch_track_selector"
android:textOn=""
android:textOff=""/>
</RelativeLayout>
switch_thumb_selector.xml
<selector>
<item android:drawable="#drawable/switch_thumb">
</item>
</selector>
switch_track_selector.xml
<selector>
<item android:drawable="#drawable/switch_track_on" android:state_checked="true"/>
<item android:drawable="#drawable/switch_track_off" android:state_checked="false"/>
</selector>
For Custom Switch (Like IOS switch) I tried below Steps:
Create a drawable track_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
<solid android:color="#ca120f" />
<corners android:radius="25dp" />
<size android:width="2dp" android:height="18dp" />
</shape>
</item>
<item android:state_checked="false">
<shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
<solid android:color="#27170432" />
<corners android:radius="25dp" />
<size android:width="2dp" android:height="18dp" />
</shape>
</item>
</selector>
Create a drawable thumb_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false">
<shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
<solid android:color="#ffffff" />
<corners android:radius="100dp" />
<size android:width="24dp" android:height="25dp" />
<stroke android:width="4dp" android:color="#0000ffff" />
</shape>
</item>
</selector>
In your layout :
<Switch
android:id="#+id/checkboxAttendanceSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:thumb="#drawable/thumb_selector"
app:track="#drawable/track_selector" />
Its working fine for me.
I had the same requirement. I checked Android code and found that
switch ignores any vertical margin/padding applied to a thumb shape drawable (thus thumb always touch the top and bottom of the track)
the width of the thumb is calculated by taking the horizontal paddings + the max width of the on and off texts.
This makes really hard to make a circle thumb.
But if you specify the thumb drawable as a layer drawable for the sole reason to be able to specify padding for the single layer of the drawable, you can get the desired effect.
Thumb selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<!--
NOTE
We want a thumb with padding around it inside the track.
Sadly, a switch draws its track and thumb with the same height ignoring
any padding of the drawable, so using a shape with padding does not work.
To overcome, we apply a trick. We create layer list because the
LayerListDrawable draws itself with taking the top, left, right, bottom
values into account.
-->
<layer-list>
<item
android:top="#dimen/switch_thumb_padding"
android:left="#dimen/switch_thumb_padding"
android:right="#dimen/switch_thumb_padding"
android:bottom="#dimen/switch_thumb_padding">
<!--
NOTE
No need to specify size because:
- The thumb fills the track in height.
- The thumb width is determined from thumb max(on, off) text +
text padding + drawable padding.
-->
<shape android:shape="oval">
<solid android:color="#color/switch_thumb"/>
<!-- NOTE did not work, had to set Switch's thumbTextPadding to the radius -->
<!--
<padding android:right="#dimen/switch_thumb_radius"
android:left="#dimen/switch_thumb_radius"/>
-->
</shape>
</item>
</layer-list>
</item>
</selector>
I set the on and off text of switch to empty (actually to "" to prevent warning about empty resource).
track:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape android:shape="rectangle">
<size android:height="#dimen/switch_track_height"/>
<corners android:radius="#dimen/switch_thumb_radius"/>
<solid android:color="#color/switch_track_off"/>
</shape>
</item>
<item android:state_checked="true">
<shape android:shape="rectangle">
<size android:height="#dimen/switch_track_height"/>
<corners android:radius="#dimen/switch_thumb_radius"/>
<solid android:color="#color/switch_track_on"/>
</shape>
</item>
</selector>
Switch style:
<style name="CustomSwitch">
<!-- NOTE this way the switch will be as width as required minimally -->
<item name="android:switchMinWidth">0dp</item>
<item name="android:track">#drawable/switch_track</item>
<item name="android:thumb">#drawable/switch_thumb</item>
<item name="android:textOff">#string/switch_thumb_off</item>
<item name="android:textOn">#string/switch_thumb_on</item>
<!-- NOTE if set to 0dp, the thumb was not visible even with padding
of the thumb drawable set to -->
<item name="android:thumbTextPadding">#dimen/switch_thumb_radius</item>-->
<!--<item name="android:thumbTextPadding">0dp</item>-->
</style>
And finally, the dimens:
<dimen name="switch_track_height">30dp</dimen>
<dimen name="switch_thumb_radius">15dp</dimen>
<dimen name="switch_thumb_padding">2dp</dimen>
So the only 'tricky' thing is to keep height = radius * 2.
No clue why this happens but solved it by placing the Switch-button within a linearLayout.
There must be some propery(width) of the thumb that has a "match_parent" of some sort that must've been causing it.
Edit: It happens when I remove the default 'ON' and 'OFF' text. So to hide the text i changed its color to white.
How to change textcolor of switch in Android
main.java
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.toggle_button);
Switch sw = (Switch) findViewById(R.id.switch1);
//Both of these need to be used to change the text color to white
sw.setTextColor(Color.WHITE);
sw.setSwitchTextAppearance(this, Color.WHITE);
//Doing this would skew the circle
//sw.setTextOn(" ");
//sw.setTextOff(" ");
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp" >
<Switch
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="#drawable/switch_thumb_selector"
android:track="#drawable/switch_track_selector" />
</LinearLayout>
You can always horizontally stretch the graphic itself (ie, "switch_thumb.png") if you can't use the other answers.
If you use the original software used to design the graphic and re-export it after stretching, it shouldn't cause blurring of the image. You'll have to eye-ball it for your dimensions, but starting at +30% stretching should get you close.

Android - styling seek bar

I wanted to style a seek bar which looks like the one in the image below.
By using default seekbar I will get something like this:
So what I need is to only change the color. I need no extra styles. Is there any straight forward approach to do this or should I build my custom drawable.?
I tried building custom one, but I could not get the exact one as shown above.
After using custom drawable, what I get is as shown below:
If I need to build the custom one, then please suggest how to reduce the width of the progress line and also the shape.
my custom implementation:
background_fill.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="90"
android:centerColor="#FF555555"
android:endColor="#FF555555"
android:startColor="#FF555555" />
<corners android:radius="1dp" />
<stroke
android:width="1dp"
android:color="#50999999" />
<stroke
android:width="1dp"
android:color="#70555555" />
</shape>
progress_fill.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="90"
android:centerColor="#FFB80000"
android:endColor="#FFFF4400"
android:startColor="#FF470000" />
<corners android:radius="1dp" />
<stroke
android:width="1dp"
android:color="#50999999" />
<stroke
android:width="1dp"
android:color="#70555555" />
</shape>
progress.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/background_fill"/>
<item android:id="#android:id/progress">
<clip android:drawable="#drawable/progress_fill" />
</item>
</layer-list>
thumb.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<gradient
android:angle="270"
android:endColor="#E5492A"
android:startColor="#E5492A" />
<size
android:height="20dp"
android:width="20dp" />
</shape>
seekbar:
<SeekBar
android:id="#+id/seekBarDistance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="88dp"
android:progressDrawable="#drawable/progress"
android:thumb="#drawable/thumb" >
</SeekBar>
I would extract drawables and xml from Android source code and change its color to red.
Here is example how I completed this for mdpi drawables:
Custom red_scrubber_control.xml (add to res/drawable):
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/red_scrubber_control_disabled_holo" android:state_enabled="false"/>
<item android:drawable="#drawable/red_scrubber_control_pressed_holo" android:state_pressed="true"/>
<item android:drawable="#drawable/red_scrubber_control_focused_holo" android:state_selected="true"/>
<item android:drawable="#drawable/red_scrubber_control_normal_holo"/>
</selector>
Custom: red_scrubber_progress.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#android:id/background"
android:drawable="#drawable/red_scrubber_track_holo_light"/>
<item android:id="#android:id/secondaryProgress">
<scale
android:drawable="#drawable/red_scrubber_secondary_holo"
android:scaleWidth="100%" />
</item>
<item android:id="#android:id/progress">
<scale
android:drawable="#drawable/red_scrubber_primary_holo"
android:scaleWidth="100%" />
</item>
</layer-list>
Then copy required drawables from Android source code, I took from this link
It is good to copy these drawables for each hdpi, mdpi, xhdpi. For example I use only mdpi:
Then using Photoshop change color from blue to red:
red_scrubber_control_disabled_holo.png:
red_scrubber_control_focused_holo.png:
red_scrubber_control_normal_holo.png:
red_scrubber_control_pressed_holo.png:
red_scrubber_primary_holo.9.png:
red_scrubber_secondary_holo.9.png:
red_scrubber_track_holo_light.9.png:
Add SeekBar to layout:
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progressDrawable="#drawable/red_scrubber_progress"
android:thumb="#drawable/red_scrubber_control" />
Result:
Android seekbar custom material style, for other seekbar customizations http://www.zoftino.com/android-seekbar-and-custom-seekbar-examples
<style name="MySeekBar" parent="Widget.AppCompat.SeekBar">
<item name="android:progressBackgroundTint">#f4511e</item>
<item name="android:progressTint">#388e3c</item>
<item name="android:thumbTint">#c51162</item>
</style>
Upd: colorControlActivated is thumbTint now.
My answer is inspired from Andras Balázs Lajtha answer
it allow to work without xml file
seekBar.getProgressDrawable().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
seekBar.getThumb().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
If you want exactly the same bar but in red, you can add a PorterDuff color filter programatically. You can get each drawable that you want to colorize through the methods of the ProgressBar base class. Then set a color filter for it.
mySeekBar.getProgressDrawable().setColorFilter(new PorterDuffColorFilter(srcColor, PorterDuff.Mode.MULTIPLY));
If the wanted color adjustment can't be made through a Porter Duff filter, you can specify your own color filter.
Google have made this easier in SDK 21. Now we have attributes for specifying the thumb tint colors:
android:thumbTint
android:thumbTintMode
android:progressTint
http://developer.android.com/reference/android/widget/AbsSeekBar.html#attr_android:thumbTint
http://developer.android.com/reference/android/widget/AbsSeekBar.html#attr_android:thumbTintMode
Just replace color atributtes with your color
background.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke android:width="1dp" android:color="#D9D9D9"/>
<corners android:radius="1dp" />
</shape>
progress.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke android:width="1dp" android:color="#2EA5DE"/>
<corners android:radius="1dp" />
</shape>
style.xml
<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>
thumb.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size android:height="30dp" android:width="30dp"/>
<stroke android:width="18dp" android:color="#882EA5DE"/>
<solid android:color="#2EA5DE" />
<corners android:radius="1dp" />
</shape>
All those answers are deprecated.
In 2019 it's easier to style your seekbar to your preferred color by changing your ProgressBar to this
<SeekBar
android:id="#+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progressTint="#36970f"
android:thumbTint="#36970f"
/>
Styling your seekbar color programmatically try the following code
seekBar.getProgressDrawable().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
seekBar.getThumb().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
For APIs < 21 (and >= 21) you can use the answer of #Ahamadullah Saikat or https://www.lvguowei.me/post/customize-android-seekbar-color/.
<SeekBar
android:id="#+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxHeight="3dp"
android:minHeight="3dp"
android:progress="50"
android:progressDrawable="#drawable/seek_bar_ruler"
android:thumb="#drawable/seek_bar_slider"
/>
drawable/seek_bar_ruler.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">
<shape android:shape="rectangle">
<solid
android:color="#94A3B3" />
<corners android:radius="2dp" />
</shape>
</item>
<item android:id="#android:id/progress">
<clip>
<shape android:shape="rectangle">
<solid
android:color="#18244D" />
<corners android:radius="2dp" />
</shape>
</clip>
</item>
</layer-list>
drawable/seek_bar_slider.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>
<solid android:color="#FFFFFF" />
<stroke
android:width="4dp"
android:color="#18244D"
/>
<size
android:width="25dp"
android:height="25dp"
/>
</shape>
output:
Change Progress Color:
int normalColor = ContextCompat.getColor(context, R.color.normal_color);
int selectedColor = ContextCompat.getColor(context, R.color.selected_color);
Drawable normalDrawable = new ColorDrawable(normalColor);
Drawable selectedDrawable = new ColorDrawable(selectedColor);
ClipDrawable clipDrawable = new ClipDrawable(selectedDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
Drawable[] layers = {normalDrawable, clipDrawable, clipDrawable};
LayerDrawable seekbarDrawable = new LayerDrawable(layers);
seekBar.setProgressDrawable(seekbarDrawable);
Change Thumb Color:
int thumbColor = ContextCompat.getColor(context, R.color.thumb_color);
Drawable unwrappedDrawable = AppCompatResources.getDrawable(context, R.drawable.ic_seekbar_thumb);
assert unwrappedDrawable != null;
Drawable wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
DrawableCompat.setTint(wrappedDrawable, thumbColor);
seekBar.setThumb(wrappedDrawable);
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:progressTint="#color/red"
android:thumbTint="#color/red" />
works Like same the answer selected. no need to make any drawable file or else.(IN 5.0 only)
Regards
Just set
android:maxHeight="3dp"
android:minHeight="3dp"
That's all
As mention one above (#andrew) , creating custom SeekBar is super Easy with this site - http://android-holo-colors.com/
Just enable SeekBar there, choose color, and receive all resources and copy to project.
Then apply them in xml, for example:
android:thumb="#drawable/apptheme_scrubber_control_selector_holo_light"
android:progressDrawable="#drawable/apptheme_scrubber_progress_horizontal_holo_light"
LayerDrawable progressDrawable = (LayerDrawable) mSeekBar.getProgressDrawable();
// progress bar line *progress* color
Drawable processDrawable = progressDrawable.findDrawableByLayerId(android.R.id.progress);
// progress bar line *background* color
Drawable backgroundDrawable = progressDrawable.findDrawableByLayerId(android.R.id.background);
// progress bar line *secondaryProgress* color
Drawable secondaryProgressDrawable = progressDrawable.findDrawableByLayerId(android.R.id.secondaryProgress);
processDrawable.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
// progress bar line all color
mSeekBar.getProgressDrawable().setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN);
// progress circle color
mSeekBar.getThumb().setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_IN);
By default, android will match the progress color of your slider to
`<item name="colorAccent">`
value in your styles.xml. Then, to set a custom slider thumb image, just use this code in your SeekBar xml layout's block:
android:thumb="#drawable/slider"
If you are using default SeekBar provided by android Sdk then their is a simple way to change the color of that . just go to color.xml inside /res/values/colors.xml and change the colorAccent.
<resources>
<color name="colorPrimary">#212121</color>
<color name="colorPrimaryDark">#1e1d1d</color>
<!-- change below line -->
<color name="colorAccent">#FF4081</color>
</resources>
Simple Way to change the seek bar color ...
<ProgressBar
android:id="#+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:theme="#style/Progress_color"/>
Style : Progress_color
<style name="Progress_color">
<item name="colorAccent">#color/white</item> <!-- Whatever color you want-->
</style>
java class change ProgressDrawable()
seek_bar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.MULTIPLY);
I change the background_fill.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<size android:height="1dp" />
<gradient
android:angle="0"
android:centerColor="#616161"
android:endColor="#616161"
android:startColor="#616161" />
<corners android:radius="0dp" />
<stroke
android:width="1dp"
android:color="#616161" />
<stroke
android:width="1dp"
android:color="#616161" />
</shape>
and the progress_fill.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<size android:height="1dp" />
<gradient
android:angle="0"
android:centerColor="#fafafa"
android:endColor="#fafafa"
android:startColor="#fafafa" />
<corners android:radius="1dp" />
<stroke
android:width="1dp"
android:color="#cccccc" />
<stroke
android:width="1dp"
android:color="#cccccc" />
</shape>
to make the background & progress 1dp height.
If you look at the Android resources, the seek bar actually use images.
You have to make a drawable which is transparent on top and bottom for say 10px and the center 5px line is visible.
Refer attached image. You need to convert it into a NinePatch.
With the Material Component Library version 1.2.0 you can use the new Slider component.
<com.google.android.material.slider.Slider
android:valueFrom="0"
android:valueTo="300"
android:value="200"
android:theme="#style/slider_red"
/>
You can override the default color using something like:
<style name="slider_red">
<item name="colorPrimary">#......</item>
</style>
Otherwise you can use these attribute in the layout to define a color or a color selector:
<com.google.android.material.slider.Slider
app:activeTrackColor="#color/...."
app:inactiveTrackColor="#color/...."
app:thumbColor="#color/...."
/>
or you can use a custom style:
<style name="customSlider" parent="Widget.MaterialComponents.Slider">
<item name="activeTrackColor">#color/....</item>
<item name="inactiveTrackColor">#color/....</item>
<item name="thumbColor">#color/....</item>
</style>
Now a days it becomes very easy, use material slider and use this to customize the color:
app:thumbColor="#color/colorPrimary"
app:trackColorInactive="#color/grey"
app:trackColorActive="#color/colorPrimary"
First Create A Drawble file using these steps
and name the drawble and specify Root element as layer-list -> click on OK. a new file custom_seekbar.xml will be created
Now in custom_seekbar.xml inside the layer-list add an item and give a shape to it. specify the color, height, corners of the SeekBar. Also, add another item of the same shape and size but you can change the color, left part of SeekBar’s thumb will be of this color.
Now again click on drawable -> new -> drawable resource file, name the file as thumb.xml and specify Root element as shape -> click on OK. a new file thumb.xml will be created. Inside this file give the height, radius, and color of the thumb. these things can be changed. It totally depends upon how you want to design.
Now go to the activity_main.xml create a layout and inside the layout add a SeekBar. Specify the height width of SeekBar and the max progress that you want to use set progress to 0.
android:progressDrawable="#drawable/custom_seekbar"
android:thumb="#drawable/thumb"
android:max="50"
android:progress="0"
This will create a customized Seekbar inside activity_main.xml.
Now open MainActivity.java class Declare objects of SeekBar and TextView, inside onCreate method initialize both objects using findViewById() method. Perform an event of SeekBar change listener that will hold progress value, and by using this event set the progress value inside TextView.
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
// increment or decrement on process changed
// increase the textsize
// with the value of progress
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
value.setText(progress+"/"+"50");
}..
Build and run the app. Put the thumb on Seekbar and move it either forward or backward it will display the process.
Code for the above implementation is given below:
Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
package com.abhi.customseekbar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
SeekBar seekBar;
TextView value;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialize the seekBar object
seekBar=findViewById(R.id.seekbar);
value=findViewById(R.id.progress);
// calling the seekbar event change listener
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
// increment or decrement on process changed
// increase the textsize
// with the value of progress
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
value.setText(progress+"/"+"50");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// This method will automatically
// called when the user touches the SeekBar
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// This method will automatically
// called when the user
// stops touching the SeekBar
}
});
}
}
Below is the code for the activity_main.xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#458C85"
tools:context=".MainActivity">
<RelativeLayout
android:id="#+id/Relative_1"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_weight="2">
<TextView
android:id="#+id/textViewSelectSizeOfArray"
android:layout_width="273dp"
android:layout_height="wrap_content"
android:layout_above="#+id/seekbar"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="9dp"
android:text="Custom Seek Bar"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="25dp" />
<SeekBar
android:id="#+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminate="false"
android:max="50"
android:progress="0"
android:progressDrawable="#drawable/custom_seekbar"
android:thumb="#drawable/thumb" />
<TextView
android:id="#+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="199dp"
android:padding="10dp"
android:text="0"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="30dp"
android:textStyle="bold">
</TextView>
</RelativeLayout>
</LinearLayout>
Below is the code for the custom_seekbar.xml file.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- color, size, shape height of seekbar -->
<item android:gravity="center_vertical">
<shape android:shape="rectangle">
<solid android:color="#605A5C"/>
<size android:height="30dp"/>
<corners android:radius="9dp"/>
</shape>
</item>
<!-- color, size, shape height of seekbar when u drag it-->
<item android:gravity="center_vertical">
<scale android:scaleWidth="100%">
<selector>
<item android:state_enabled="false"
android:drawable="#color/purple_200"/>
<item>
<shape android:shape="rectangle">
<solid android:color="#color/black"/>
<size android:height="30dp"/>
<corners android:radius="9dp"/>
</shape>
</item>
</selector>
</scale>
</item>
</layer-list>
Below is the code for the thumb.xml file.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/purple_200"/>
<size android:height="30dp" android:width="25dp"/>
<corners android:radius="5dp"/>
</shape>
For those who use Data Binding:
Add the following static method to any class
#BindingAdapter("app:thumbTintCompat")
public static void setThumbTint(SeekBar seekBar, #ColorInt int color) {
seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_IN);
}
Add app:thumbTintCompat attribute to your SeekBar
<SeekBar
android:id="#+id/seek_bar"
style="#style/Widget.AppCompat.SeekBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:thumbTintCompat="#{#android:color/white}"
/>
That's it. Now you can use app:thumbTintCompat with any SeekBar. The progress tint can be configured in the same way.
Note: this method is also compatble with pre-lollipop devices.
Small correction to the answer by #arnav-rao:
to make sure that the thumb is colored properly, use:
<style name="MySeekBar" parent="Widget.AppCompat.SeekBar">
<item name="android:progressBackgroundTint">#color/colorBgTint</item>
<item name="android:progressTint">#color/colorProgressTint</item>
<item name="android:thumbTint">#color/colorThumbTint</item>
</style>
here android:thumbTint actually colors the "thumb"
check this tutorial very good
https://www.lvguowei.me/post/customize-android-seekbar-color/
simple :
<SeekBar
android:id="#+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:maxHeight="3dp"
android:minHeight="3dp"
android:progressDrawable="#drawable/seekbar_style"
android:thumbTint="#color/positive_color"/>
then a style file:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#android:id/background">
<shape android:shape="rectangle" >
<solid
android:color="#color/positive_color" />
</shape>
</item>
<item android:id="#android:id/progress">
<clip>
<shape android:shape="rectangle" >
<solid
android:color="#color/positive_color" />
</shape>
</clip>
</item>
</layer-list>
I wanted to create a style for the seekbar and then add the style to a theme so that I could have it applied to an entire theme while still allowing a substyle to override it. Here is what I did:
<!-- Blue App Theme -->
<style name="AppTheme.Blue" parent="BaseTheme.Blue">
<item name="android:seekBarStyle">#style/SeekbarStyle.Blue</item>
<item name="seekBarStyle">#style/SeekbarStyle.Blue</item> <!--This is essential for some devices, e.g. Samsung-->
</style>
<!-- Slider Styles -->
<style name="SeekbarStyle.Blue" parent="Widget.AppCompat.SeekBar">
<item name="android:progressBackgroundTint">#color/material_blue_a700_pct_30</item>
<item name="android:thumbTint">#color/material_blue_a700</item>
<item name="android:thumbTintMode">src_in</item>
<item name="android:progressTint">#color/material_blue_a700</item>
</style>
<style name="SeekbarStyle.Green" parent="Widget.AppCompat.SeekBar">
<item name="android:progressBackgroundTint">#color/material_green_a700_pct_30</item>
<item name="android:thumbTint">#color/material_green_a700</item>
<item name="android:thumbTintMode">src_in</item>
<item name="android:progressTint">#color/material_green_a700</item>
</style>
The above sets the Theme seekbar style to blue for all devices 21+ including Samsung (which need the seekBarStyle applied in addition to android:seekBarStyle; not sure why but I saw this issue firsthand). If you want all seekbars to keep the theme style and only apply the green seekbar style to a few widgets then add the following to the widget you want:
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.Green"/>
You can also make it this way :
<SeekBar
android:id="#+id/redSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progressDrawable="#color/red"
android:maxHeight="3dip"/>
Hope it will help!

Categories

Resources