I am implementing a recycleview to display list of data. I want to highlight selected item on recycleview. It is working while clickable,focusable and focusableInTouchMode are true. But in that case item view click listner not working. Please help me on this.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
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:id="#+id/cardlist_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginEnd="3dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_marginStart="3dp"
android:layout_marginTop="1dp"
android:foreground="?android:attr/selectableItemBackground"
app:cardBackgroundColor="#color/bg_land"
app:cardCornerRadius="0dp"
app:cardElevation="#dimen/cardview_default_elevation">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/recycle_item_selector"
android:orientation="vertical">
//some stuff here
</LinearLayout>
</android.support.v7.widget.CardView>
And this is my selector xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="#color/colorAccent" />
</shape>
</item>
<item android:state_checked="true">
<shape>
<solid android:color="#color/colorAccent" />
</shape>
</item>
<item android:state_focused="true">
<shape>
<solid android:color="#color/colorAccent" />
</shape>
</item>
<item android:state_activated="true">
<shape>
<solid android:color="#color/colorAccent" />
</shape>
</item>
<item>
<shape>
<solid android:color="#color/bg_land" />
</shape>
</item>
</selector>
Related
How can i achieve this type of vertical progress bar?The progress bar's progress also has round corner
I tried to find solution but can not find any.I tried to set a custom drawable as follow`
<?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>
<gradient
android:angle="180"
android:centerColor="#ffffffff"
android:centerY="0.75"
android:endColor="#ffffffff"
android:startColor="#ffffffff" />
<corners android:radius="#dimen/xxs" />
</shape>
</item>
<item android:id="#android:id/progress">
<clip
android:clipOrientation="vertical"
android:gravity="bottom">
<shape>
<gradient
android:centerColor="#E9E9E9"
android:centerY="0.75"
android:endColor="#E9E9E9"
android:startColor="#E9E9E9" />
<corners android:radius="#dimen/xxs" />
</shape>
</clip>
</item>
</layer-list>
I think you need to go with the some chart library that would be great solution but if you want to go with customisation your chart then go with below code.
veritcal_progressbar.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>
<corners android:radius="5dip"/>
<solid android:color="#FFFFFF"/>
<stroke
android:width="1dp"
android:color="#FF000000" />
</shape>
</item>
<item android:id="#android:id/progress">
<clip
android:clipOrientation="vertical"
android:gravity="bottom">
<shape>
<corners android:radius="5dip"/>
<solid android:color="#FFBE00"/>
<stroke
android:width="1dp"
android:color="#FF000000" />
</shape>
</clip>
</item>
</layer-list>
Use in layout file.
<ProgressBar
android:id="#+id/vprogressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:progressDrawable="#drawable/veritcal_progressbar"/>
You can achieve this using below code
<RelativeLayout 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:gravity="center"
tools:context=".MainActivity">
<com.google.android.material.slider.Slider
android:id="#+id/slider"
android:layout_width="300dp"
android:layout_height="50dp"
android:valueFrom="0.0"
android:valueTo="100.0"
android:value="60.0"
android:rotation="270"
android:scaleY="15"
app:thumbRadius="0dp"
app:haloRadius="0dp"
android:layout_centerInParent="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#color/white"
android:textSize="16dp"
android:text="60%"/>
</RelativeLayout>
Output
How to generate views as shown below,
I tried to adding stroke to cardView(Material cardView) and using it in a recyclerView. I have used multiple views, below is the xml code for the outer ring
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="6dp"
android:layout_marginEnd="6dp"
android:gravity="center">
<com.google.android.material.circularreveal.cardview.CircularRevealCardView
android:id="#+id/selectedCardColor"
android:layout_width="40dp"
android:layout_height="40dp"
android:visibility="visible"
app:cardBackgroundColor="#color/app_color_white"
app:strokeColor="#color/app_color_green"
app:strokeWidth="1dp"
app:cardCornerRadius="28dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="false">
<com.google.android.material.circularreveal.cardview.CircularRevealCardView
android:id="#+id/selectedCardInnerColor"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center"
android:layout_margin="6dp"
app:cardBackgroundColor="#color/app_color_green"
app:cardCornerRadius="28dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="false" />
</com.google.android.material.circularreveal.cardview.CircularRevealCardView>
</LinearLayout>
and the code for circle shape,
<?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"
android:id="#+id/parent"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="6dp"
android:layout_marginEnd="6dp">
<com.google.android.material.card.MaterialCardView
android:id="#+id/unSelectedCardColor"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
app:cardBackgroundColor="#color/app_color_green"
app:cardCornerRadius="28dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="false" />
</LinearLayout>
It works only in pie(android 9) and above. Other devices it appears as below,
The ring should appear on selected views. I should be able to change color programmatically. Thanks.
Try if it works .....Make a ring.xml in your Drawable folder
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="20dp" android:left="20dp" android:bottom="20dp" android:right="20dp" >
<shape >
<solid android:color= "#FF0000"/>
<corners android:radius="300dp"/>
</shape>
</item>
<item android:left="40dp" android:bottom="40dp" android:top="40dp" android:right="40dp" >
<shape >
<solid android:color= "#ffffff"/>
<corners android:radius="300dp"/>
</shape>
</item>
<item android:left="60dp" android:bottom="60dp" android:right="60dp" android:top="60dp" >
<shape >
<gradient
android:startColor="#00ffff"
android:endColor="#0000ff"
android:angle="90"/>
<corners android:radius="300dp"/>
<stroke android:width="1dp"/>
</shape>
</item>
</layer-list>
please replace the white color with the color of YOUR BACKGRAOUND
use it wherever you like.eg as the background of your button
You may change the color of the ring in the first item and change the color of circle shape in the last item.
Please Try Below code
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="5dp"
android:left="5dp"
android:bottom="5dp"
android:right="5dp" >
<shape >
<solid android:color= "#090909"/>
<corners android:radius="400dp"/>
</shape>
</item>
<item android:left="10dp"
android:bottom="10dp"
android:top="10dp"
android:right="10dp">
<shape >
<solid android:color= "#ffffff"/>
<corners android:radius="300dp"/>
</shape>
</item>
<item
android:left="20dp"
android:bottom="20dp"
android:right="20dp"
android:top="20dp" >
<shape
android:shape="oval">
<solid
android:color="#9C27B0"/>
<size
android:width="200dp"
android:height="200dp"/>
</shape>
</item>
Trying to do something really simple here and have looked up a bunch of SO resource, but to no avail. I am trying to get the ripple effect on a button which uses a drawable background of type of shape drawable. The relevant files:
background_button:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1px"
android:color="#80ffffff" />
<solid android:color="#android:color/transparent" />
</shape>
ripple.xml within the drawable-v21 folder:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item android:drawable="#drawable/background_button"/>
</ripple>
Button.xml:
<Button
android:id="#+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:background="#drawable/ripple"
/>
What is wrong here.
Thanks!
My advice would be not to use <ripple tag. I have 2 reasons: it is not that easy to do exactly what you want and you have to 2 different .xml files for every background. I don't like to have drawable-v21 folder.
My solution is to wrap your Button with FrameLayout and use android:foreground attribute. This way you can have whatever background you want and you can keep the ripple effect.
<FrameLayout
android:id="#+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:foreground="?attr/selectableItemBackground">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background_button"/>
</FrameLayout>
Notice that I moved all the attributes and the id to FrameLayout. You should set onClickListener to FrameLayout instead of the Button.
Btw, ?attr/selectableItemBackground is great. Use it as much as possible. It replaces blue Holo color with gray for old devices from Android 4.0 to 4.3.
Try this.
ripple_effect.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#2797e0"
tools:targetApi="lollipop">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#2797e0" />
</shape>
</item>
button.xml
<Button
android:id="#+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:background="#drawable/ripple_effect"/>
build.gradle
compile 'com.android.support:appcompat-v7:23.1.1'
More simple solution - to use this drawable-xml as value of property "android:background" of the Button:
drawable/bkg_ripple.xml:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item android:drawable="?attr/colorPrimary"/>
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
</ripple>
button.xml:
<Button
android:id="#+id/mybutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
android:background="#drawable/bkg_ripple"
/>
I had the similar task. Let me explain how i solve it. It outputs circle imageViews with shadow and ripple effect not goes beyond circle boundaries.
<ImageView
android:id="#+id/iv_undo"
android:layout_width="#dimen/iv_width_altmenu"
android:layout_height="#dimen/iv_height_altmenu"
android:src="#drawable/undo"
android:elevation="#dimen/elevation_buttons"
android:foreground="#drawable/ripple_effect"
android:background="#drawable/buttons_inactive_coloring"
/>
The following provides ripple effect:
android:foreground="#drawable/ripple_effect"
The following provides circle imageView.
android:background="#drawable/buttons_inactive_coloring"
The following provides shadow:
android:foreground="#drawable/ripple_effect"
Another problem you may be probably face is ripple effect goes out of circle boundaries. In order to solve, use following file(ripple_effect.xml)
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#color/button_active"
tools:targetApi="lollipop">
<item android:id="#android:id/mask">
<shape android:shape="oval">
<solid android:color="#color/button_active" />
</shape>
</item>
</ripple>
If you do not use the following :
<item android:id="#android:id/mask">
<shape android:shape="oval">
<solid android:color="#color/button_active" />
</shape>
</item>
ripple effect goes beyond circle boundaries and this does not looks nice. Be careful and takes it easy.
That works for me
drawable/rect.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/listview_background_shape">
<stroke android:width="1dp" android:color="#FF4C71F5" />
<padding android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
<corners android:radius="5dp" />
<solid android:color="#00000000" />
</shape>
and for ripple drawable/rip.xml:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#f816a463"
tools:targetApi="lollipop">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#f816a463" />
</shape>
</item>
</ripple>
and for your button
<Button
android:id="#+id/nm"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#drawable/rect"
android:enabled="true"
android:text="#string/vnm"
android:textColor="#color/colorpr"
android:textStyle="bold"
android:foreground="#drawable/rip"/>
Hope this helps!
This work for me..
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#color/smooth_white"
tools:targetApi="lollipop">
<item android:drawable="#drawable/button_green_background" />
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#color/smooth_white" />
</shape>
</item>
</ripple>
The point is section
<item android:drawable="#drawable/button_green_background" />
Try this:
card_back.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="20dp" />
<solid android:color="?attr/cardBGColor" />
<stroke android:width="1dp" android:color="?attr/borderColor"/>
</shape>
</item>
</selector>
card_ripple.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#bbb">
<item android:drawable="#drawable/card_back" />
</ripple>
And setup it this way: android:background="#drawable/card_ripple"
Quick solution
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#color/blue"
tools:targetApi="lollipop">
<item
android:id="#android:id/mask"
tools:targetApi="lollipop">
<shape android:shape="rectangle">
<solid android:color="#color/blue" />
<corners android:radius="5dp" />
</shape>
</item>
<item android:id="#android:id/background">
<shape android:shape="rectangle">
<stroke
android:width="0.1dp"
android:color="#312D2D" />
<solid android:color="#FFF" />
<corners android:radius="50dp" />
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
</item>
</ripple>
This will help
<com.abcd.ripplebutton.widget.RippleButton
android:id="#+id/Summit"
android:layout_width="260dp"
android:layout_height="50dp"
android:text="Login"
android:textColor="#color/white"
android:textStyle="bold"
app:buttonColor="#color/Button"
app:rippleColor="#color/white" />
For more See this
For me only this work
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#cccccc">
<item android:id="#android:id/background">
<shape android:shape="rectangle">
<solid android:color="#android:color/white" />
</shape>
</item>
</ripple>
And in layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="#drawable/ripple_white"/>
Yes, it should be clickable
I am looking to Custom The Switch Button to becoming as following :
How to achieve this ?
However, I might not be taking the best approach, but this is how I have created some Switch like UIs in few of my apps.
Here is the code -
<RadioGroup
android:checkedButton="#+id/offer"
android:id="#+id/toggle"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="#dimen/margin_medium"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="#dimen/margin_medium"
android:background="#drawable/pink_out_line"
android:orientation="horizontal">
<RadioButton
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:id="#+id/search"
android:background="#drawable/toggle_widget_background"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="#null"
android:gravity="center"
android:text="Search"
android:textColor="#color/white" />
<RadioButton
android:layout_marginRight="1dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:id="#+id/offer"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/toggle_widget_background"
android:button="#null"
android:gravity="center"
android:text="Offers"
android:textColor="#color/white" />
</RadioGroup>
pink_out_line.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="2dp" />
<solid android:color="#80000000" />
<stroke
android:width="1dp"
android:color="#color/pink" />
</shape>
toggle_widget_background.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/pink" android:state_checked="true" />
<item android:drawable="#color/dark_pink" android:state_pressed="true" />
<item android:drawable="#color/transparent" />
</selector>
And here is the output -
Its a simple xml design. It looks like iOS switch, check this below image
You need to create custom_thumb.xml and custom_track.xml
This is my switch,I need a very big switch so added layout_width/layout_height parameter
<androidx.appcompat.widget.SwitchCompat
android:id="#+id/swOnOff"
android:layout_width="#dimen/_200sdp"
android:layout_marginStart="#dimen/_50sdp"
android:layout_marginEnd="#dimen/_50sdp"
android:layout_marginTop="#dimen/_30sdp"
android:layout_gravity="center"
app:showText="true"
android:textSize="#dimen/_20ssp"
android:fontFamily="#font/opensans_bold"
app:track="#drawable/custom_track"
android:thumb="#drawable/custom_thumb"
android:layout_height="#dimen/_120sdp"/>
Now create custom_thumb.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:shape="oval">
<solid android:color="#ffffff"/>
<size android:width="#dimen/_100sdp"
android:height="#dimen/_100sdp"/>
<stroke android:width="1dp"
android:color="#8c8c8c"/>
</shape>
</item>
<item android:state_checked="true">
<shape android:shape="oval">
<solid android:color="#ffffff"/>
<size android:width="#dimen/_100sdp"
android:height="#dimen/_100sdp"/>
<stroke android:width="1dp"
android:color="#34c759"/>
</shape>
</item>
</selector>
Now create custom_track.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:shape="rectangle">
<corners android:radius="#dimen/_100sdp" />
<solid android:color="#ffffff" />
<stroke android:color="#8c8c8c" android:width="1dp"/>
<size android:height="20dp" />
</shape>
</item>
<item android:state_checked="true">
<shape android:shape="rectangle">
<corners android:radius="#dimen/_100sdp" />
<solid android:color="#34c759" />
<stroke android:color="#8c8c8c" android:width="1dp"/>
<size android:height="20dp" />
</shape>
</item>
</selector>
you can use the following code to change color and text :
<org.jraf.android.backport.switchwidget.Switch
android:id="#+id/th"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:thumb="#drawable/apptheme_switch_inner_holo_light"
app:track="#drawable/apptheme_switch_track_holo_light"
app:textOn="#string/switch_yes"
app:textOff="#string/switch_no"
android:textColor="#000000"
/>
Create a xml named colors.xml in res/values folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#ff0000</color>
<color name="green">#00ff00</color>
</resources>
In drawable folder, create a xml file my_btn_toggle.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#color/red" />
<item android:state_checked="true" android:drawable="#color/green" />
</selector>
and in xml section defining your toggle button add:
android:background="#drawable/my_btn_toggle
to change the color of textOn and textOffuse
android:switchTextAppearance="#style/Switch"
<Switch android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="#drawable/custom_switch_inner_holo_light"
android:track="#drawable/custom_switch_track_holo_light"
android:textOn="#string/yes"
android:textOff="#string/no"/>
drawable/custom_switch_inner_holo_light.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#drawable/custom_switch_thumb_disabled_holo_light" />
<item android:state_pressed="true" android:drawable="#drawable/custom_switch_thumb_pressed_holo_light" />
<item android:state_checked="true" android:drawable="#drawable/custom_switch_thumb_activated_holo_light" />
<item android:drawable="#drawable/custom_switch_thumb_holo_light" />
</selector>
drawable/custom_switch_track_holo_light.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#drawable/custom_switch_bg_focused_holo_light" />
<item android:drawable="#drawable/custom_switch_bg_holo_light" />
</selector>
Next images are 9.paths drawables and they must be in different density (mdpi, hdpi, xhdpi, xxhdpi). As example I give xxhdpi (you can resize they if u needed):
drawable/custom_switch_thumb_disabled_holo_light
drawable/custom_switch_thumb_pressed_holo_light
drawable/custom_switch_thumb_activated_holo_light
drawable/custom_switch_thumb_holo_light
drawable/custom_switch_bg_focused_holo_light
drawable/custom_switch_bg_holo_light
I achieved this
by doing:
1) custom selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_switch_off"
android:state_checked="false"/>
<item android:drawable="#drawable/ic_switch_on"
android:state_checked="true"/>
</selector>
2) using v7 SwitchCompat
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:button="#drawable/checkbox_yura"
android:thumb="#null"
app:track="#null"/>
I use this approach to create a custom switch using a RadioGroup and RadioButton;
Preview
Color Resource
<color name="blue">#FF005a9c</color>
<color name="lightBlue">#ff6691c4</color>
<color name="lighterBlue">#ffcdd8ec</color>
<color name="controlBackground">#ffffffff</color>
control_switch_color_selector (in res/color folder)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:color="#color/controlBackground"
/>
<item
android:state_pressed="true"
android:color="#color/controlBackground"
/>
<item
android:color="#color/blue"
/>
</selector>
Drawables
control_switch_background_border.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp" />
<solid android:color="#android:color/transparent" />
<stroke
android:width="3dp"
android:color="#color/blue" />
</shape>
control_switch_background_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape>
<solid android:color="#color/blue"></solid>
</shape>
</item>
<item android:state_pressed="true">
<shape>
<solid android:color="#color/lighterBlue"></solid>
</shape>
</item>
<item>
<shape>
<solid android:color="#android:color/transparent"></solid>
</shape>
</item>
</selector>
control_switch_background_selector_middle.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape>
<solid android:color="#color/blue"></solid>
</shape>
</item>
<item android:state_pressed="true">
<shape>
<solid android:color="#color/lighterBlue"></solid>
</shape>
</item>
<item>
<layer-list>
<item android:top="-1dp" android:bottom="-1dp" android:left="-1dp">
<shape>
<solid android:color="#android:color/transparent"></solid>
<stroke android:width="1dp" android:color="#color/blue"></stroke>
</shape>
</item>
</layer-list>
</item>
</selector>
Layout
<RadioGroup
android:checkedButton="#+id/calm"
android:id="#+id/toggle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="24dp"
android:layout_marginTop="24dp"
android:background="#drawable/control_switch_background_border"
android:orientation="horizontal">
<RadioButton
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="3dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:id="#+id/calm"
android:background="#drawable/control_switch_background_selector_middle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="#null"
android:gravity="center"
android:text="Calm"
android:fontFamily="sans-serif-medium"
android:textColor="#color/control_switch_color_selector"/>
<RadioButton
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:id="#+id/rumor"
android:background="#drawable/control_switch_background_selector_middle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="#null"
android:gravity="center"
android:text="Rumor"
android:fontFamily="sans-serif-medium"
android:textColor="#color/control_switch_color_selector"/>
<RadioButton
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_marginRight="3dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:id="#+id/outbreak"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/control_switch_background_selector"
android:button="#null"
android:gravity="center"
android:text="Outbreak"
android:fontFamily="sans-serif-medium"
android:textColor="#color/control_switch_color_selector" />
</RadioGroup>
Use the code below to create a custom switch button like the one shown below.
<androidx.appcompat.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/customSwitch"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:checked="false"
app:track="#drawable/track"
android:thumb="#drawable/thumb"
android:text="" />
#drawable/track
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false">
<shape android:shape="rectangle">
<solid android:color="#FF3333"/>
<corners android:radius="100sp"/>
<stroke android:color="#8e8e8e"
android:width="1dp"/>
</shape>
</item>
<item android:state_checked="true">
<shape android:shape="rectangle">
<solid android:color="#color/color_green"/> <!--color name="color_green">#3bd391</color-->
<corners android:radius="100sp"/>
</shape>
</item>
</selector>
#drawable/thumb
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="#drawable/switch_thumb_false"/>
<item android:state_checked="true"
android:drawable="#drawable/switch_thumb_true"/>
</selector>
#drawable/switch_thumb_false
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="4dp"
android:top="4dp"
android:left="4dp"
android:right="4dp">
<shape android:shape="oval">
<solid android:color="#FFFFFF"/>
<size android:height="3dp"
android:width="3dp"/>
</shape>
</item>
<item android:drawable="#drawable/ic_baseline_close_16"
android:bottom="8dp"
android:top="8dp"
android:left="8dp"
android:right="8dp"/>
</layer-list>
#drawable/switch_thumb_true
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="4dp"
android:top="4dp"
android:left="4dp"
android:right="4dp">
<shape android:shape="oval">
<solid android:color="#FFFFFF"/>
<size android:height="3dp"
android:width="3dp"/>
<stroke android:width="1sp"
android:color="#8e8e8e" />
</shape>
</item>
<item android:drawable="#drawable/ic_baseline_correct_16"
android:bottom="8dp"
android:top="8dp"
android:left="8dp"
android:right="8dp"
/>
</layer-list>
#drawable/ic_baseline_correct_16
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="16dp"
android:height="16dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="#008F28"
android:alpha="0.9">
<path
android:fillColor="#FF000000"
android:pathData="M9,16.2L4.8,12l-1.4,1.4L9,19 21,7l-1.4,-1.4L9,16.2z"/>
</vector>
#drawable/ic_baseline_close_16
<vector
android:height="16dp"
android:tint="#FF0000"
android:viewportHeight="24"
android:viewportWidth="24"
android:width="16dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#android:color/white" android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>
</vector>
You can use Android Material Components.
build.gradle:
implementation 'com.google.android.material:material:1.0.0'
layout.xml:
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="#+id/toggleGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:checkedButton="#id/btn_one_way"
app:singleSelection="true">
<Button
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:id="#+id/btn_one_way"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="One way trip" />
<Button
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:id="#+id/btn_round"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Round trip" />
</com.google.android.material.button.MaterialButtonToggleGroup>
You can use the regular Switch widget and just call setTextOn() and setTextOff(), or use the android:textOn and android:textOff attributes.
Example 1:
custom_thumb.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">
<corners android:radius="20dp" />
<solid android:color="#color/white"/>
<size android:width="37dp" android:height="37dp" />
<stroke android:width="4dp" android:color="#color/white" />
</shape>
</item>
<item android:state_checked="false">
<shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
<corners android:radius="20dp" />
<solid android:color="#color/white"/>
<size android:width="37dp" android:height="37dp" />
<stroke android:width="4dp" android:color="#0000ffff" />
</shape>
</item>
</selector>
custom_track.xml
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
android:visible="true"
android:dither="true"
android:useLevel="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/black"/>
<corners
android:radius="20dp"/>
<size
android:width="50dp"
android:height="26dp" />
<stroke android:color="#color/white"
android:width="4dp"/>
</shape>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#color/black"
android:gravity="center">
<Switch
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:thumb="#drawable/custom_thumb"
android:track="#drawable/custom_track"
tools:ignore="UseSwitchCompatOrMaterialXml" />
</LinearLayout>
</LinearLayout>
Example 2:
custom_thumb.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">
<corners android:radius="20dp" />
<solid android:color="#color/black"/>
<size android:width="37dp" android:height="37dp" />
<stroke android:width="4dp" android:color="#color/white" />
</shape>
</item>
<item android:state_checked="false">
<shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
<corners android:radius="20dp" />
<solid android:color="#color/black"/>
<size android:width="37dp" android:height="37dp" />
<stroke android:width="4dp" android:color="#color/white" />
</shape>
</item>
</selector>
custom_track.xml
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
android:visible="true"
android:dither="true"
android:useLevel="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/black"/>
<corners
android:radius="20dp"/>
<size
android:width="50dp"
android:height="26dp" />
<stroke android:color="#color/white"
android:width="4dp"/>
</shape>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#color/black"
android:gravity="center">
<Switch
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:thumb="#drawable/custom_thumb"
android:track="#drawable/custom_track"
tools:ignore="UseSwitchCompatOrMaterialXml" />
</LinearLayout>
</LinearLayout>
Example 3:
custom_thumb.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">
<corners android:radius="20dp" />
<solid android:color="#color/white" />
<size android:width="37dp" android:height="37dp" />
<stroke android:width="4dp" android:color="#color/black" />
</shape>
</item>
<item android:state_checked="false">
<shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
<corners android:radius="20dp" />
<solid android:color="#color/white" />
<size android:width="37dp" android:height="37dp" />
<stroke android:width="4dp" android:color="#cdcdcd" />
</shape>
</item>
</selector>
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">
<shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
<solid android:color="#color/black" />
<corners android:radius="20dp" />
<size android:width="50dp" android:height="26dp" />
</shape>
</item>
<item android:state_checked="false">
<shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
<solid android:color="#cdcdcd" />
<corners android:radius="20dp" />
<size android:width="50dp" android:height="26dp" />
</shape>
</item>
</selector>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#EDEDED"
android:orientation="vertical"
tools:context=".MainActivity">
<Switch
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:thumb="#drawable/custom_thumb"
android:track="#drawable/custom_track"
tools:ignore="UseSwitchCompatOrMaterialXml" />
</LinearLayout>
With the Material Components Library you can use the MaterialButtonToggleGroup:
<com.google.android.material.button.MaterialButtonToggleGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:checkedButton="#id/b1"
app:selectionRequired="true"
app:singleSelection="true">
<Button
style="?attr/materialButtonOutlinedStyle"
android:id="#+id/b1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="OPT1" />
<Button
style="?attr/materialButtonOutlinedStyle"
android:id="#+id/b2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="OPT2" />
</com.google.android.material.button.MaterialButtonToggleGroup>
More info on this link: http://www.mokasocial.com/2011/07/sexily-styled-toggle-buttons-for-android/
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/toggle_me"/>
and the drawable will be something like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/toggle_me_on" /> <!-- checked -->
<item android:drawable="#drawable/toggle_me_off" /> <!-- default/unchecked -->
</selector>
There are two ways to create custom ToggleButton
1) By defining custom background
2) By creating custom button
Check http://www.zoftino.com/android-toggle-button for custom styles
Toggle button with custom background
Define drawable as xml resource like below and set it as background of toggle button. In the below example, drawable toggle_color is a color selector, you need to define this also.
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="4dp"
android:insetTop="4dp"
android:insetRight="4dp"
android:insetBottom="4dp">
<layer-list android:paddingMode="stack">
<item>
<ripple android:color="?attr/android:colorControlHighlight">
<item>
<shape android:shape="rectangle"
android:tint="?attr/android:colorButtonNormal">
<corners android:radius="8dp"/>
<solid android:color="#android:color/white" />
<padding android:left="8dp"
android:top="6dp"
android:right="8dp"
android:bottom="6dp" />
</shape>
</item>
</ripple>
</item>
<item android:gravity="left|fill_vertical">
<shape android:shape="rectangle">
<corners android:radius="4dp"/>
<size android:width="8dp" />
<solid android:color="#color/toggle_color" />
</shape>
</item>
<item android:gravity="right|fill_vertical">
<shape android:shape="rectangle">
<corners android:radius="4dp"/>
<size android:width="8dp" />
<solid android:color="#color/toggle_color" />
</shape>
</item>
</layer-list>
</inset>
Toggle button with custom button
Create your own images for two state of toggle button (make sure images exist for all sizes of screens) and place them in drawable folder, create selector and set it as button.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/toggle_on" />
<item android:drawable="#drawable/toggle_off" />
</selector>
switch
<androidx.appcompat.widget.SwitchCompat
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
app:track="#drawable/track"
android:thumb="#drawable/thumb"
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="#drawable/switch_thumb_false"/>
<item android:state_checked="true"
android:drawable="#drawable/switch_thumb_true"/>
</selector>
track.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:shape="rectangle">
<size android:width="24dp" android:height="12dp" />
<solid android:color="#EFE0BB" />
<corners android:radius="6dp" />
</shape>
</item>
<item android:state_checked="true">
<shape android:shape="rectangle">
<size android:width="24dp" android:height="12dp" />
<solid android:color="#color/colorPrimary" />
<corners android:radius="6dp" />
</shape>
</item>
</selector>
switch_thumb_true.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#EFE0BB" />
<size
android:width="10dp"
android:height="10dp" />
<stroke
android:width="2dp"
android:color="#color/colorPrimary" />
</shape>
</item>
</layer-list>
switch_thumb_false.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape android:shape="oval">
<solid android:color="#color/colorPrimary"/>
<size android:height="12dp"
android:width="12dp"/>
<stroke android:color="#EFE0BB"
android:width="2dp"/>
</shape>
</item>
</layer-list>
<Switch
android:thumb="#drawable/thumb"
android:track="#drawable/track"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
I have a layout with a GridView.
Each item in the Gridview is a LinearLayout that implements the Checkable interface.
All works OK. But it only show correctly in SDK 4.2.2
SDK 4.2.2
SDK 2.1
The black background should not be shown.
GridView code
<GridView
android:id="#+id/gvBirds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:verticalSpacing="4dp"
android:layout_margin="32dp"
android:scrollbars="vertical"
android:numColumns="auto_fit"
android:listSelector="#color/transparent">
</GridView>
GridView item code
<?xml version="1.0" encoding="utf-8"?>
<pt.rogerioamaral.ByMAP.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridItemLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:background="#drawable/grid_item_selector">
<TextView
android:id="#+id/lbBirdName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="nome grande"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/main_gradient_end"
android:drawableTop="#drawable/falcao_small"
android:background="#drawable/main_grid_background"
android:layout_margin="5dp"/>
</pt.rogerioamaral.ByMAP.CheckableLinearLayout>
Selector code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:state_checked="true">
<shape android:shape="rectangle">
<solid android:color="#color/main_gradient_end"/>
<corners android:radius="20dp"/>
</shape>
</item>
<item android:state_checked="true">
<shape android:shape="rectangle">
<stroke android:color="#color/main_gradient_end"
android:width="3dp"/>
<corners android:radius="20dp"/>
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#color/main_gradient_end"/>
<corners android:radius="20dp"/>
</shape>
</item>
<item android:drawable="#color/transparent"/>
</selector>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="main_gradient_start">#454444</color>
<color name="main_gradient_end">#BAB8B9</color>
<color name="transparent">#00000000</color>
</resources>
What I have to do to have the same behavior in both situations?
In your GridView XML android:listSelector(int) is actually the selected item. It doesn't error because your #color/transparent is (int)0x00000000.
I'm pretty sure the difference in the background is due to a change in the default background color between the SDKs.
Hopefully these two changes will fix it. (note the first is just for clarity)
<GridView
android:id="#+id/gvBirds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:verticalSpacing="4dp"
android:layout_margin="32dp"
android:scrollbars="vertical"
android:numColumns="auto_fit"
android:listSelector="0"> <!-- selects the first item in the list, changed for clarity -->
</GridView>
<item android:state_checked="true">
<shape android:shape="rectangle">
<stroke android:color="#color/main_gradient_end"
android:background="#color/transparent"
android:width="3dp"/> <!-- added to set background color to transparent -->
<corners android:radius="20dp"/>
</shape>
</item>
Need to add <solid> atributte with color="#color/transparent" on state_checked="true"
<item android:state_checked="true">
<shape android:shape="rectangle">
<solid android:color="#color/transparent"/>
<stroke android:color="#color/main_gradient_end"
android:width="3dp"/>
<corners android:radius="20dp"/>
</shape>
</item>