Tuesday, March 16, 2021

Dynamic RadioButton

Android Dynamic RadioButton

Instead of creating RadioButton through drag and drop from palette, android also facilitates you to create it programmatically (dynamically). For creating dynamic RadioButton, we need to useandroid.view.ViewGroup.LayoutParams which configures the width and height of views and implementssetOnCheckedChangeListener() method of RadioGroup class.

Example of Dynamic RadioButton

Let's see an example of Dynamic RadioButton.

activity_main.xml

File: activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     android:id="@+id/relativeLayout"  
  11.     tools:context="com.example.test.dynamicradiobutton.MainActivity">  
  12.   
  13. </RelativeLayout>  

Activity class

File: MainActivity.java

  1. package com.example.test.dynamicradiobutton;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.widget.RadioButton;  
  6. import android.widget.RadioGroup;  
  7. import android.widget.RelativeLayout;  
  8.   
  9. import android.widget.RelativeLayout.LayoutParams;  
  10. import android.widget.Toast;  
  11.   
  12. public class MainActivity extends AppCompatActivity {  
  13.     RadioGroup rg;  
  14.     RelativeLayout rl;  
  15.     RadioButton rb1,rb2;  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.   
  22.         rg = new RadioGroup(this);  
  23.         rl = (RelativeLayout) findViewById(R.id.relativeLayout);  
  24.         rb1 = new RadioButton(this);  
  25.         rb2 = new RadioButton(this);  
  26.   
  27.         rb1.setText("Male");  
  28.         rb2.setText("Female");  
  29.         rg.addView(rb1);  
  30.         rg.addView(rb2);  
  31.         rg.setOrientation(RadioGroup.HORIZONTAL);  
  32.   
  33.         RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);  
  34.         params.leftMargin =150;  
  35.         params.topMargin = 100;  
  36.   
  37.         rg.setLayoutParams(params);  
  38.         rl.addView(rg);  
  39.   
  40.         rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
  41.             @Override  
  42.             public void onCheckedChanged(RadioGroup group, int checkedId) {  
  43.                 RadioButton radioButton = (RadioButton) findViewById(checkedId);  
  44.                 Toast.makeText(getApplicationContext(),radioButton.getText(),Toast.LENGTH_LONG).show();  
  45.             }  
  46.         });  
  47.     }  
  48. }  

android Dynamically Radio Button 1
android Dynamically 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