Android में CheckBox के साथ ऑर्डर सिस्टम बनाना
यह उदाहरण दर्शाता है कि कई CheckBox विजेट्स का उपयोग करके एक सरल फूड ऑर्डर ऐप कैसे बनाया जाए, जहां उपयोगकर्ता आइटम चुन सकता है और कुल राशि देख सकता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: लेआउट फ़ाइल (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="MainActivity">
<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="पिज़्ज़ा"
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="कॉफी"
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="बर्गर"
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="ऑर्डर करें"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox3" />
</android.support.constraint.ConstraintLayout>
चरण 3: MainActivity में लॉजिक (src/MainActivity.java)
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;
public class MainActivity extends AppCompatActivity {
private CheckBox pizza, coffee, burger;
private Button buttonOrder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pizza = findViewById(R.id.checkBox);
coffee = findViewById(R.id.checkBox2);
burger = findViewById(R.id.checkBox3);
buttonOrder = findViewById(R.id.button);
buttonOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int totalAmount = 0;
StringBuilder result = new StringBuilder();
result.append("चुने गए आइटम:\n");
if (pizza.isChecked()) {
result.append("पिज़्ज़ा - 100₹\n");
totalAmount += 100;
}
if (coffee.isChecked()) {
result.append("कॉफी - 50₹\n");
totalAmount += 50;
}
if (burger.isChecked()) {
result.append("बर्गर - 120₹\n");
totalAmount += 120;
}
result.append("\nकुल: ").append(totalAmount).append("₹");
Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();
}
});
}
}
CheckBox के महत्वपूर्ण गुण
android:text: चेकबॉक्स के बगल में दिखने वाला लेबलisChecked(): चेकबॉक्स चयनित है या नहीं जांचने के लिएsetOnCheckedChangeListener: चेक स्टेट बदलने पर तुरंत प्रतिक्रिया के लिएandroid:buttonTint: चेकबॉक्स का रंग बदलने के लिए (API 21+)
चरण 4: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ऐप चलाएं
ऐप लॉन्च करें, चेकबॉक्स चुनें/अनचुनें, और "ऑर्डर करें" बटन दबाकर चयनित आइटम और कुल राशि का टोस्ट देखें।

