Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example on ToggleButton

I am developing an application using a toggle button, I entered 1 or 0 in EditText. When button is clicked, the toggle button has to change if I enter 1 the toggle button shows TOGGLE ON, if I enter 0 the toggle button has to show TOGGLE OFF. I am unable to get toggle values when the button is clicked.

My code is:

public class MainActivity extends Activity {

  String editString="";

  Button btn;
  EditText ed;
  ToggleButton toggle;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button)findViewById(R.id.btn);
    ed  = (EditText)findViewById(R.id.ed);
    toggle = (ToggleButton)findViewById(R.id.toggBtn);

    editString = ed.getText().toString();

    btn.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub
        toggle.toggle();
        if(editString.equals("1")){

          toggle.setTextOff("TOGGLE ON");

        }
        else if(editString.equals("0")){

          toggle.setTextOn("TOGGLE OFF");

        }
      }
    });
  }
}

xml file:

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" 
android:orientation="vertical">

 <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ed"/>
 <Button  android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/btn"
         android:text="Summit"/>
  <ToggleButton
        android:id="@+id/toggBtn"
        android:layout_below="@+id/text6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:gravity="center"
         />  

like image 806
Durga Avatar asked Jan 22 '14 08:01

Durga


People also ask

What is ToggleButton How do you create it explain with example?

A toggle button allows the user to change a setting between two states. You can add a basic toggle button to your layout with the ToggleButton object. Android 4.0 (API level 14) introduces another kind of toggle button called a switch that provides a slider control, which you can add with a Switch object.

What is toggle switch in Android?

In android, Toggle Button is a user interface control that is used to display ON (Checked) or OFF (Unchecked) states as a button with a light indicator. The ToggleButton is useful for the users to change the settings between two states either ON or OFF.

What are activities in android?

An activity provides the window in which the app draws its UI. This window typically fills the screen, but may be smaller than the screen and float on top of other windows. Generally, one activity implements one screen in an app.


1 Answers

You should follow the Google guide;

ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            // The toggle is enabled
        } else {
            // The toggle is disabled
        }
    }
});

You can check the documentation here

like image 61
Berat Eyüboğlu Avatar answered Nov 03 '22 11:11

Berat Eyüboğlu