Mobile Application Development Practical 11

Question 1
Write a program to show five checkboxes and toast selected checkboxes.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="40dp">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Select names of states from below"
            android:textColor="@color/purple_700"
            android:textSize="20dp"/>

        <CheckBox
            android:id="@+id/mah"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Maharashtra"
            android:layout_marginTop="10dp"/>
        <CheckBox
            android:id="@+id/guj"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Gujrat" />
        <CheckBox
            android:id="@+id/kar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Karnataka" />
        <CheckBox
            android:id="@+id/tamil"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tamil Nadu" />
        <CheckBox
            android:id="@+id/goa"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Goa" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Submit"
            android:layout_marginLeft="8dp"/>
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.practical11;

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 ch1,ch2,ch3,ch4,ch5;
    Button b;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b=findViewById(R.id.button);
        ch1=findViewById(R.id.mah);
        ch2=findViewById(R.id.guj);
        ch3=findViewById(R.id.kar);
        ch4=findViewById(R.id.tamil);
        ch5=findViewById(R.id.goa);
        View.OnClickListener cl=new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String msg="";
                if(ch1.isChecked())
                    msg=msg+"Maharashtra";
                if(ch2.isChecked())
                    msg=msg+",Gujrat";
                if(ch3.isChecked())
                    msg=msg+",Karnataka";
                if(ch4.isChecked())
                    msg=msg+",Tamil Nadu";
                if(ch5.isChecked())
                    msg=msg+",Goa";
                Toast.makeText(MainActivity.this,msg+" is selected",Toast.LENGTH_LONG).show();
            }
        };
        b.setOnClickListener(cl);
    }
}
Output
Screenshot_20230312_142412

Leave a Comment

Your email address will not be published. Required fields are marked *