Skip to content

Instantly share code, notes, and snippets.

@extsalt
Created June 25, 2021 11:24
Show Gist options
  • Save extsalt/7786169a4442969825f96409e4823d18 to your computer and use it in GitHub Desktop.
Save extsalt/7786169a4442969825f96409e4823d18 to your computer and use it in GitHub Desktop.
Android Checkbox demo
package com.example.mycalculator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button button;
CheckBox math, phy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
math = this.findViewById(R.id.math);
math.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String message = "";
if (isChecked) {
message = "You selected ";
} else {
message = "You did not select";
}
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
}
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment