Checkbox in Android
Checkbox in Android
A checkbox is a graphical user interface element that is commonly used in Android applications to allow users to select one or more items from a list of options. A checkbox typically consists of a small square box that can be checked or unchecked and is accompanied by a label that describes the option being presented.
In Android, a checkbox is implemented as a subclass of the Text View class and can be easily added to a layout using XML or Java code. Once a checkbox has been added to a layout, its state can be programmatically controlled using the is Checked () method, which returns a Boolean value indicating whether the checkbox is currently checked or not.
Checkboxes are often used in combination with other UI elements, such as buttons and text views, to create more complex UI interactions. For example, a list of checkboxes could be used to allow users to select multiple items from a list, and a button could be used to perform an action based on the selected items.
Overall, checkboxes are a simple and powerful UI element in Android that can greatly enhance the usability of your application. By providing users with clear and intuitive options for selecting items, checkboxes can help to streamline the user experience and make your app more user-friendly.
In Android, a checkbox is implemented as a subclass of the Text View class and can be easily added to a layout using XML or Java code. Once a checkbox has been added to a layout, its state can be programmatically controlled using the is Checked () method, which returns a Boolean value indicating whether the checkbox is currently checked or not.
Checkboxes are often used in combination with other UI elements, such as buttons and text views, to create more complex UI interactions. For example, a list of checkboxes could be used to allow users to select multiple items from a list, and a button could be used to perform an action based on the selected items.
Overall, checkboxes are a simple and powerful UI element in Android that can greatly enhance the usability of your application. By providing users with clear and intuitive options for selecting items, checkboxes can help to streamline the user experience and make your app more user-friendly.
•Item ordering checkbox-
•Activity main.xml file-
<?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" android:layout_width="match_parent" android:layout_height="match_parent">
<CheckBox
android:id="@+id/checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="144dp" android:layout_marginTop="68dp" android:text="Lays" 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_marginStart="144dp" android:layout_marginTop="28dp"
android:text="Bytes" 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_marginStart="144dp" android:layout_marginTop="28dp"
android:text="Oreo" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/checkBox2" />
<Button
android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="144dp" android:layout_marginTop="184dp"
android:text="Order" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/checkBox3" />
</androidx.constraintlayout.widget.ConstraintLayout>
•Main Activity Java
package com.example.myapp;
import android.os.Bundle; import android.widget.Button; import android.widget.CheckBox; import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity { CheckBox a, b, c;
Button buttonOrder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); addListenerOnButtonClick();
}
public void addListenerOnButtonClick() {
//Getting instance of CheckBoxes and Button from the activty_main.xml file
a = findViewById(R.id.checkBox);
b = findViewById(R.id.checkBox2);
c = findViewById(R.id.checkBox3);
buttonOrder = findViewById(R.id.button); //Applying the
Listener on the Button click buttonOrder.setOnClickListener(view -> {
int totalamount = 0;
StringBuilder result = new StringBuilder(); // result.append("Selected Items:");
if (a.isChecked()) { result.append("\tLays");
totalamount += 1000; }
if (b.isChecked()) { result.append("\tBytes");
totalamount += 5000; }
if (c.isChecked()) { result.append("\tOreo");
totalamount += 1200; }
result.append("\nTotal: ").append(totalamount).append("$");
//Displaying the message on the toast Toast.makeText(getApplicationContext(),
Toast.LENGTH_LONG).show();
result.toString(),
});
}
}
Output
Comments
Post a Comment