I've been trying to customize the toggle button look but with no success.
Here is how I want it to look like:
Can someone give me a tutorial?
create toggle_selector.xml in res/drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/toggle_on" android:state_checked="true"/>
<item android:drawable="#drawable/toggle_off" android:state_checked="false"/>
</selector>
apply the selector to your toggle button
<ToggleButton
android:id="#+id/chkState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/toggle_selector"
android:textOff=""
android:textOn=""/>
Note: for removing the text i used following in above code
textOff=""
textOn=""
I don't know if this is the best solution but it worked fine for me:
1.- Decide how big do you want the toggle button. In my case width 56dp and height 76dp.
2.- Create the icon set 56px-76px for mdpi, 84px-113px hdpi, same for xhdpi and xxhdpi
3.- Move the icons in the corresponding drawable folder. In my case 20 icons 5 in each folder, named ic_name1_on, ic_name1_off [...] ic_name5_off
4.- Create the following xml files in a new folder called drawable (if it does not exist yet):
ic_name1_toggle.xml
ic_name1_toggle_bg.xml
ic_name2_toggle.xml
(...)
ic_name5_toggle_bg.xml
5.- In ic_name1_toggle.xml the code must be:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="false"
android:drawable="#drawable/ic_name1_off" />
<item
android:state_checked="true"
android:drawable="#drawable/ic_name1_on" />
</selector>
6.- In ic_name1_toggle_bg.xml the code must be:
<?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="#android:color/transparent" />
<item android:id="#+android:id/toggle"
android:drawable="#drawable/ic_name1_toggle" />
</layer-list>
7.- Finally in your layout.xml:
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="56dp"
android:layout_height="76dp"
android:background="#android:color/transparent"
android:button="#drawable/ic_name1_toggle_bg"
android:textOff=""
android:textOn="" />
I think you need to define a custom background for your button.
Take a look at the Developer Guide on customizing a Button's background.
However, in step Three, Create a new XML file in the res/drawable/ directory Use this Xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_da"
android:state_checked="true" />
<item android:drawable="#drawable/button_nu" />
</selector>
The element android:state_checked="true" is what defines that state as the checked background.
Let me know if this works for you.
Create selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_da" android:state_checked="true"/>
<item android:drawable="#drawable/btn_nu"/>
</selector>
and use it as background for your ToggleButton.
Related
I have tried to make a xml selector with the following:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/shuffleon" android:state_checked="true" />
<item android:drawable="#drawable/shuffleoff" android:state_selected="false" />
</selector>
and when I try to set the backgroundDrawable to the checkBox the checkbox doesn't replace the CheckBox style too:
shuffle.setBackground(android.support.v4.content.res.ResourcesCompat.getDrawable(getResources(), R.drawable.shuffle, null));
Following this question: Change icons of checked and unchecked for Checkbox for Android I need to set the button drawable with my xml: android:button="#drawable/checkbox" but I can't do this because I'm creating the CheckBox programmatically.
Is there a way how to achieve this?
Simple way
Create a new layout in drawable folder and name it custom_checkbox (You can rename also)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/Checked_image_name"android:state_checked="true"/>
<item android:drawable="#drawable/Unchecked_image_name" android:state_checked="false"/>
</selector>
Use this in your layout activity
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/custom_checkbox"/>
Use below line of code, i think it will work
yourcheckbox.setButtonDrawable(yourdrawable);
If you use sdk 28 (androidx) and above try to use
androidx.appcompat.widget.AppCompatCheckBox
instead of
Checkbox
Use This
shuffle.setButtonDrawable(R.drawable.custom_checkbox_selector);
XML code;
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/radiobutton_checked" android:state_checked="true"/>
<item android:drawable="#drawable/radiobutton_unchecked" android:state_checked="false"/>
</selector>
This works for me:
<android.support.v7.widget.AppCompatCheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/custom_checkbox"
android:button="#null"/>
drawable/checkbox_custome.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/checkbox_empty" />
<item android:state_checked="true"
android:drawable="#drawable/checkbox_check" />
</selector>
I am trying to customize a Button UI in Android.
I tried the following things:
btn.setBackgroundColor
btn.setBackgroundResource
btn.setBackgroundColor
But all of these are increasing the size of the Button, and because of that the Buttons near by can not be segregated (??).
Please suggest something.
If you want to apply a Hover effect then you have to do this in your XML where button layout is like
Button
android:id="#+id/xyz"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="15dp"
android:background="#drawable/general_btn_hover_effect"
android:onClick="somefunction"
android:text="#string/search_number"
android:textColor="#ffffff"
android:textSize="20sp"
/>
Notice android:background="#drawable/general_btn_hover_effect" there and then in #drawable folder make a general_btn_hover_effect.xml and write this into it
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/blank_normal_bg" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="#drawable/blank_hover_bg" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="#drawable/blank_hover_bg" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="#drawable/blank_normal_bg"/>
</selector>
Changing background color can not change size of button. Maybe you changed size in xml layout. Check again
Im trying to create custom button and i cant see my problem.
i create in res/drawable/ custom.xml
<?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/forwardpress"></item>
<item android:state_focused="true" android:drawable="#drawable/forwardhover"></item>
<item android:drawable="#drawable/forward"></item>
</selector>
and on main.xml i have this button
<Button
android:id="#+id/bFor"
android:background="#drawable/custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right" />
The problem is, button is stay presed without animation, (yes i have 3 diferent picture in drawable folder)
Funny thing is, I for one button works but the other does not
i try this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/backpress" android:state_pressed="true"/>
<item android:drawable="#drawable/back"/>
its work but for second button i try the same process
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/forwardpress" android:state_pressed="true"/>
<item android:drawable="#drawable/forward"/>
</selector>
for second button doesnt work, wierd...
Clean and build your project again.
I want to make a custom RadioGroup with shapes of my own.
I want the android:checked="true" to look like this:
I think you should use a selector in that case. I use this for CheckBoxes, simply put the resulting XML file as android:background. This whould be an example:
<?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_checked" />
<item android:state_checked="false" android:drawable="#drawable/radio_unchecked" />
</selector>
I have a drawable named btndonate.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true" android:dither="true"
android:variablePadding="true"
>
<item android:drawable="#drawable/donate_hover"
android:state_pressed="true" />
<item android:drawable="#drawable/btn_donate"
android:state_window_focused="true" />
<item android:state_focused="true" android:drawable="#drawable/donate_hover"> </item>
<item android:state_focused="false" android:drawable="#drawable/btn_donate"></item>
</selector>
I am using it in activity's xml like
<Button android:layout_centerInParent="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="#drawable/btndonate" android:id="#+id/btn_home_donate"></Button>
i am using the same button with different id's in my other activities xmls.. but it shows up in some and hidden in others i dont know y the same button does not show up n all my activities though the drawable is same for all of them .. ??
any one plz help ??
That's odd... Are you sure, you didn't make any mistakes?
Do you use the same xml file for your script?
Try using this:
Button anyButton = (Button) findViewById(R.id.anyButton);
anyButton.setVisibility(View.VISIBLE);
Hope this helps. Don't forget to mark correct when it works. ;)