Thursday, April 4, 2019

RadioButton and RadioGroup in Android App | 2019 | Android Buddy

RadioButton and RadioGroup:-





Making a radio button in android app is the most important part of any android app. Whenever you create a sign-up form or any registration form there is always a use of radio button. So, in this documentation, we will make some radio button and also see the output message when a user clicks or check any radio button.

Follow the below steps:-

  • Step1:- Create a new project and named that project RadioExample.
  • Step2:- Open your activity_main.xml layout file.
  • Step3:- Make some change like I have done!. copy and paste the below code:-
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"    
 xmlns:app="http://schemas.android.com/apk/res-auto"    
 xmlns:tools="http://schemas.android.com/tools"    
 android:layout_width="match_parent"    
 android:layout_height="match_parent"    
 android:orientation="vertical"    
 tools:context=".MainActivity">
    
  <RadioGroup        
     android:orientation="vertical"        
     android:layout_width="wrap_content"        
     android:layout_height="wrap_content">
        <RadioButton
             android:onClick="ClickedItem"            
             android:text="@string/radio1"            
             android:id="@+id/r1"            
             android:layout_width="wrap_content"            
             android:layout_height="wrap_content" />


        <RadioButton            
            android:onClick="ClickedItem"            
            android:text="@string/radio2"            
            android:id="@+id/r2"            
            android:layout_width="wrap_content"            
            android:layout_height="wrap_content" />

        <RadioButton            
             android:onClick="ClickedItem"            
             android:text="@string/radio3"            
             android:id="@+id/r3"            
             android:layout_width="wrap_content"            
            android:layout_height="wrap_content" />

        <RadioButton            
           android:onClick="ClickedItem"            
           android:text="@string/radio4"            
           android:id="@+id/r4"            
           android:layout_width="wrap_content"            
           android:layout_height="wrap_content" />

        <RadioButton            
          android:onClick="ClickedItem"            
          android:text="@string/radio5"            
          android:id="@+id/r5"            
          android:layout_width="wrap_content"            
          android:layout_height="wrap_content" />

    </RadioGroup>


</LinearLayout>

  • Step1:- Yup! you are doing good, Now its time to make those radio buttons to do some actions. Copy the below code and paste it inside you MainActivity.java file.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

    public void ClickedItem(View v){
         if (v.getId()==R.id.r1){
             Toast.makeText(getApplicationContext(),getString(R.string.radio1),Toast.LENGTH_SHORT).show();
         }
        if (v.getId()==R.id.r2){
            Toast.makeText(getApplicationContext(),getString(R.string.radio2),Toast.LENGTH_SHORT).show();
        }
        if (v.getId()==R.id.r3){
            Toast.makeText(getApplicationContext(),getString(R.string.radio3),Toast.LENGTH_SHORT).show();
        }
        if (v.getId()==R.id.r4){
            Toast.makeText(getApplicationContext(),getString(R.string.radio4),Toast.LENGTH_SHORT).show();
        }

        if (v.getId()==R.id.r5){
            Toast.makeText(getApplicationContext(),getString(R.string.radio5),Toast.LENGTH_SHORT).show();
        }
    }
}

  • Now run your app.
So, as you can see we have a method named as ClickedItem(). This method this called whenever a radio button is clicked. For checking which radio is been clicked at a time we got to check radio button id and when a click event is performed we just toast a message.




For setting any string value we just created an R.string.radio1 as a name of radiolabel text. The getString() method provide a string value. Passing the string name inside the getString() method will return String value.

Here, we used v.getId() method, this method is used whenever we need any id of the selected view from the XML layout.

It`s just very simple and easy to make a radio button inside the android app. :D

Thanks! for your support.please do not forget to visit:- Android Buddy


No comments:

Post a Comment