MAIN ACTIVITY JAVA
package com.example.viraj.aswitch;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Switch aswitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aswitch=(Switch)findViewById(R.id.switch1);
aswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean bviraj) {
if (bviraj==true)
{
Toast.makeText(getApplicationContext(),"THE SWITCH IS ON",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),"THE SWITCH IS Off",Toast.LENGTH_SHORT).show();
}
}
});
}
}
package com.example.viraj.aswitch;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Switch aswitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aswitch=(Switch)findViewById(R.id.switch1);
aswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean bviraj) {
if (bviraj==true)
{
Toast.makeText(getApplicationContext(),"THE SWITCH IS ON",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),"THE SWITCH IS Off",Toast.LENGTH_SHORT).show();
}
}
});
}
}
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"
tools:context="com.example.viraj.aswitch.MainActivity">
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="197dp"
android:text="Switch....."
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
0 comments:
Post a Comment