mainActivity.java
------------------
package com.thistimebd.web.aswitch;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Switch aSwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aSwitch=(Switch) findViewById(R.id.switchId);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(MainActivity.this,"On",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,"Off",Toast.LENGTH_SHORT).show();
}
}
});
}
}
===========================================================================
activity_main.xml
----------------
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
tools:context="com.thistimebd.web.aswitch.MainActivity">
<Switch
android:id="@+id/switchId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="wifi"
android:textSize="20sp"
android:textStyle="bold"
android:drawableLeft="@drawable/wifi"
/>
</android.support.constraint.ConstraintLayout>
thistimebd Bangladesh Live online newsportal, education, Lifestyle, Health, Photography, gif image etc.
Make your own name or company name website | contact: thistimebd24@gmail.com
Copyright © 2020-2024 News Portal in Bangladesh - THISTIMEBD.COM. ALL Rights Reserved.