I need to make Toggle button programmatically On & OFF.
You can use toggleButton.setChecked(true or false)
method to make Toggle button programmatically On & OFF.
It's so simple inside your layout file
<ToggleButton android:id="@+id/ToggleButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Off Stage"
android:textOn="On Stage"/>
and in Java
ToggleButton tglbtn = (ToggleButton)findViewById(R.Id.ToggleButton01);
tglbtn.setChecked(false);
Try toggleButton.setSelected(true)
& toggleButton.setSelected(false)
It will make toggle on & off.
This will make the toggle to true or false. U can use toggleButton.toggle();
to change from one state to other.
setSelected()
doesn't change the toggle state. According to google it does the following: Changes the selection state of this view. A view can be selected or not. Note that selection is not the same as focus. Views are typically selected in the context of an AdapterView like ListView or GridView; the selected view is the view that is highlighted.
–
Seattle Try ToggleButton. It has a .toggle()
method to switch the states.
in xml file
<ToggleButton android:id="@+id/ToggleButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Off Stage"
android:textOn="On Stage"/>
in java file
ToggleButton tglbtn;
tglbtn=(ToggleButton) findViewById(R.id.ToggleButton01);
tglbtn.toggle();
this idea also you can try
tglbtn.setSelected(false);
To change both the state and the UI of a toggle button you need to implement two functions:
toggle.setChecked(Boolean value)
toggle.setSelected(Boolean value)
setChecked() sets the intrinsic boolean associated with the view object and setSelected sets the UI.
To Toggle set and get value use the following
togg=toggleButton.getText().toString();
// Get the default value off
String off =mo.getOn();
if (off.equals("OFF")){
holder.toggleButton.setChecked(false);
}else{
holder.toggleButton.setChecked(true);
}
© 2022 - 2024 — McMap. All rights reserved.
Switch
too. – Calcaneus