Wednesday, January 19, 2022

Android Checkbox Example - JAVA CheckBox Tutorial With Example In Android Studio Checkboxes | Android Developers

 Experiment 11- DIPLOMA

Hello Programmers!! 

So today I have come up with a new topic of Android Studio code/program . In this we will make an android application to demonstrate the use of checkbox using own customized android application in java

I have attached a XML, Java and Manifest file as well. There are many other Android Studio, C++, etc related programs on our blog.

How to use CheckBox in Android :-


XML code for CheckBox :-

<?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"

    tools:context=".MainActivity"

    android:orientation="vertical">

    <TextView

        android:id="@+id/textView"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_marginEnd="8dp"

    android:layout_marginStart="8dp"

    android:layout_marginTop="48dp"

    android:text="Choose your hobbies:"

    android:textSize="24sp"

    app:layout_constraintEnd_toEndOf="parent"

    app:layout_constraintStart_toStartOf="parent"

    app:layout_constraintTop_toTopOf="parent" />

    <CheckBox

        android:id="@+id/checkBox"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:text="Painting"

    android:layout_marginTop="16dp"

    android:textSize="18sp" />

    <CheckBox

        android:id="@+id/checkBox2"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:text="Reading"

    android:layout_marginTop="16dp"

    android:textSize="18sp" />

    <CheckBox

        android:id="@+id/checkBox3"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_marginTop="16dp"

    android:text="Singing"

    android:textSize="18sp"

app:layout_constraintTop_toTopOf="@+id/textView"

    tools:layout_editor_absoluteX="382dp" />

    <CheckBox

        android:id="@+id/checkBox4"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:text="Cooking"

    android:layout_marginTop="16dp"

    android:textSize="18sp"

    app:layout_constraintTop_toBottomOf="@+id/checkBox"

    tools:layout_editor_absoluteX="386dp" />

    <Button

        android:id="@+id/button"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_marginTop="16dp"

    android:onClick="Check"

    android:text="submit" />

</LinearLayout>


JAVA code for Checkbox :-

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.CheckBox;

import android.widget.Toast;

import android.view.View;

public class MainActivity extends AppCompatActivity {

    CheckBox ch, ch1, ch2, ch3;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ch = (CheckBox) findViewById(R.id.checkBox);

        ch1 = (CheckBox) findViewById(R.id.checkBox2);

        ch2 = (CheckBox) findViewById(R.id.checkBox3);

        ch3 = (CheckBox) findViewById(R.id.checkBox4);

    }

    public void Check(View v)

    {

        String msg="";

        if(ch.isChecked())

            msg = msg + " Painting ";

        if(ch1.isChecked())

            msg = msg + " Reading ";

        if(ch2.isChecked())

            msg = msg + " Singing ";

        if(ch3.isChecked())

            msg = msg + " Cooking ";


        // Toast is created to display the

        // message using show() method.

        Toast.makeText(this, msg + "are selected",

                Toast.LENGTH_LONG).show();

    }

}

Programs:-

For many other Language Programs of microprocessor 8086, C++ , Data Structures, etc and MSBTE diploma and MCA,BCA, engineering related concepts visit our  BLOG

No comments:

Post a Comment