Introduction
In this article, I will explain how to create RadioGroup And RadioButton apps in Android applications using Android Studio.
Requirements
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.
Step 2
Here, you can create your application name and choose where your project is stored on the location. Click the NEXT button.
Now, select Target Android devices.
Step 3
Here, we can add the activity, more activity available, and click Next button.
Now, we can write the activity name and click "Finish" button.
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.
Now, we can see the Graphical User Interface design.
Step 5
Now, go to the resource folder and select the String.xml. Then, you will add the string value.
srings.xml code
- <resources>
- <string name="app_name">RadioButtonApp</string>
- <string name="CPLUSPLUS">C++</string>
- <string name="JAVA">JAVA</string>
- <string name="CSHARP">C-SHARP</string>
- <string name="ASPNET">ASP.NET</string>
- <string name="ANDROID">ANDROID</string>
- </resources>
Step 6
Here, you need to build on the design and write the XML code.
activity_main.xml code.
- <?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: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">
-
- <RadioGroup android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_alignParentEnd="true">
-
- <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" />
-
- <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" />
-
- <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" />
-
- <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" />
-
- <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" />
-
- <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" />
-
- </RadioGroup>
-
- </RelativeLayout>
Step 7
Now, you will go to MainActivity.java page and build Java code.
First of all, declare a file that is an extension.
Now, we can see MainActivity.java code.
- package xyz.rvconstructions.www.radiobuttonapp;
-
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
-
- import android.widget.RadioButton;
-
- import android.widget.Toast;
-
-
- public class MainActivity extends AppCompatActivity {
- RadioButton cplus, java, csharp, aspnet, android;
- String selectedLanguage;
- Button submit;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
-
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- cplus = (RadioButton) findViewById(R.id.radioButton);
- java = (RadioButton) findViewById(R.id.radioButton2);
- csharp = (RadioButton) findViewById(R.id.radioButton3);
- aspnet = (RadioButton) findViewById(R.id.radioButton4);
- android = (RadioButton) findViewById(R.id.radioButton5);
- submit = (Button) findViewById(R.id.button);
- submit.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (cplus.isChecked()) {
- selectedLanguage = cplus.getText().toString();
- } else if (java.isChecked()) {
- selectedLanguage = java.getText().toString();
- } else if (csharp.isChecked()) {
- selectedLanguage = csharp.getText().toString();
- } else if (aspnet.isChecked()) {
- selectedLanguage = aspnet.getText().toString();
- } else if (android.isChecked()) {
- selectedLanguage = android.getText().toString();
- }
- Toast.makeText(getApplicationContext(), selectedLanguage, Toast.LENGTH_LONG).show();
- }
- });
- return true;
- }
-
- }
Step 8
Here, go to run it. Select Run -> Run app option.
Here, you will choose Emulator or the devices like Nokia Nokia _X in my case.
Step 9
Here, look for the output.
Now, select C-SHARP and click the "Submit" button.
Now, select ASP.NET and click the submit button.
Now, you can select Android and click the "Submit" button . See the below output.