Android: Specify two different images for togglebutton using XML
Your code is fine. However, the toggle button will display the first item in your selector that it matches, so the default should come last. Arrange the items in the following manner to ensure they will all be utilized:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true" /> //currently pressed turning the toggle on
<item android:state_pressed="true" /> //currently pressed turning the toggle off
<item android:state_checked="true" /> //not pressed default checked state
<item /> //default non-pressed non-checked
</selector>
Toggle button using two image on different state
Do this:
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/check" <!--check.xml-->
android:layout_margin="10dp"
android:textOn=""
android:textOff=""
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_centerVertical="true"/>
create check.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/selected_image"
android:state_checked="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/unselected_image"
android:state_checked="false"/>
</selector>
Setting image source for ToggleButton
You can make the toggle button look like whatever you want. You just need to create a style. Have a look in the /platforms//data/res/values/styles.xml and search for Widget.Button.ToggleButton It looks like this:
<style name="Widget.Button.Toggle">
<item name="android:background">@android:drawable/btn_toggle_bg</item>
<item name="android:textOn">@android:string/capital_on</item>
<item name="android:textOff">@android:string/capital_off</item>
<item name="android:disabledAlpha">?android:attr/disabledAlpha</item>
</style>
So if you go find the btn_toggle_bg drawable you find this:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+android:id/background" android:drawable="@android:drawable/btn_default_small" />
<item android:id="@+android:id/toggle" android:drawable="@android:drawable/btn_toggle" />
</layer-list>
Which shows you that it uses the standard Button
background and a drawable called btn_toggle for the src. btn_src.xml looks like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@drawable/btn_toggle_off" />
<item android:state_checked="true" android:drawable="@drawable/btn_toggle_on" />
</selector>
Which are the two drawables that are used to show the state of the button. They are actually called btn_toggle_{on/off}.9.png since they are 9 Patch images so they stretch to match the button size.
Change toggle button image onclick
Delete both btnFav.setChecked(true)
and btnFav.setChecked(false)
in your OnClick method. It is a togglebutton which toggles the setChecked on its own by every click and you reset it to the old value. So in your case it always has the same value(the start value).
I would suggest you rather use setOnCheckedChangeListener
instead of onClickListener
.
Android: Create a toggle button with image and no text
Can I replace the toggle text with an image
No, we can not, although we can hide the text by overiding the default style of the toggle button, but still that won't give us a toggle button you want as we can't replace the text with an image.
How can I make a normal toggle button
Create a file ic_toggle in your
res/drawable
folder<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="@drawable/ic_slide_switch_off" />
<item android:state_checked="true"
android:drawable="@drawable/ic_slide_switch_on" />
</selector>Here
@drawable/ic_slide_switch_on
&@drawable/ic_slide_switch_off
are images you create.Then create another file in the same folder, name it ic_toggle_bg
<?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_toggle" />
</layer-list>Now add to your custom theme, (if you do not have one create a styles.xml file in your
res/values/
folder)<style name="Widget.Button.Toggle" parent="android:Widget">
<item name="android:background">@drawable/ic_toggle_bg</item>
<item name="android:disabledAlpha">?android:attr/disabledAlpha</item>
</style>
<style name="toggleButton" parent="@android:Theme.Black">
<item name="android:buttonStyleToggle">@style/Widget.Button.Toggle</item>
<item name="android:textOn"></item>
<item name="android:textOff"></item>
</style>This creates a custom toggle button for you.
How to use it
Use the custom style and background in your view.
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
style="@style/toggleButton"
android:background="@drawable/ic_toggle_bg"/>
What kind of Button can toggle/change states in Android?
Try changing the topic to :"What kind of Button can toggle/change states?"
Seems like you need ToggleBotton
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="26dp"
android:background="@color/button_colors"
android:button="@null"
android:textOff="@null"
android:textOn="@null" />
And this xml defines the colors/images of the button at rest/pressed states, put it in res/color/button_colors.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/button_rest"/>
<item android:state_checked="true" android:drawable="@drawable/button_on" />
</selector>
How to create a custom toggle button with an image on it that also scales correctly for each density in android
Even the built in two-state toggle button requires an onClick method to handle click events, for that reason I would argue that it is no more complex to have a regular button which handles visual toggling in an onClick method of its own.
Since you want the option of using images I will use an ImageButton for the example.
The XML for an ImageButton with a custom color:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/toggleButton"
android:layout_marginTop="152dp"
android:layout_centerHorizontal="true"
android:src="@drawable/state0"
android:onClick="onToggleClick"
android:background="@color/red"/>
Color assets should be defined in a new XML values file ( see http://developer.android.com/guide/topics/resources/more-resources.html#Color)
The onClick method to handle toggling the visuals of the button looked like this in the example I built for myself:
public void onToggleClick(View view){
toggleState = (toggleState+1)%3;
switch(toggleState){
case 0:
toggleButton.setImageResource(R.drawable.state0);
toggleButton.setBackgroundColor(0xffff0000);
break;
case 1:
toggleButton.setImageResource(R.drawable.state1);
toggleButton.setBackgroundColor(0xff00ffff);
break;
case 2:
toggleButton.setImageResource(R.drawable.state2);
toggleButton.setBackgroundColor(0xffff00ff);
break;
}
}
Please don't hardcode the colors as hex values, make variables for them, in your final project.
For reference hex colors are in the form of 0xAARRGGBB
To the comment:
If you would like to pair text and an image on the same button android supports that in XML. It would look something like this to have an icon to the left of a button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:drawableLeft="@drawable/button_icon"
... />
source: http://developer.android.com/guide/topics/ui/controls/button.html
But then you wouldn't be able to switch the image with the method of the ImageButton class.
Change Image with toggle button
Try this logic for toggling image with only one button ...write this in click func
boolean check = false;
public void lightBox(View view) {
if(check) {
image
.setImageResource(R.drawable.kawa1 );
check = false;}
else {
image
.setImageResource(R.drawable.kawa2 );
check = true;}
}
Related Topics
I Cant See Any Firebaserecycleradapter Items on My Layout
How to Resolve "Duplicate Files Copied in APK Meta-Inf/*"
Listview Viewholder Checkbox State
Why Do We Have to Call Super in Android Sometimes
How to Ask Permission to Access Gallery on Android M.
Version Control for One-Man Project Using Eclipse
Reading a Jsp Variable from JavaScript
Running a .SQL Script Using MySQL with Jdbc
Leaking This in Constructor Warning
Google Maps API and Custom Polyline Route Between Markers
Android "Single Top" Launch Mode and Onnewintent Method
Caused By: Java.Lang.Classnotfoundexception: Didn't Find Class in Firebase Phone Authentication
Reducing Android App (Apk) Size
Spannable on Android for Textview
Resources.Openrawresource() Issue Android
How Does One Configure Rjava on Osx to Select the Right Jvm -- .Jinit() Failing