Button, RadioButton, CheckBox in Android

Home/Android/Button, RadioButton, CheckBox in Android

Button, RadioButton, CheckBox in Android




Android Components Example: In this example, we show you how to make a use of Button, RadioButton and CheckBox in your application.

For a clear and easy to understand example, we can use another control i.e. Textview.

Note: This example is developed in Android Studio and tested in Android Marshmallow 6.0

This example represents a Food Buying app which calculates the price of the food you buy and make you pay with the options given like credit card or cash. It covers the basic android components like buttons, radiobuttons, checkbok, textview. If you check for burger and pasta then the app calculates the price for both of them and show you the total of both just below the total button. Similarly, if you you check for burger, uncheck for pasta and then check mexican pockets then it will charge you for burger and mexican pockets instead of adding up the charge for pasta.

For doing this you have to integrate buttons, radiobuttons and checkbox in your .xml file according to your use and design and then you have to change your .java file and assign controls to your components you used.

1. Add Button, RadioButton, TextView and CheckBox in .xml file 

main.xml


2.  Code

Write the following code in your main file i.e. MainActivity.java

MainActivity.java

package com.example.cloudinfinity.myapplication;

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.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
{
    CheckBox c1,c2,c3;
    Button b;
    TextView t1,t2;
    int total=0;
    RadioButton r1,r2;
    String s;
    int f1=0,f2=0,f3=0;


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

        c1=(CheckBox) findViewById(R.id.checkBox);
        c2=(CheckBox) findViewById(R.id.checkBox2);
        c3=(CheckBox) findViewById(R.id.checkBox3);
        b=(Button) findViewById(R.id.button);
        t1=(TextView) findViewById(R.id.textView);
        t2=(TextView) findViewById(R.id.textView1);
        r1=(RadioButton) findViewById(R.id.radioButton);
        r2=(RadioButton) findViewById(R.id.radioButton2);


        c1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v)
            {
                s = "Burger = Rs. 60/-";
                Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
                if(f1==0)
                {
                    f1=1;
                    total += 60;
                }
                else
                {
                    f1=0;
                    total-=60;
                }
            }
        });

        c2.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                s="Pasta = Rs. 120/-";
                Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
                if(f2==0)
                {
                    f2=1;
                    total += 120;
                }
                else
                {
                    f2=0;
                    total-=120;
                }
            }
        });

        c3.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                s="Mexican Wrap = Rs. 80/-";
                Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
                if(f3==0)
                {
                    f3=1;
                    total += 80;
                }
                else
                {
                    f3=0;
                    total-=80;
                }
            }
        });

        b.setOnClickListener(new View.OnClickListener() {

                                 @Override
                                 public void onClick(View v) {
                                     t2.setText(String.valueOf(total));
                                 }
                             }
        );

        r1.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v)
            {
                s="Payment by Cash";
                Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
            }
        });

        r2.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                s="Payment by Credit/Debit";
                Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
            }
        });
    }
}

3. Run the application

This following result you will get:

Button
By | 2017-05-24T11:29:58+00:00 February 27th, 2016|Android|0 Comments

About the Author:

Leave A Comment

seventeen + 11 =

Avada WordPress Theme