Android program to implement checkbox widget and intent

Android program to implement checkbox widget and intent

In this program, we will be passing the total amount of all the food items checked to another activity(screen), so we can better understand checkbox widget as well as intent.

Here we will have two java files one for displaying the items with checkboxes(MainActivity.java), and the other for displaying the total amount of all the food items checked(SecondActivity.java).

Steps to create the application:- 

  • Open Android Studio and create a new Android application and name it as “CheckBox” and company domain as codedost so your package will be automatically set.
  • Open an Empty Activity and name it as MainActivity.
  • You will need to also create another Activity called SecondActivity. Images are shown on how to create the second activity with its XML file. For that right click on app, goto New, goto Activity, Empty activity and type in SecondActivity and click on OK. This will create both java file as well as the xml file for you
  • Copy the contents of res/layout/activity_main.xml file and res/layout/activity_second.xml file.
  • Run the application to launch Android emulator or you can run it on your mobile also(which is way faster).

XML File(res/layout/activity_main.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="codedost.checkbox.MainActivity">

    <CheckBox
        android:text="Paneer Makahani (Rs 325)"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="21sp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="61dp"
        android:id="@+id/checkBox1" />

    <CheckBox
        android:text="Roomali Roti (Rs 80)"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkBox1"
        android:layout_marginTop="36dp"
        android:textSize="21sp"
        android:id="@+id/checkBox2" />

    <CheckBox
        android:text="PATNE RICE (Rs 500)"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkBox2"
        android:layout_marginTop="42dp"
        android:textSize="21sp"
        android:id="@+id/checkBox3" />

    <Button
        android:text="PLACE ORDER"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkBox3"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="78dp"
        android:id="@+id/button" />
</RelativeLayout>

XML File(res/layout/activity_main.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="TextView"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:id="@+id/textView" />
</LinearLayout>

MainActivity.java

package codedost.checkbox;

import android.content.Intent;
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 {

    CheckBox cb1,cb2,cb3;
    Button bOrder;


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

        cb1=(CheckBox) findViewById(R.id.checkBox1);
        cb2=(CheckBox) findViewById(R.id.checkBox2);
        cb3=(CheckBox) findViewById(R.id.checkBox3);
        bOrder=(Button) findViewById(R.id.button);


        bOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer cost= new StringBuffer();
                int amount=0;
                if(cb1.isChecked())
                {
                    cost.append("\nPaneer Makahani (Rs 325)");
                    amount+=325;
                }
                if(cb2.isChecked())
                {
                    cost.append("\nRoomali Roti (Rs 80)");
                    amount+=80;
                }
                if(cb3.isChecked())
                {
                    cost.append("\nPATNE RICE (Rs 500)\n");
                    amount+=500;
                }
                cost.append("\n\nTotal cost = Rs"+amount);
                String r=cost.toString();
                Intent i=new Intent(MainActivity.this,SecondActivity.class);
                i.putExtra("Cost",r);
                startActivity(i);

//                Toast.makeText(getApplicationContext(),"Total Amount = "+amount+"/-",Toast.LENGTH_LONG).show();
            }
        });
    }
}

SecondActivity.java

package codedost.checkbox;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity{

    TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        tv=(TextView) findViewById(R.id.textView);

        Intent i=getIntent();
        String cost=i.getStringExtra("Cost");
        tv.setText(cost);
    }
}

Output

Share Me!