Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make ON/OFF switch button in android.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 152
    Comment on it

    In the below code first I have added a Switch button in main layout. Here I have created two-state toggle switch that can select between two options. The user may drag the thumb back and forth to choose the selected option. In MainActivity I have used onCheckedChanged method and toast function. See the below code it will clearly described how to make On/Off switch button .

    Step(1)-Main.xml layout-

    <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:layout_marginTop="100dp"
        android:padding="5dp"
        tools:context=".MainActivity" >
    
        <Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Switch"
        android:id="@+id/mySwitch"
    
        android:layout_centerHorizontal="true"
            android:checked="false" />
    
        android:layout_alignParentLeft=true
        android:layout_alignParentTop=true
        android:layout_marginLeft=48dp
        android:layout_marginTop=26dp
        android:height=50dp
        android:text=@string/switch_text
        android:textSize=50sp
        android:switchMinWidth=70sp
        android:switchPadding=60sp/>
    </RelativeLayout>
    

    Step(2)-MainActivity-

    public class MainActivity extends Activity {
        private Switch mySwitch;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mySwitch=(Switch)findViewById(R.id.mySwitch);
    
            mySwitch.setChecked(true);
         mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
             @Override
         public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
    
                 if(isChecked){
                     Toast.makeText(getApplicationContext(),
                             "Notification Switch is Off",Toast.LENGTH_SHORT).show();
                 }
             }
             });
         }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: