Develop an android application to show five checkboxes and toast selected checkboxes
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/msg"
style="@style/TextAppearance.AppCompat.Large"
android:layout_margin="10dp"
android:textStyle="bold"/>
<CheckBox
android:id="@+id/chkAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/android"
style="@style/TextAppearance.AppCompat.Headline"/>
<CheckBox
android:id="@+id/chkJava"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/java"
style="@style/TextAppearance.AppCompat.Headline"/>
<CheckBox
android:id="@+id/chkPhp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/php"
style="@style/TextAppearance.AppCompat.Headline"/>
<CheckBox
android:id="@+id/chkCpp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cpp"
style="@style/TextAppearance.AppCompat.Headline"/>
<CheckBox
android:id="@+id/chkC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/c"
style="@style/TextAppearance.AppCompat.Headline"/>
<Button android:id="@+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display"
android:layout_marginTop="20dp"
android:onClick="showSelected"/>
</LinearLayout>
MainActivity.java
package com.example.checkboxdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private CheckBox chkAndroid, chkJava, chkPhp, chkCpp, chkC;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chkAndroid = findViewById(R.id.chkAndroid);
chkJava = findViewById(R.id.chkJava);
chkPhp = findViewById(R.id.chkPhp);
chkCpp = findViewById(R.id.chkCpp);
chkC = findViewById(R.id.chkC);
}
public void showSelected(View view) {
String selected = "You selected: \n";
if(chkAndroid.isChecked())
selected += "Android";
if(chkJava.isChecked())
selected += "\nJava";
if(chkPhp.isChecked())
selected += "\nPHP";
if(chkCpp.isChecked())
selected += "\nCPP";
if(chkC.isChecked())
selected += "\nC";
Toast.makeText(MainActivity.this, selected, Toast.LENGTH_SHORT).show();
}
}
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">CheckBoxApp</string>
<string name="msg">Select your favourite programming languages</string>
<string name="android">Android</string>
<string name="java">Java</string>
<string name="php">PHP</string>
<string name="cpp">CPP</string>
<string name="c">C</string>
</resources>
OUTPUT