Friday, April 25, 2014

Implementation of Toggle Button and CheckBox

In this post we will discuss some basic implementation of Toggle Button and CheckBox.

Both (i.e Toggle Button and CheckBox) allow the user to change a setting between two states.

Toggle Button are subclasses of compound Button and perform action in similar manner.

Here we have created a Android project which contains Both(i.e Toggle Button and CheckBox).



The Basic layout  for this project:-

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent"  
   android:orientation="vertical"   
   android:background="#fff88f">  
   <ToggleButton  
     android:id="@+id/toggleButton1"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:background="#ffaaaf"  
     android:text="ToggleButton" />  
   <TextView  
     android:id="@+id/textView1"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:background="#ff888f"  
     android:text="Large Text"  
     android:textAppearance="?android:attr/textAppearanceLarge" />  
   <CheckBox  
     android:id="@+id/checkBox1"  
     android:layout_width="320dp"  
     android:layout_height="wrap_content"  
     android:layout_gravity="right"  
     android:background="#f11800"  
     android:onClick="onCheckboxClicked"  
     android:text="    CHECK BOX" />  
   <TextView  
     android:id="@+id/textView2"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:background="#444888"  
     android:text="Large Text"  
     android:textAppearance="?android:attr/textAppearanceLarge" />  
 </LinearLayout>  


The java code :-

 public class MainActivity extends Activity {  
      ToggleButton Tbtn;  
      CheckBox cb;  
      TextView tv1, tv2;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
           Tbtn = (ToggleButton)findViewById(R.id.toggleButton1);  
           cb = (CheckBox)findViewById(R.id.checkBox1);  
           tv1 = (TextView)findViewById(R.id.textView1);  
           tv2 = (TextView)findViewById(R.id.textView2);  
           // Toggle Button  
           Tbtn.setOnClickListener(new View.OnClickListener() {  
             public void onClick(View v) {  
               if (Tbtn.isChecked()) {  
                 tv1.setText("The Toggle Button is ON");  
               } else {  
                    tv1.setText("The Toggle Button is OFF");  
               }  
             }  
           });  
           //CheckBox  
      }  
      public void onCheckboxClicked(View view) {  
        // Is the view now checked?  
        boolean checked = ((CheckBox) view).isChecked();  
        // Check which checkbox was clicked  
        switch(view.getId()) {  
          case R.id.checkBox1:  
            if (checked)  
            {  
                 tv2.setText("checkbox is checked");  
            }  
            else{  
                 tv2.setText("checkbox is unchecked");  
            }  
            break;  
        }  
      }  


The link for the code :-  https://www.dropbox.com/s/qa93eybbpp9a6wb/checktoggle.zip?m=

In my next post i will show how to create menu.




No comments:

Post a Comment