Open top menu

How to uesd ToggleButton in android?


In this blog simple explain how to used ToggleButton.

setTextOff()--Means the ToggleButton is Checked.

setTextOn()--Means the ToggleButton is UnChecked.

activity_main.xml................................................................................................

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
android:layout_width="fill_parent"    
android:layout_height="fill_parent"   
android:orientation="vertical"     >

<ImageView            
android:layout_width="wrap_content"            
android:layout_height="wrap_content"            
android:id="@+id/imageView"            
android:layout_gravity="center"            
android:src="@drawable/my_heder_img"             />

<ToggleButton            
android:layout_width="200dp"           
android:layout_height="wrap_content"            
android:text="New ToggleButton"           
android:layout_gravity="center"            
android:textOff="Show TextView"            
android:textOn="Hide TextView"            
android:id="@+id/toggleButton"             
 />



<EditText            
android:layout_width="200dp"            
android:layout_height="wrap_content"            
android:id="@+id/editText"            
android:visibility="gone"            
android:layout_marginTop="50dp"            
android:layout_gravity="center_horizontal" />


</LinearLayout>






MainActivity.java.................................................................................................

public class MainActivity extends Activity  {
    ToggleButton tgbutton;
    EditText edtext;
    @Override    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        setContentView(R.layout.activity_main);
        tgbutton = (ToggleButton) findViewById(R.id.toggleButton);

        edtext=(EditText)findViewById(R.id.editText);
        tgbutton.setOnClickListener(new View.OnClickListener() {

            @Override            public void onClick(View v) {
                // TODO Auto-generated method stub                if (tgbutton.isChecked()) {
                    edtext.setVisibility(View.VISIBLE);
                    Toast.makeText(getApplicationContext(),"SHOW",
                            Toast.LENGTH_SHORT).show();
                } else {

                    Toast.makeText(getApplicationContext(),"Hide",
                            Toast.LENGTH_SHORT).show();
                    edtext.setVisibility(View.GONE);
                }
            }
        });

    }
}






0 comments