Open top menu

Example-How to used SwitchButton in android studio?

The SwitchButton is a android widgets in android studio. switchbutton used two state ON and OFF.

setOnCheckedChangeListener()- User change button state onclick swicthButton . 

Then change state via this method

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"    
android:background="#ffffff"     >
<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"></ImageView>

<Switch            
android:layout_width="wrap_content"            
android:layout_height="wrap_content"            
android:id="@+id/switch1"            
android:layout_gravity="center_horizontal" />
             />
<TextView            
android:layout_width="200dp"            
android:layout_height="wrap_content"            
android:id="@+id/editText"            
android:layout_marginTop="30dp"            
android:layout_gravity="center_horizontal" />
<ImageView            
android:layout_width="wrap_content"            
android:layout_height="wrap_content"            
android:id="@+id/imagebuof"            
android:visibility="visible"            
android:layout_gravity="center"            
android:src="@drawable/bulb_off"></ImageView>
<ImageView       
 android:layout_width="wrap_content"       
 android:layout_height="wrap_content"        
android:id="@+id/imagebuon"        
android:layout_gravity="center"        
android:visibility="gone"        
android:src="@drawable/bulb_on"></ImageView>




</LinearLayout>










...................................................................................................................................................................



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

public class MainActivity extends Activity  {
    Switch tgbutton;
    TextView edtext;
    ImageView imageoff,imageon;
    @Override    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        setContentView(R.layout.content_main);
        tgbutton = (Switch) findViewById(R.id.switch1);

        edtext=(TextView)findViewById(R.id.editText);
        imageoff=(ImageView)findViewById(R.id.imagebuof);
        imageon=(ImageView)findViewById(R.id.imagebuon);
        tgbutton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {

                if(isChecked){
                    edtext.setText("Switch ON");
                    imageoff.setVisibility(View.GONE);
                    imageon.setVisibility(View.VISIBLE);
                }else{
                    edtext.setText("Switch OFF");
                    imageoff.setVisibility(View.VISIBLE);
                    imageon.setVisibility(View.GONE);
                }

            }
        });

    }
}

...................................................................................................................................................................



0 comments