Prerequisite

If you not understand this lecture Radio button in android studio.You should also read my previous lectures. Checkbox creation using android studio and Button and its click events in android . After reading these lecture you will also able to create android Radio button of your app.

Overview

Radio button allow to user to choice one option at  a same time to set predefined options. Radio button are arrange as group two or more and displayed on screen a list of circular holes that can contain white space .Radio button is a  two-states button that can be either checked or unchecked. When the radio button is checked, you can click it to unchecked it.

Introduction

In this lecture well we describe how can create Radio button in android.We will also describe Radio button and its description.what is Radio button. why we can use it .After Reading this lecture you will understand Radio button in android And i hope you will create Radio button of your application.

Description of  android radio button

In this lecture we will describe how can create a android Radio button . If you can use  android Radio button . you can select one option at a time. Radio button is a  two-states button that can be either checked or unchecked. When the radio button is checked, you can click it to unchecked it. Radio button  works with RadioGroupe. Radio group can hold multiple Radio Buttons.Once a radio button is checked by user it cannot be unchecked again by pressing the same button. In this application we describe how can create a Radio button and how can handle click events on Radio button.For more detail about android Radio button u click on Radio button using android. First of all you will create a new project in android studio.  Every project contain two file first is java class file and second is layout XML file your first step is open a layout XML file and add a text view and set height and width and we also  add a RadioGroupe . All  the radio button are add within  RadioGroupe. So you will find out the value folder expand this value folder and click on string XML file and add  string  resource in string.XML file.Now i open a string. XML file and paste that code  into string resource file.

XML Layout 

So we add Radio Group into this layout and set height and width and inside the Radio group we will  add three Radio button and  also add Edit text. purpose of add a Edit text is that.when user select a any one Radio option . Message show which option is selected.

Different property that are used in layout.xml file  explain here

  • warp_content   warp_content means height and width similar to the text that are entered.
  • Match_parent   means height and width similar to parent.
  • Radio button     Radio button allow to user to choice one option at  a same time to set predefined options
  • Edit text              show massage  which option select.

In this example we are going to create a simple UI that includes only (android Radio button ) with Different attributes.And paste the following code android Radio button using android into layout .XML file.

Now you may open the Graphical layout editor to preview the User Interface you created of Radio button  using android.

Alt Tag android Radio button

 

Main Activity

In main activity we will use a  switch case in which get id of each Radio button.It can divide into two alternative if and else condition when user can select a option then if condition will invoke.when user can not select a option then else condition will invoke.three cases we will use and every case will divide  into two alternative if and else.

Code that are placed in java class file of Radio button  using android

Run the application of Radio button using android

Alt tag android Radio button 1

Now when you can click first option then output show in below pic

Alt Tag output android radio button

Now when you can click  Last option then output show in below pic

Alt Tag output android radio button2

“Thank you”  for reading this lecture, Hope you got the idea.

  • charsi

    I am getting the following error:

    E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.cmmalli.helloworld, PID: 3823
    java.lang.IllegalStateException: Could not find method onRadioButtonClicked(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatRadioButton with id ‘radioButton15’
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
    at android.view.View.performClick(View.java:5610)
    at android.widget.CompoundButton.performClick(CompoundButton.java:122)
    at android.view.View$PerformClick.run(View.java:22260)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
    Application terminated.

    My Code is:

    public class MainActivity extends AppCompatActivity {

    EditText TotalBilleditText;
    EditText TipeditText;
    TextView textView5;
    Button btnAdd;
    RadioGroup rg;
    RadioButton radioButton15;

    double num1, num2, sum;
    double tax15, tax18, tax20, tax25;
    /**
    * ATTENTION: This was auto-generated to implement the App Indexing API.
    * See https://g.co/AppIndexing/AndroidStudio for more information.
    */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView5 = (TextView)findViewById(R.id.textView5);
    textView5.setEnabled(false);
    }

    public void selectTip(View view) {
    boolean checked = ((RadioButton) view).isChecked();
    switch (view.getId()) {
    case R.id.radioButton15:
    if (checked) {
    textView5.setText(“you select apple”);
    textView5.setEnabled(true);

    } else {
    textView5.setEnabled(false);
    }
    break;

    }

    }
    }

    Can you please guide me where am I going wrong? Thank you!