Sunday, July 12, 2020

Switch Example in Android

Working with Switch in Android


A Switch is a two-state toggle button. A user can select between two options.

Add switch in layout :

<Switch    
android:layout_marginTop="5dp"    
android:id="@+id/switchs"    
android:layout_width="wrap_content"    
android:layout_height="wrap_content"    
android:text="Show System Apps  "    
android:textSize="15dp"    
android:layout_gravity="right"    
android:layout_marginRight="5dp"    
android:textColor="#4d4d4d"/>






in Activity handle the click event


Switch switch = (Switch)findViewById(R.id.switch);

switchShowSystemApps.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

      
        if(isChecked == true)
        {
            // your code
        }
        else        {
           // your code
        }
    }
});

No comments:

Post a Comment