Saturday, 22 March 2014

toggle buttons in android


It allows the user to change the states by one button and user can identify it through the button text.
Here is the example to work with the toggle button. You need to place the toggle button control in the XML screen it will look like as;

XML code for that Toggle button control  will be look alike;
<ToggleButton 

    android:id="@+id/btnToggle"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:textOn="On"

    android:textOff="Off"

    android:onClick="onToggleClicked"/>
Event to handle in code side  there it will fire the onToggleClicked event;
public void onToggleClicked(View view) {

   

    boolean on = ((ToggleButton) view).isChecked();

    

    if (on) {

        // Enable Yes

    } else {

        // Disable NO

    }

}
Finally it will look like;



No comments:

Post a Comment