0% found this document useful (0 votes)
45 views4 pages

MAD LAB - Checkboxes

The document contains code for an Android app that displays a toast message based on checkboxes selected. It has a MainActivity class with an onSubmit method that builds a String based on checked checkboxes and displays a toast. The activity_main.xml layout contains 3 checkboxes and a button. Strings for the checkbox text and button are defined in strings.xml.

Uploaded by

Sharan M A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views4 pages

MAD LAB - Checkboxes

The document contains code for an Android app that displays a toast message based on checkboxes selected. It has a MainActivity class with an onSubmit method that builds a String based on checked checkboxes and displays a toast. The activity_main.xml layout contains 3 checkboxes and a button. Strings for the checkbox text and button are defined in strings.xml.

Uploaded by

Sharan M A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

MAD LAB

Aditya Kumar

//MainActivity.java
package com.example.checkbox_toast;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onSubmit(View view){
StringBuilder sb = new StringBuilder("toppings_label");
if(((CheckBox)findViewById(R.id.op1)).isChecked()){
sb.append("chocolate_syrup_text");
}
if(((CheckBox)findViewById(R.id.op2)).isChecked()){
sb.append("sprinkles_text");
}
if(((CheckBox)findViewById(R.id.op3)).isChecked()){
sb.append("crushed_nuts_text");
}
Toast t = Toast.makeText(this,sb,Toast.LENGTH_SHORT);
t.show();
}
}

//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="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<CheckBox
android:id="@+id/op1"
android:layout_width="165dp"
android:layout_height="50dp"
android:text="@string/opt1" />

<CheckBox
android:id="@+id/op2"
android:layout_width="165dp"
android:layout_height="50dp"
android:text="@string/opt2" />

<CheckBox
android:id="@+id/op3"
android:layout_width="165dp"
android:layout_height="50dp"
android:text="@string/opt3" />

<Button
android:id="@+id/button"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginStart="100dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="30dp"
android:onClick="onSubmit"
android:text="@string/btnText" />
</LinearLayout>

//strings.xml
<resources>
<string name="app_name">Checkbox_Toast</string>
<string name="opt1">Chocolate Syrup</string>
<string name="opt2">Sprinkles</string>
<string name="opt3">Crushed Nuts</string>
<string name="btnText">Lets Toast!</string>
</resources>
//MainActivity.java
package com.example.checkbox_toast;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onSubmit(View view){
int finalAmt = 0;
StringBuilder sb = new StringBuilder("Orders: \n");
if(((CheckBox)findViewById(R.id.op1)).isChecked()){
sb.append("Pizza (Rs350)\n");
finalAmt += 350;
}
if(((CheckBox)findViewById(R.id.op2)).isChecked()){
sb.append("Pasta (Rs250)\n");
finalAmt += 250;
}
if(((CheckBox)findViewById(R.id.op3)).isChecked()){
sb.append("Khichdi (Rs100)\n");
finalAmt += 100;
}
sb.append("Total: "+finalAmt);
Toast t = Toast.makeText(this,sb,Toast.LENGTH_SHORT);
t.show();
}
}

//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="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<CheckBox
android:id="@+id/op1"
android:layout_width="165dp"
android:layout_height="50dp"
android:text="@string/opt1" />

<CheckBox
android:id="@+id/op2"
android:layout_width="165dp"
android:layout_height="50dp"
android:text="@string/opt2" />

<CheckBox
android:id="@+id/op3"
android:layout_width="165dp"
android:layout_height="50dp"
android:text="@string/opt3" />

<Button
android:id="@+id/button"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginStart="100dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="30dp"
android:onClick="onSubmit"
android:text="@string/btnText" />
</LinearLayout>

//strings.xml
<resources>
<string name="app_name">MakeDolands</string>
<string name="opt1">Pizza</string>
<string name="opt2">Pasta</string>
<string name="opt3">Khichdi</string>
<string name="btnText">Lets Toast!</string>
</resources>

You might also like