Simple Checkboxes
Java
package com.example.project2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
CheckBox c1,c2;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
c1 = findViewById(R.id.m1);
c2 = findViewById(R.id.m2);
b1 = findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (c1.isChecked() && c2.isChecked()){
Toast.makeText(getApplicationContext(), "Bring me Thanos " +c1.getText().toString()+"\n"+c2.getText().toString(), Toast.LENGTH_SHORT).show();
}
else if(c1.isChecked()){
Toast.makeText(getApplicationContext(), c1.getText().toString(), Toast.LENGTH_SHORT).show();
} else if(c2.isChecked()){
Toast.makeText(getApplicationContext(), c2.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Movies Watched" />
<CheckBox
android:id="@+id/m1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Infinity War" />
<CheckBox
android:id="@+id/m2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Endgame" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SHOW" />
</LinearLayout>
Comments
Post a Comment