Tuesday, March 16, 2021

Custom RadioButton

Android Custom RadioButton

Rather than default user interface of android RadioButton, we can also implement a custom radio button. Custom RadioButton makes user interface more attractive.

Example of Custom RadioButton

Let's see an example of custom RadioButton.

activity_main.xml

File: activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:paddingBottom="@dimen/activity_vertical_margin"  
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.     android:paddingRight="@dimen/activity_horizontal_margin"  
  10.     android:paddingTop="@dimen/activity_vertical_margin"  
  11.     android:orientation="vertical"  
  12.     tools:context="com.example.test.customradiobutton.MainActivity">  
  13.   
  14.   
  15.   
  16.     <TextView  
  17.         android:id="@+id/tv"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_marginTop="30dp"  
  21.         android:gravity="center_horizontal"  
  22.         android:textSize="25dp"  
  23.         android:text="Customized Radio Buttons" />  
  24.   
  25.   
  26.     <!--   Customized RadioButtons  -->  
  27.   
  28.   
  29.     <RadioGroup  
  30.         android:layout_width="wrap_content"  
  31.         android:layout_height="wrap_content"  
  32.         android:id="@+id/radioGroup">  
  33.   
  34.         <RadioButton  
  35.             android:id="@+id/radioMale"  
  36.             android:layout_width="fill_parent"  
  37.             android:layout_height="wrap_content"  
  38.             android:text="  Male"  
  39.             android:layout_marginTop="10dp"  
  40.             android:checked="false"  
  41.             android:button="@drawable/custom_radio_button"  
  42.             android:textSize="20dp" />  
  43.   
  44.         <RadioButton  
  45.             android:id="@+id/radioFemale"  
  46.             android:layout_width="fill_parent"  
  47.             android:layout_height="wrap_content"  
  48.             android:text="   Female"  
  49.             android:layout_marginTop="20dp"  
  50.             android:checked="false"  
  51.             android:button="@drawable/custom_radio_button"  
  52.             android:textSize="20dp" />  
  53.     </RadioGroup>  
  54.   
  55.     <Button  
  56.         android:layout_width="wrap_content"  
  57.         android:layout_height="wrap_content"  
  58.         android:text="Show Selected"  
  59.         android:id="@+id/button"  
  60.         android:onClick="onclickbuttonMethod"  
  61.         android:layout_gravity="center_horizontal" />  
  62.   
  63. </LinearLayout>  

custom_radio_button.xml

Now implement a selector in another file (custom_radio_button.xml) in drawable and place two different checked and unchecked button images.

File: checkbox.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  4.   
  5.     <item android:state_checked="true" android:drawable="@drawable/checkedradiobutton" />  
  6.     <item android:state_checked="false" android:drawable="@drawable/unchekedradiobutton" />  
  7.   
  8. </selector>  

Activity class

File: MainActivity.java

  1. package com.example.test.customradiobutton;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.RadioButton;  
  8. import android.widget.RadioGroup;  
  9. import android.widget.Toast;  
  10.   
  11. public class MainActivity extends AppCompatActivity {  
  12.     Button button;  
  13.     RadioButton genderradioButton;  
  14.     RadioGroup radioGroup;  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.   
  20.         radioGroup=(RadioGroup)findViewById(R.id.radioGroup);  
  21.     }  
  22.   
  23.   
  24.     public void onclickbuttonMethod(View v){  
  25.         int selectedId = radioGroup.getCheckedRadioButtonId();  
  26.         genderradioButton = (RadioButton) findViewById(selectedId);  
  27.         if(selectedId==-1){  
  28.             Toast.makeText(MainActivity.this,"Nothing selected", Toast.LENGTH_SHORT).show();  
  29.         }  
  30.         else{  
  31.             Toast.makeText(MainActivity.this,genderradioButton.getText(), Toast.LENGTH_SHORT).show();  
  32.         }  
  33.   
  34.     }  
  35. }  

Output

android Custom Radio Button 1
android Custom Radio Button 2

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile