Friday, 7 June 2013

Toggle Button Android


Toggle Button - UI Tutorials Series 

Hi Guys I decide to make all simple and complex UI control/view Tutorials for newbies :)
So here is our first example.
Toggle Button in Android
This is just the simple example for implementing Toggle Button in android application
Create Simple Layout as
<RelativeLayout 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" >

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="55dp"
        android:textOn="On"
        android:textOff="Off" />


</RelativeLayout>

and Here is Our Favorite MainActivity.java :)

public class MainActivity extends Activity {
       private ToggleButton onOffTBtn;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);

              onOffTBtn = (ToggleButton) findViewById(R.id.toggleButton1);

              onOffTBtn.setOnClickListener(new OnClickListener() {

                     @Override
                     public void onClick(View v) {
                           // TODO Auto-generated method stub
                           if (onOffTBtn.isChecked()) {
                                  Toast.makeText(MainActivity.this, "On", 1).show();
                           } else {
                                  Toast.makeText(MainActivity.this, "Off", 1).show();
                           }

                     }
              });
       }

}


you can download example from here 
Happy Coddddddding :)

No comments:

Post a Comment