Create A RadioGroup And Radio Button App In Android Application Using Android Studio

Introduction

In this article, I will explain how to create RadioGroup And RadioButton apps in Android applications using Android Studio.

Requirements

  • Android Studio2.1.3

If you want to create RadioGroup and RadioButton, you should follow the given steps.

Step 1

Now, open Android Studio and you can choose File >> New. Afterwards, choose New Project.

Android

Step 2

Here, you can create your application name and choose where your project is stored on the location. Click the NEXT button.

Android

Now, select Target Android devices.

Android

Step 3

Here, we can add the activity, more activity available, and click Next button.

Android

Now, we can write the activity name and click "Finish" button.

Android

Step 4

Now, open your project and go to activity_main.xml and afterwards, build the design. You should choose toolbox and if you want some option (RadioGroup, RadioButton, button), use the drag and drop method.

Android

Now, we can see the Graphical User Interface design.

Android

Step 5

Now, go to the resource folder and select the String.xml. Then, you will add the string value.

Android

srings.xml code

  1. <resources>  
  2.     <string name="app_name">RadioButtonApp</string>  
  3.     <string name="CPLUSPLUS">C++</string>  
  4.     <string name="JAVA">JAVA</string>  
  5.     <string name="CSHARP">C-SHARP</string>  
  6.     <string name="ASPNET">ASP.NET</string>  
  7.     <string name="ANDROID">ANDROID</string>  
  8. </resources>  
Step 6

Here, you need to build on the design and write the XML code.

activity_main.xml code.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" 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="xyz.rvconstructions.www.radiobuttonapp.MainActivity">  
  3.   
  4.     <RadioGroup android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_alignParentEnd="true">  
  5.   
  6.         <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C++" android:id="@+id/radioButton" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:textColor="#154" android:textSize="20sp" android:textStyle="bold" android:layout_gravity="center_horizontal" />  
  7.   
  8.         <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="JAVA" android:id="@+id/radioButton2" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:textColor="#1a5511" android:textSize="20sp" android:textStyle="bold" android:layout_gravity="center_horizontal" />  
  9.   
  10.         <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C-SHARP" android:id="@+id/radioButton3" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:textColor="#552711" android:textSize="20sp" android:textStyle="bold" android:layout_gravity="center_horizontal" />  
  11.   
  12.         <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ASP.NET" android:id="@+id/radioButton4" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:textColor="#112b55" android:textSize="20sp" android:textStyle="bold" android:layout_gravity="center_horizontal" />  
  13.   
  14.         <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ANDROID" android:id="@+id/radioButton5" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:textColor="#205511" android:textSize="20sp" android:textStyle="bold" android:layout_gravity="center_horizontal" />  
  15.   
  16.         <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="submit" android:id="@+id/button" android:layout_margin="20dp" android:background="#fbff00" android:padding="10dp" android:layout_gravity="center_horizontal" />  
  17.   
  18.     </RadioGroup>  
  19.   
  20. </RelativeLayout> // This is just a sample script. Paste your real code (javascript or HTML) here. if ('this_is'==/an_example/){of_beautifier();}else{var a=b?(c%d):e[f];}  
Step 7

Now, you will go to MainActivity.java page and build Java code.

First of all, declare a file that is an extension.

Android

Now, we can see MainActivity.java code.
  1. package xyz.rvconstructions.www.radiobuttonapp;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.Menu;  
  6. import android.view.MenuItem;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9.   
  10. import android.widget.RadioButton;  
  11.   
  12. import android.widget.Toast;  
  13.   
  14.   
  15. public class MainActivity extends AppCompatActivity {  
  16.     RadioButton cplus, java, csharp, aspnet, android;  
  17.     String selectedLanguage;  
  18.     Button submit;  
  19.   
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_main);  
  24.   
  25.   
  26.     }  
  27.   
  28.     @Override  
  29.     public boolean onCreateOptionsMenu(Menu menu) {  
  30.   
  31.         cplus = (RadioButton) findViewById(R.id.radioButton);  
  32.         java = (RadioButton) findViewById(R.id.radioButton2);  
  33.         csharp = (RadioButton) findViewById(R.id.radioButton3);  
  34.         aspnet = (RadioButton) findViewById(R.id.radioButton4);  
  35.         android = (RadioButton) findViewById(R.id.radioButton5);  
  36.         submit = (Button) findViewById(R.id.button);  
  37.         submit.setOnClickListener(new View.OnClickListener() {  
  38.             @Override  
  39.             public void onClick(View v) {  
  40.                 if (cplus.isChecked()) {  
  41.                     selectedLanguage = cplus.getText().toString();  
  42.                 } else if (java.isChecked()) {  
  43.                     selectedLanguage = java.getText().toString();  
  44.                 } else if (csharp.isChecked()) {  
  45.                     selectedLanguage = csharp.getText().toString();  
  46.                 } else if (aspnet.isChecked()) {  
  47.                     selectedLanguage = aspnet.getText().toString();  
  48.                 } else if (android.isChecked()) {  
  49.                     selectedLanguage = android.getText().toString();  
  50.                 }  
  51.                 Toast.makeText(getApplicationContext(), selectedLanguage, Toast.LENGTH_LONG).show();  
  52.             }  
  53.         });  
  54.         return true;  
  55.     }  
  56.   
  57. }  
Step 8

Here, go to run it. Select Run -> Run app option.

Android

Here, you will choose Emulator or the devices like Nokia Nokia _X in my case.

Android

Step 9

Here, look for the output.

Android

Now, select C-SHARP and click the "Submit" button.

Android

Now, select ASP.NET and click the submit button.

Android

Now, you can select Android and click the "Submit" button . See the below output.

Android