Ad Code

Responsive Advertisement

Basic Android Program 15

Question 15: Write a program to show five checkboxes and toast selected checkboxes.

Download hole Program / Project code, by clicking following link:

Name the different methods of Checkbox in Android Studio?

Here are some of the methods of CheckBox in Android Studio:
  1. isChecked(): This method returns a boolean value indicating whether the CheckBox is currently checked or not.
  2. setChecked(boolean checked): This method sets the checked state of the CheckBox to the specified boolean value.

  3. toggle(): This method toggles the current checked state of the CheckBox.

  4. setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener): This method sets a listener to be called when the checked state of the CheckBox changes.

  5. setButtonDrawable(Drawable drawable): This method sets the drawable used as the background for the CheckBox.
  6. setText(CharSequence text): This method sets the text displayed next to the CheckBox.
Note that these are just a few of the methods available for the CheckBox control. There are many other methods that can be used to customize the behavior and appearance of the CheckBox.

List different attributes of Checkbox in Android Studio?

Here are some of the attributes of CheckBox in Android Studio:
  1. android:id: This attribute sets the unique identifier for the CheckBox.
  2. android:checked: This attribute sets whether the CheckBox is checked by default or not.
  3. android:text: This attribute sets the text displayed next to the CheckBox.

  4. android:button: This attribute sets the drawable used as the background for the CheckBox.
  5. android:buttonTint: This attribute sets the color used to tint the CheckBox.
  6. android:padding: This attribute sets the padding around the CheckBox.
  7. android:layout_width and android:layout_height: These attributes set the width and height of the CheckBox.

  8. android:layout_gravity: This attribute sets the gravity of the CheckBox within its parent layout.

  9. android:layout_margin: This attribute sets the margin around the CheckBox.
Note that these are just a few of the attributes available for the CheckBox control. There are many other attributes that can be used to customize the appearance and behavior of the CheckBox.

Write xml tag to create a checkbox named “Android”?

To create a CheckBox named "Android" in XML, use the following code:
<CheckBox
android:id="@+id/checkBox_android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android"/>
In this code, the android:id attribute sets the unique identifier for the CheckBox, which is checkBox_android in this case. The android:layout_width and android:layout_height attributes set the width and height of the CheckBox to "wrap_content". The android:text attribute sets the text displayed next to the CheckBox to "Android".


Programming Code:

Following code write in: MainActivity.java
package com.go2collage.practical_15;

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 {

private CheckBox ch1, ch2, ch3, ch4, ch5, ch6, ch7, ch8, ch9, ch10;
private Button submit;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// checkbox
ch1 = findViewById(R.id.chkAndroid);
ch2 = findViewById(R.id.chkFlutter);
ch3 = findViewById(R.id.chkC);
ch4 = findViewById(R.id.chkCpp);
ch5 = findViewById(R.id.chkHTML);
ch6 = findViewById(R.id.chkJS);
ch7 = findViewById(R.id.chkJAVA);
ch8 = findViewById(R.id.chkPHP);
ch9 = findViewById(R.id.chkXML);
ch10 = findViewById(R.id.chkKotlin);

// btn submit
submit = findViewById(R.id.btnSubmit);

submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

String showSelected = "You Selected: \n";

if (ch1.isChecked())
showSelected += "Android \n";

if (ch2.isChecked())
showSelected += "Flutter \n";

if (ch3.isChecked())
showSelected += "C \n";

if (ch4.isChecked())
showSelected += "C++ \n";

if (ch5.isChecked())
showSelected += "HTML \n";

if (ch6.isChecked())
showSelected += "JavaScript \n";

if (ch7.isChecked())
showSelected += "JAVA \n";

if (ch8.isChecked())
showSelected += "PHP \n";

if (ch9.isChecked())
showSelected += "XML \n";

if (ch10.isChecked())
showSelected += "Kotlin \n";

Toast.makeText(MainActivity.this, showSelected, Toast.LENGTH_SHORT).show();

}
});

}
}

Following code write in: activity_main.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"
tools:context=".MainActivity"
android:orientation="vertical"
android:layout_margin="20dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="My Favourite Programming Languages"
android:textSize="20sp"
android:textStyle="bold"
android:textAlignment="center"
android:textColor="@color/black"
android:layout_marginTop="30dp"
android:layout_marginBottom="20dp"
tools:ignore="HardcodedText" />


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="5dp"
>

<LinearLayout
android:layout_width="175dp"
android:layout_height="wrap_content"
android:orientation="vertical"
>

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chkAndroid"
android:text="@string/android"
android:textSize="18sp"
android:textColor="@color/black"
/>

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chkFlutter"
android:text="@string/flutter"
android:textSize="18sp"
android:textColor="@color/black"
/>

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chkC"
android:text="@string/c"
android:textSize="18sp"
android:textColor="@color/black"
/>

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chkCpp"
android:text="@string/cpp"
android:textSize="18sp"
android:textColor="@color/black"
/>

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chkHTML"
android:text="@string/html"
android:textSize="18sp"
android:textColor="@color/black"
/>

</LinearLayout>

<LinearLayout
android:layout_width="175dp"
android:layout_height="wrap_content"
android:orientation="vertical"
>

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chkJS"
android:text="@string/js"
android:textSize="18sp"
android:textColor="@color/black"
/>

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chkJAVA"
android:text="@string/java"
android:textSize="18sp"
android:textColor="@color/black"
/>

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chkPHP"
android:text="@string/php"
android:textSize="18sp"
android:textColor="@color/black"
/>

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chkXML"
android:text="@string/xml"
android:textSize="18sp"
android:textColor="@color/black"
/>

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chkKotlin"
android:text="@string/kotlin"
android:textSize="18sp"
android:textColor="@color/black"
/>

</LinearLayout>

</LinearLayout>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnSubmit"
android:text="Submit"
android:layout_marginTop="20dp"
android:textSize="18sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="go2collage"
android:textSize="24sp"
android:textStyle="bold"
android:textAlignment="center"
android:layout_marginTop="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />

</LinearLayout>

Output:



Post a Comment

0 Comments

Ad Code

Responsive Advertisement