Quick Contact


    Android Check Box

    The Android CheckBox is a two-state icon that can be tested or unchecked. Android. Widget contains the Checkbox. Checkbox is a kind of checkbox. The Checkbox class of Android is a subset of the Compound Button class. It is commonly used in situations where a person can choose one or more options from a set of options. Choosing sports, for example.

    Checkboxes can be used in a variety of ways. It may, for example, be used to determine the user’s hobby, trigger or deactivate a certain action, and so on.

    The Checkbox category of Android is a subset of the Compound Button class.

    Android CheckBox class

    Methods of CheckBox class

    The CheckBox class inherits several methods from the View, Text View, and Button classes. The following are a few of them:

    Android CheckBox Example

    activity_main.xml

    For the layout, move the three-check box and one mouse. This is how the activity main.xml file will appear now:

    File: activity_main.xml

    < ?xml version="1.0" encoding="utf-8"?>

    < android.support.constraint.ConstraintLayoutxmlns: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=”Tutorials.ducatindia.com.checkbox.MainActivity”>

    < CheckBox

    android:id=”@+id/checkBox”

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_marginLeft=”177dp”

    android:layout_marginTop=”78dp”

    android:text=”Onion”

    app:layout_constraintStart_toStartOf=”parent”

    app:layout_constraintTop_toTopOf=”parent” />

    < CheckBox

    android:id=”@+id/checkBox2″

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_marginLeft=”241dp”

    android:layout_marginTop=”34dp”

    android:text=”Tomato”

    app:layout_constraintStart_toStartOf=”parent”

    app:layout_constraintTop_toBottomOf=”@+id/checkBox” />

    < CheckBox

    android:id=”@+id/checkBox3″

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_marginLeft=”179dp”

    android:layout_marginTop=”22dp”

    android:text=”Apple”

    app:layout_constraintTop_toBottomOf=”@+id/checkBox2″ />

    app:layout_constraintStart_toStartOf=”parent”

    < Button

    android:id=”@+id/button”

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_marginLeft=”110dp”

    android:layout_marginTop=”184dp”

    android:text=”Order”

    app:layout_constraintStart_toStartOf=”parent”

    app:layout_constraintTop_toBottomOf=”@+id/checkBox3″ />

    < /android.support.constraint.ConstraintLayout>

    Activity class

    Let’s write programs to see which toggle switch is on and which is off.

    File: MainActivity.java

    package Tutorials.ducatindia.com.checkbox;

    import android.support.v7.app.AppCompatActivity;

    import android.os.Bundle;

    import android.view.View;

    import android.widget.Button;

    import android.widget.CheckBox;

    import android.widget.Toast;

    import android.os.Bundle;

    import android.widget.CheckBox;

    public class MainActivity extends AppCompatActivity {

    CheckBoxTomato,Apple;

    Button buttonOrder;

    Override

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    addListenerOnButtonClick();

    }

    public void addListenerOnButtonClick(){

    //Trying to obtain CheckBox and Key examples from the main.xml file of the task

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

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

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

    buttonOrder=(Button)findViewById(R.id.button);

    buttonOrder.setOnClickListener(new View.OnClickListener(){

    @Override

    public void onClick(View view) {

    int totalamount=0;

    StringBuilder result=new StringBuilder();

    result.append(“Selected Items:”);

    if(pizza.isChecked()){

    result.append(“\nTomato 200Rs”);

    totalamount+=200;

    }

    if(coffe.isChecked()){

    result.append(“\nCoffe 30Rs”);

    totalamount+=30;

    }

    if(burger.isChecked()){

    result.append(“\nBurger 105Rs”);

    totalamount+=105;

    }

    result.append(“\nTotal: “+totalamount+”Rs”);

    Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();

    }

    });

    }

    }

    Example 2

    package tutorials.ducatindia.tdi.checkbox;

    import android.support.v7.app.AppCompatActivity;

    import android.os.Bundle;

    import android.view.View;

    import android.widget.CheckBox;

    import android.widget.Toast;

    	public class MainActivity extends AppCompatActivity {
    	CheckBoxkh, kh1, kh2, kh3;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    
    		setContentView(R.layout.activity_main);
    
    		// Finding CheckBox by its unique ID
    		kh=(CheckBox)findViewById(R.id.checkBox);
    		kh1=(CheckBox)findViewById(R.id.checkBox2);
    		kh2=(CheckBox)findViewById(R.id.checkBox3);
    		kh3=(CheckBox)findViewById(R.id.checkBox4);
    	}
    
    	public void Check(View v)
    	{
    		String msg="";
    
    		
    
    		if(kh.isChecked())		
    			msg = msg + " Painting ";
    		if(kh1.isChecked())
    			msg = msg + " Reading ";
    		if(kh2.isChecked())
    			msg = msg + " Singing ";
    		if(kh3.isChecked())
    			msg = msg + " Cooking ";
    		// message using show() method.
    		Toast.makeText(this, msg + "are selected",
    					Toast.LENGTH_LONG).show();
    	}
    }
    	
    activity_main.xml

    A Text View, four Checkboxes, and a button are included in the activity main.xml file. The user is prompted to choose his or her hobbies by the Text View.

    The user selects their options first, then clicks the Enter key. After clicking the Submit button, a toast will appear, displaying the hobbies you’ve chosen.

    < ?xml version="1.0" encoding="utf-8"?>

    < LinearLayoutxmlns: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”

    android:background=”#ffffff”

    android:orientation=”vertical”

    tools:context=”com.example.hp.checkbox.MainActivity”>

    < TextView

    android:id=”@+id/textView”

    android:layout_width=”match_parent”

    android:layout_height=”wrap_content”

    android:layout_marginEnd=”8dp”

    < !--create 8dp space from start of margin-->

    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”

    < !-- covers the entire width of the screen -->

    android:layout_width=”match_parent”

    < !-- covers as much height as required. -->

    android:layout_height=”wrap_content”

    android:text=”Painting”

    android:layout_marginTop=”16dp”

    android:textSize=”18sp” />

    < CheckBox

    android:id=”@+id/checkBox2″

    < !-- covers the entire width of the screen -->

    android:layout_width=”match_parent”

    < !-- covers as much height as required. -->

    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>

    Output:
    Android Check Box

    Enroll Yourself in Live Classes For Android Training.

    Copyright 1999- Ducat Creative, All rights reserved.