0% found this document useful (0 votes)
160 views

Practical No 11 Checkbox

The document shows how to create an Android application with five checkboxes to select programming languages and display the selected options in a toast notification. It includes the XML layout file, Java activity code, and strings resource file.

Uploaded by

Harshal Bhangale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
160 views

Practical No 11 Checkbox

The document shows how to create an Android application with five checkboxes to select programming languages and display the selected options in a toast notification. It includes the XML layout file, Java activity code, and strings resource file.

Uploaded by

Harshal Bhangale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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>

You might also like