Skip to content

Instantly share code, notes, and snippets.

@alitamoor65
Last active April 2, 2018 14:50
Show Gist options
  • Save alitamoor65/6e3357649c44f2f9eabdafd47a9c55e7 to your computer and use it in GitHub Desktop.
Save alitamoor65/6e3357649c44f2f9eabdafd47a9c55e7 to your computer and use it in GitHub Desktop.
Toggle Button Android Studio
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
android:gravity="center"
tools:context="jovial.etutorialscafe.togglebutton.MainActivity">
<ToggleButton
android:background="@drawable/mtogglebutton"
android:id="@+id/tooglebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
package jovial.etutorialscafe.togglebutton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
ToggleButton toggleButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton = (ToggleButton)findViewById(R.id.tooglebutton);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(MainActivity.this, "Toggle ON", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this, "Toggle OFF", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment