How to change the background of a SeekBar? - android

I would like to change the background color of a SeekBar from yellow (default) to a color I choose but I've only found the method to change the color of the black area around the SeekBar. Can anyone help me????

xmls ( background_fill , progress_fill and progress could look like that for a gradient red
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>
background_fill xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#FF555555" android:centerColor="#FF555555"
android:endColor="#FF555555" android:angle="90" />
<corners android:radius="5px" />
<stroke android:width="2dp" android:color="#50999999" />
<stroke android:width="1dp" android:color="#70555555" />
</shape>
and progress_fill xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#FF470000" android:centerColor="#FFB80000"
android:endColor="#FFFF4400" android:angle="180" />
<corners android:radius="5px" />
<stroke android:width="2dp" android:color="#50999999" />
<stroke android:width="1dp" android:color="#70555555" />
</shape>
i did not complete the implementing for android:thumb, so the thumb will be still the original one
Therefore we just have to delete this line again from our layout xml where we define the seekbar
android:thumb="#drawable/thumb"
for more info see this link

Related

Seekbar thumb with transparent right and left padding/margin

Summarize the problem:
Hello, I want to create Seekbar like this one in the design:
So the thumb has something like padding/margin at left and right and bars are rounded on both sides.
What I've tried so far:
I've already tried to create thumb picture like this:
Unfortunately, it turned out that I can't use this picture because I need to change this black halo around green point programmatically.
That's why I've built seekbar completely in XML (no pngs images):
Seekbar:
<SeekBar
android:id="#+id/player_seekbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
style="#style/SeekBarPlayer"
local:MvxBind="Max Duration; Progress SliderPosition, Mode=OneWay; SecondaryProgress Downloaded; Visibility(LivePlaybackToVisibility(CurrentTrack))"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView" />
SeekbarBackground:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<corners android:radius="5dp" />
<solid android:color="#3d4b58" />
</shape>
</item>
</layer-list>
SeekbarSecondaryProgress:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/progressshapesecondary" >
<clip>
<shape
android:shape="rectangle" >
<size android:height="5dp"/>
<corners
android:radius="5dp" />
<solid android:color="#8883e76b"/>
</shape>
</clip>
</item>
</layer-list>
SeekbarProgress:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/progressshape" >
<clip>
<shape
android:shape="rectangle" >
<size android:height="5dp"/>
<corners
android:radius="5dp" />
<solid android:color="#83e76b"/>
</shape>
</clip>
</item>
</layer-list>
And it provides me a seekbar like this:
Summarazing
How can I achieve the following seekbar design from now on?
Use below code for thumb drawable:
seekbar_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#8883e76b"/>
<stroke android:color="#color/black" android:width="3dp"/>
<size android:height="30dp" android:width="30dp"/>
</shape>
</item>
</selector>
Use below code for seekbar, just change color what you want:
<item android:id="#android:id/background">
<shape android:shape="rectangle">
<corners android:radius="20dp" />
<solid android:color="#color/fofo_grey" />
</shape>
</item>
<item android:id="#android:id/progress">
<clip>
<shape android:shape="rectangle">
<corners android:radius="20dp" />
<solid android:color="#color/signup_green" />
<stroke
android:width="6dp"
android:color="#color/fofo_grey" />
</shape>
</clip>
</item>

why different result of same xml only with border?

I use following xml, it works on most device , but in some devices the background is not trasparent, is black? why and how solve?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<stroke android:width="#dimen/_3sdp" android:color="#dcdcdc" />
<corners android:radius="#dimen/_10sdp" />
<padding android:bottom="#dimen/_6sdp" android:left="#dimen/_6sdp" android:right="#dimen/_6sdp" android:top="#dimen/_6sdp" />
</shape>
</item>
</selector>
The result must be like this :
In your above xml border, you've only specified the color of shape. You didn't mention the color of background. You can set solid background color as Transparent inside shape tag:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<stroke android:width="#dimen/_3sdp" android:color="#dcdcdc" />
<corners android:radius="#dimen/_10sdp" />
<padding android:bottom="#dimen/_6sdp" android:left="#dimen/_6sdp" android:right="#dimen/_6sdp" android:top="#dimen/_6sdp" />
<solid android:color="#android:color/transparent" /> <!-- Add This -->
</shape>
</item>
Hope this helps.

How can I create the following shape in Android?

I am trying to create a following shape in android.
I tried following code but it's only for changing the corners
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ffffff" />
<gradient
android:startColor="#00EBCF"
android:endColor="#00BEEC"
android:angle="180"
android:type="linear" />
<corners
android:bottomLeftRadius="80dp"
android:bottomRightRadius="80dp"
android:topLeftRadius="0dp"
android:topRightRadius="0dp" />
What do I need to make changes in code to achieve the above shape?
Edit1
As per the answer I used following code (modifications in layout height):
<?xml version="1.0" encoding="utf-8"?>
</shape>
</item>
<item
android:width="1500dp"
android:height="200dp"
android:top="95dp">
<shape android:shape="oval">
<solid android:color="#36D6E9" />
</shape>
</item>
But when I am using this layout as a background for LinearLayout. It's showing me this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/header_shape_rect">
use this as an example
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:height="200dp">
<shape android:shape="rectangle">
<solid android:color="#ccc"/>
</shape>
</item>
<item android:height="200dp" android:top="95dp" android:width="1500dp" >
<shape android:shape="oval">
<solid android:color="#ccc"/>
</shape>
</item>
</layer-list>

How can I set the width and colour of all the borders of EditText?

I have EditText. How can I set its border width and colour only and in xml? I can't figure it out and all I've found so far didn't explain that in a simple way. But I believe that so such a simple thing there must be a simple way to do it.
Create a drawable xml for your EditText with named EditTextStyle.xml in your Drawable Folder like below
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="2dp"
android:color="#000000" />
</shape>
Change your color and width according to your requirement and add this as the background of your EditText of your Layout xml
android:background="#drawable/EditTextStyle"
You can create Customize editText xml in Drawable folder like
custom_edttext.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#00000000" />
<stroke
android:width="1dp"
android:color="#641E1E" />
<corners
android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
/>
</shape>
and use custom_edittext.xml as background of editText
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/custom_edittext"/>
I don't have enough karma to say this as a comment. So typing as answer. See this link. Just create an xml file in drawable(here rounded_edittext.xml in the example) and set background for the EditText as
android:background="#drawable/rounded_edittext"
The <stroke> in the rounded_edittext.xml is used to set the thickness and color of the border of the EditText.
Here's the rounded_edittext.xml as shown in the link.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<stroke android:width="1dp" android:color="#2f6699" />
<corners android:topLeftRadius="10dp"
android:topRightRadius="10dp" android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp" />
</shape>
I hope this answer match with your needs.
<?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="#76b385" />
<corners android:radius="12dp" />
</shape>
</item>
<item
android:bottom="5dp"
android:left="1dp"
android:right="1dp">
<shape android:shape="rectangle" >
<gradient
android:angle="-90"
android:centerColor="#ffffff"
android:centerX="50%"
android:endColor="#ffffff"
android:startColor="#ededed"
android:type="linear" />
<corners android:radius="5dp" />
</shape>
</item>
</layer-list>
Modify this xml with your needs.
put this file in your drawable folder
edit_text_back.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#00000000" />
<stroke
android:width="1dp"
android:color="#641E1E" />
<corners
android:radius="5dp" />
</shape>
and add use this drawable as background to your edittext as
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/edit_text_back"/>
If you need any assistance.Please let me know and mark this answer up.if it helps.Happy coding :)

How can I add depth to a shape

I need to make my EditText look like its got depth like in the image below.
I have this shape I don't know what to add to it
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="50dp" />
<solid android:color="#color/primary_dark" />
</shape>
you can accomplish that with a drawable like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--background to compensate the transparency under shadow and glow (for bigger depth values)-->
<item>
<shape android:shape="rectangle">
<corners android:radius="50dp" />
<solid android:color="#color/primary_dark" />
</shape>
</item>
<!--top, dark, inner shadow-->
<item
android:bottom="#dimen/depth">
<shape android:shape="rectangle">
<corners android:radius="50dp" />
<solid android:color="#1A000000" />
</shape>
</item>
<!--bottom, light, outer glow-->
<item
android:top="#dimen/depth">
<shape android:shape="rectangle">
<corners android:radius="50dp" />
<solid android:color="#4DFFFFFF" />
</shape>
</item>
<!--your color-->
<item
android:top="#dimen/depth"
android:bottom="#dimen/depth">
<shape android:shape="rectangle">
<corners android:radius="50dp" />
<solid android:color="#color/primary_dark />
</shape>
</item>
</layer-list>
and define a dimen in dimens.xml
<dimen name="depth">2dp</dimen>
by changing of the dimen you can change the depth of the drawable
This might also make your field look close to the effect but with a little more depth (see below). It might be worth adding the background to a outer view to wrap the edit text too so you can indent the text if you haven't already done this.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- outer shine -->
<item
android:top="4dp"
android:left="4dp">
<shape android:shape="rectangle">
<corners android:radius="50dp" />
<solid android:color="#1597FB" />
</shape>
</item>
<!-- inner shadow -->
<item>
<shape android:shape="rectangle">
<corners android:radius="50dp" />
<solid android:color="#004173" />
</shape>
</item>
<!-- inner background -->
<item
android:top="2dp"
android:left="2dp">
<shape android:shape="rectangle">
<corners android:radius="50dp" />
<solid android:color="#006EC3" />
</shape>
</item>
</layer-list>
And then you EditText as below;
<FrameLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:background="#drawable/rounded_rect_layered_combined">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:textColor="#ffffff"/>
</FrameLayout>

Categories

Resources