Android switches - making thumb height larger than track height, how? - android

Is there any way to make the height of the thumb of a switch bigger than the track height in Android versions prior to Lollipop ?
I am essentially trying to create a material design-like switch widget in Kitkat (and older versions- the problem here is the thumb by default 'fits' inside the track). I have been working on this for quite some time now but I can't seem to arrive at the correct design.
I followed the solutions on this SO post: How to change the size of a Switch Widget.
Any suggestions or a solution would be most appreciated.

I added a transparent stroke to my track drawable shape which results in a padding around the track:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/track_color"/>
<size android:height="#dimen/track_height" />
<size android:width="#dimen/track_width" />
<!-- results in the track looking smaller than the thumb -->
<stroke
android:width="6dp"
android:color="#00ffffff"/>
</shape>

Yup
I solved this
<android.support.v7.widget.SwitchCompat
android:id="#+id/mySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="ON"
android:textOff="OFF"
style="#style/Color1SwitchStyle"
android:text="" />
This is My Color1SwitchStyle
<style name="Color1SwitchStyle">
<item name="android:thumb">#drawable/customswitchselector</item>
<item name="android:track">#drawable/my_custom_track</item>
//when use Switch in xml file
<item name="track">#drawable/my_custom_track</item>
//when use android.support.v7.widget.SwitchCompat in xml
</style>
*Hear is my customswitchselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true"
android:drawable="#drawable/on" >
<shape
android:shape="rectangle"
android:dither="true"
android:useLevel="false"
android:visible="true">
<gradient
android:startColor="#66AAFF00"
android:endColor="#6600FF00"
android:angle="270"/>
<corners
android:radius="15dp"/>
<size
android:width="200dp"
android:height="80dp" />
<stroke
android:width="4dp"
android:color="#0000ffff"/>
</shape>
</item>
<item android:state_checked="false"
android:drawable="#drawable/off">
<shape
android:shape="rectangle"
android:dither="true"
android:useLevel="false"
android:visible="true">
<gradient
android:startColor="#66AAFF00"
android:endColor="#6600FF00"
android:angle="270"/>
<corners
android:radius="15dp"/>
<size
android:width="200dp"
android:height="80dp" />
<stroke
android:width="4dp"
android:color="#0000ffff"/>
</shape>
</item>
</selector>
And hear is magic my my_custom_track.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:state_enabled="true"
android:drawable="#drawable/switch_track_on" />
<item
android:state_checked="false"
android:state_enabled="false"
android:drawable="#drawable/switch_track_off" />
</selector>
switch_track_on.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle"
android:dither="true"
android:useLevel="false"
android:visible="true">
<gradient
android:startColor="#color/profile_pic_bg"
android:endColor="#color/profile_pic_bg"
android:angle="270"/>
<corners
android:radius="0dp"/>
<size
android:width="100dp"
android:height="10dp" />
<stroke
android:width="12dp"
android:color="#00ffffff"/>
</shape>
</item>
</layer-list>
switch_track_off.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle"
android:dither="true"
android:useLevel="false"
android:visible="true">
<gradient
android:startColor="#color/gray"
android:endColor="#color/gray"
android:angle="270"/>
<corners
android:radius="0dp"/>
<size
android:width="100dp"
android:height="10dp" />
<stroke
android:width="12dp"
android:color="#00ffffff"/>
</shape>
</item>
</layer-list>
Thanks stevenp without your comment i cannot solve this

Adding top and bottom inset in the 'track' drawable works for me. android:bottom="5dp" android:top="5dp"
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="5dp"
android:top="5dp">
<shape>
<corners android:radius="15dp" />
<solid android:color="#android:color/transparent" />
<stroke
android:width="2dp"
android:color="#EEEEEE" />
</shape>
</item>
<androidx.appcompat.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:track="#drawable/switch_track_drawable" />

The solution I came up with is just to use for the track a shape that is a line instead of a rectangle, and set the width explicitly in its stroke.
For example if the switch is defined as follows:
<android.support.v7.widget.SwitchCompat
android:id="#+id/my_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:track="#drawable/switch_track"
app:theme="#android:style/Theme.DeviceDefault.Light" />
The in the switch_track.xml drawable use a line shape with whatever width you desire:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false" >
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line" >
<stroke android:color="#50bbb9bf" android:width="5dp"/>
</shape>
</item>
<item android:state_checked="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">
<stroke android:color="#5000b6de" android:width="5dp"/>
</shape>
</item>
</selector>

Related

Android custom radiobutton

I want to make custom radio button.I have created a drawable for normal(radio_normal) state.But I find it difficult to create a shape for checked state(radio_pressed).I have tried creating my desired way.But didn't work out well.I don't know if I have done it the right way.For checked state the shape should look like this.The outer border and the inner circle must be white in color.Can anybody help me with that?
radio_normal:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:useLevel="false">
<solid
android:color="#2196f3"></solid>
</shape>
radio_pressed:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#android:color/transparent">
</solid>
<size
android:height="60dp"
android:width="60dp">
</size>
<stroke
android:color="#ffffff"
android:width="1dp"
>
</stroke>
</shape>
</item>
<item>
<shape android:shape="oval">
<solid
android:color="#ffffff">
</solid>
</shape>
<size
android:height="30dp"
android:width="30dp">
</size>
</item>
</layer-list>
radio_bg:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="#drawable/radio_pressed"
>
</item>
<item
android:state_checked="false"
android:drawable="#drawable/radio_normal"
>
</item>
</selector>
activity_main:
<RadioButton
android:id="#+id/radio"
android:layout_centerInParent="true"
android:layout_width="10dp"
android:button="#drawable/radio_bg"
android:layout_marginTop="100dp"
android:layout_height="10dp"
/>
This is about as close as I can get you, hope it works on your side and that it helps :)
radio_normal:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:useLevel="false">
<solid android:color="#2196f3" />
<size
android:width="25dp"
android:height="25dp" />
</shape>
radio_pressed:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:padding="3dp">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:useLevel="false">
<solid android:color="#android:color/white" />
<size
android:width="25dp"
android:height="25dp" />
</shape>
</item>
<item
android:bottom="7dp"
android:left="7dp"
android:right="7dp"
android:top="7dp">
<shape
android:shape="oval"
android:useLevel="false">
<solid android:color="#android:color/white" />
</shape>
</item>
</layer-list>
radio_bg:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="#drawable/radio_pressed"
>
</item>
<item
android:state_checked="false"
android:drawable="#drawable/radio_normal"
>
</item>
</selector>
activity_main:
<RadioButton
android:id="#+id/radio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:button="#drawable/radio_bg"/>
Let me know if this worked for you :)
Is this maybe what you were looking for ?
radio_pressed:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:useLevel="false">
<solid android:color="#android:color/white" />
<size
android:width="10dp"
android:height="10dp" />
</shape>
</item>
<item
android:bottom="3dp"
android:left="3dp"
android:right="3dp"
android:top="3dp">
<shape
android:shape="oval"
android:useLevel="false">
<solid android:color="#android:color/white" />
</shape>
</item>
</layer-list>
Reaction to comment:
The code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:useLevel="false">
<solid android:color="#android:color/white" />
<size
android:width="150dp"
android:height="150dp" />
</shape>
</item>
<item
android:bottom="50dp"
android:left="50dp"
android:right="50dp"
android:top="50dp">
<shape
android:shape="oval"
android:useLevel="false">
<solid android:color="#android:color/white" />
</shape>
</item>
</layer-list>
Note: The only changes made was in the sizes to accomodate your imageview of 150dp X 150dp. When using it for your radio button I highly reccomend using the 10x10dp sizes

How to fix custom switch appearance on Android 4

I have a custom switch element which looks great on Android 5 and 6. But there is a problem with Android 4.
The question is how to keep shape of the toggle circular?
Android 5,6. Picture 1
Android 4. Picture 2
Here is my code:
<Switch
android:id="#+id/row_device_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textOn="ON"
android:thumb="#drawable/customswitchselector"
android:track="#drawable/custom_track"
android:textOff="OFF"
android:layout_marginRight="14dp"
android:switchMinWidth="#dimen/custom_switcher_track_width"
/>
custom_track.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle"
android:visible="true"
android:dither="true"
android:useLevel="false"
>
<corners
android:radius="#dimen/cutsom_switcher_track_radius"/>
<size
android:width="#dimen/custom_switcher_track_width"
android:height="#dimen/custom_switcher_track_height" />
<stroke
android:width="#dimen/custom_switcher_stroke_thickness"
android:color="#android:color/white"
/>
</shape>
</item>
<item
android:left="#dimen/custom_switcher_track_line_horizontal_margin"
android:right="#dimen/custom_switcher_track_line_horizontal_margin"
>
<shape
android:shape="line"
>
<stroke
android:width="#dimen/custom_switcher_екфсл_thickness"
android:color="#android:color/white"
/>
</shape>
</item>
</layer-list>
customswitchselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true">
<layer-list>
<item>
<shape
android:shape="oval"
android:visible="true"
android:dither="true"
android:useLevel="false"
>
<solid
android:color="#color/colorListEnd"
/>
<size
android:width="#dimen/custom_switcher_circle_size"
android:height="#dimen/custom_switcher_circle_size" />
<stroke
android:width="#dimen/custom_switcher_stroke_thickness"
android:color="#android:color/white"/>
</shape>
</item>
<item
android:left="#dimen/custom_switcher_between_circles_margin"
android:right="#dimen/custom_switcher_between_circles_margin"
android:top="#dimen/custom_switcher_between_circles_margin"
android:bottom="#dimen/custom_switcher_between_circles_margin"
>
<shape
android:shape="oval"
>
<size
android:width="#dimen/custom_switcher_inner_circle_size"
android:height="#dimen/custom_switcher_inner_circle_size" />
<solid
android:color="#android:color/white"/>
</shape>
</item>
</layer-list>
</item>
<item android:state_checked="false">
<layer-list>
<item>
<shape
android:shape="oval"
android:visible="true"
android:dither="true"
android:useLevel="false">
<solid
android:color="#color/colorListEnd"
/>
<size
android:width="#dimen/custom_switcher_circle_size"
android:height="#dimen/custom_switcher_circle_size" />
<stroke
android:width="#dimen/custom_switcher_stroke_thickness"
android:color="#android:color/white"/>
</shape>
</item>
<item
android:left="#dimen/custom_switcher_between_circles_margin"
android:right="#dimen/custom_switcher_between_circles_margin"
android:top="#dimen/custom_switcher_between_circles_margin"
android:bottom="#dimen/custom_switcher_between_circles_margin"
>
<shape
android:shape="oval">
<size
android:width="#dimen/custom_switcher_inner_circle_size"
android:height="#dimen/custom_switcher_inner_circle_size"
/>
<stroke
android:width="#dimen/custom_switcher_stroke_thickness"
android:color="#android:color/white"
/>
</shape>
</item>
</layer-list>
</item>
</selector>
There are several small issues with Switch making it not work uniformly. In your case you need to use android:textOff and android:textOn with value "". Otherwise the width of the track and of the thumbs will be adjusted to hold the text. Also you need to define android:thumbTextPadding to assure the size of the thumb. The drawable is just a background and doesn't effect it directly.
<Switch
android:id="#+id/row_device_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="14dp"
android:switchMinWidth="#dimen/custom_switcher_circle_radius"
android:textOff=""
android:textOn=""
android:thumb="#drawable/customswitchselector"
android:thumbTextPadding="#dimen/switch_thumb_radius"
android:track="#drawable/custom_track" />
The #dimen/custom_switcher_circle_radius should have a value of half of the #dimen/custom_switcher_circle_size dimension.

How Do Create This Type Of CheckBox In Android

How to create this type of checkbox in android when it pressed. I tried . May I know what is the correct way to achieve my objective?Maybe this question too basic, but i did't find any suitable solution.Please Help me out.
I want to create Like this
When it unchecked it will looks like :
Here is my XML code for checkbox:
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:button="#drawable/custom_checkbox"/>
Here is custom_checkbox.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/checked" />
<item android:state_pressed="true"
android:drawable="#drawable/checked" />
<item android:drawable="#drawable/unchecked" />
</selector>
Here is checked.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#54d66A" android:endColor="#54d66A" android:angle="270"/>
<stroke android:width="4px" android:color="#ffc0c0c0" />
<size android:height="20dp" android:width="20dp"/>
</shape>
Here is my output:
You need to create a drawable with layer-list with two items as rectangle shape . one for the background and other as stroke with some margin.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient android:startColor="#54d66A" android:endColor="#54d66A" android:angle="270"/>
<size android:height="20dp" android:width="20dp"/>
</shape>
</item>
<item android:bottom="2dp" android:left="2dp" android:right="2dp" android:top="2dp">
<shape android:shape="rectangle" >
<stroke android:width="2dp" android:color="#ffc0c0c0" />
</shape>
</item>
</layer-list>
And , set this drawable as state_checked background.

Android Selecting a drawable from level-list in XML

How can I select in XML, for an android:drawable property, one of the drawables defined in the level-list XML?
I am defining the drawables in the level-list XML itself, to avoid defining many small XML files.
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="0">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#color/empty"
android:endColor="#android:color/white"
android:angle="90" />
</shape>
</item>
<item android:maxLevel="1">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:startColor="#color/low"
android:endColor="#android:color/white"
android:angle="90" />
</shape>
</item>
<item android:maxLevel="2">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#color/middle"
android:endColor="#android:color/white"
android:angle="90" />
</shape>
</item>
<item android:maxLevel="3">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#color/high"
android:endColor="#android:color/white"
android:angle="90" />
</shape>
</item>
<item android:maxLevel="4">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#color/full"
android:endColor="#android:color/white"
android:angle="90" />
</shape>
</item>
</level-list>
It is not possible. I was just searching with the same idea in mind, expecting something like an attribute for the ImageView. Something like Android:level.
But no, the setLevel method has no equivalent in XML attributes. You (and I) must call setLevel programmatically.
In API 24 (and above) this feature is now supported; android:level found in the <scale> element which Specifies the initial drawable level in the range 0 to 10000 and can be used in <layer-list> as thus;
<?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:gravity="center_vertical|fill_horizontal">
<shape android:shape="rectangle"
android:tint="?attr/colorControlNormal">
<size android:height="4dp" />
<solid android:color="#android:color/white_disabled_material" />
</shape>
</item>
<item android:id="#android:id/secondaryProgress"
android:gravity="center_vertical|fill_horizontal">
<scale android:scaleWidth="100%"
android:level="2000">
<shape android:shape="rectangle"
android:tint="?attr/colorControlActivated" >
<size android:height="4dp" />
<solid android:color="#android:color/black" />
</shape>
</scale>
</item>
<item android:id="#android:id/progress"
android:gravity="center_vertical|fill_horizontal">
<scale android:scaleWidth="100%"
android:level="1000">
<shape android:shape="rectangle"
android:tint="?attr/colorControlActivated" >
<size android:height="4dp" />
<solid android:color="#android:color/white_disabled_material" />
</shape>
</scale>
</item>
</layer-list>
to change the level dynamically you then call setLevel programatically

How to apply shape and selector simultaneously for Button?

I have applied a shape for a button like:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient android:startColor="#DD000000" android:endColor="#DD2d2d2d" android:angle="90"></gradient>
<corners android:radius="15dip"></corners>
</shape>
Now I want to use a selector like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/active"
android:state_pressed="true" />
<item android:drawable="#drawable/passive"/>
for this Button as well. Is it possible ...???
use this way:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>.......</shape>
</item>
..........
..........
</selector>
Detailed to the point answer
Create a color resource in
res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="yellow" type="color">#F7B500</item>
<item name="yellow_dark" type="color">#AC7E00</item>
<integer-array name="androidcolors">
<item>#color/yellow</item>
<item>#color/yellow_dark</item>
</integer-array>
</resources>
Create a drawable at
res/drawable/bg_yellow_round.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/yellow" />
<stroke
android:width="2dp"
android:color="#color/yellow" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>
Create another drawable, you want for transition at same place and name it
res/drawable/bg_yellow_dark_round.xml
<solid android:color="#color/yellow_dark" />
<stroke
android:width="2dp"
android:color="#color/yellow_dark" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
Now create a color state list at
res/color/btn_selector_yellow.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="#android:integer/config_shortAnimTime">
<item android:drawable="#color/yellow" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="#drawable/bg_yellow_dark_round" android:state_pressed="true"/>
<item android:drawable="#drawable/bg_yellow_round"/>
</selector>
Now set it to your button as following
<Button
android:id="#+id/button1"
android:layout_width="248dp"
android:layout_height="44dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginTop="20dp"
android:background="#color/btn_selector_yellow"
android:text="AZ_ is so cool" />
Now this will do transition from
to
.
shape.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/star_off"/>
<corners android:radius="5dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<item android:drawable="#color/tab_focused" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="#color/tab_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/shape"/>
</selector>
You can also create a shape that is using a selector inside. If your shape is just changing its color in different states, this is a lot cleaner.
color/color_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/blue_dark" android:state_pressed="true" />
<item android:color="#color/blue_light" />
</selector>
drawable/shape.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/color_selector" />
<corners android:bottomLeftRadius="6dip" android:bottomRightRadius="6dp" />
<padding android:bottom="0dip" android:left="0dip" android:right="0dip" android:top="0dip" />
</shape>
Well I know it's way too late
But here is a solved example
<TextView
android:id="#+id/txt_out_going_calls"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="04dp"
android:layout_weight="1"
android:background="#drawable/header_text_view_selector"
android:gravity="center"
android:text="#string/outgoing_calls_tab_button_text"
android:textColor="#color/home_text_color" />
and my header_text_view_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/home_fragment_tab_color_selected"/>
<corners android:radius="25dip" />
<padding android:bottom="08dip" android:left="9dip" android:right="9dip" android:top="08dip" />
</shape>
</item>
<item android:state_selected="false">
<shape>
<solid android:color="#color/home_fragment_tab_color_simple"/>
<corners android:radius="25dip" />
<padding android:bottom="08dip" android:left="9dip" android:right="9dip" android:top="08dip" />
</shape>
</item>
</selector>
So basically i m creating a rounded textview with selector. Here i m handling only state_selected and not_selected. Hope it helps
This is my way, and it works!
<item android:state_pressed="true">
<shape android:shape="oval">
<gradient android:centerX=".6"
android:centerY=".40"
android:endColor="#color/colorPrimary"
android:gradientRadius="20"
android:startColor="#color/colorPrimary"
android:type="radial" />
<stroke android:width="1dp"
android:color="#FFFFFF" />
<size android:width="55dp"
android:height="55dp" />
</shape>
</item>
<item android:state_focused="false">
<shape android:shape="oval">
<gradient android:centerX=".6"
android:centerY=".40"
android:endColor="#android:color/transparent"
android:gradientRadius="20"
android:startColor="#android:color/transparent"
android:type="radial" />
<stroke android:width="1dp"
android:color="#FFFFFF" />
<size android:width="55dp"
android:height="55dp" />
</shape>
</item>
My example is a circle button with state_pressed.
code bellow:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="#color/light_primary_color" />
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#color/accent_color" />
</shape>
</item>
</selector>
Use your shape name as you use any image and use it selector as you use image. Try out you will not face any problem. Is that what you were asking?
To be more reusable, you can set states on single properties. Avoid duplicating your shapes
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item>
<shape android:shape="rectangle" >
<corners android:radius="5dp"/>
<solid
android:state_enabled="false"
android:color="#color/transparent"
/>
<solid
android:state_enabled="true"
android:color="#color/background"
/>
<stroke
android:width="#dimen/dividerHeight"
android:color="#color/dividerLight"
/>
</shape>
</item>
</selector>
I was able to set background programmatically once disabled using this method.

Categories

Resources