LinearLayout layout = (LinearLayout)findViewById( The value for this attribute must be the name of the method you want to call in response to a click event. optional sets that are mutually exclusive if you think that the user needs to see all available Content and code samples on this page are subject to the licenses described in the Content License. Kotlin setOnClickListener for Button Android Button widget is a UI element generally used to receive user actions as input. Generally, we can use RadioButton controls in an android application to allow users to select only one option from the set of values. Make sure you are getting the correct view's ID before implementing the onClick on that view. Android Radio Button attributes. Following is the pictorial representation of using RadioButton control in android applications. In general, we can use the RadioButton control in the Android app to allow users to select only one option from Values. Specifically, the method must: Tip: If you need to change the radio button state yourself, LinearLayout that has a vertical orientation by default. Create a new android application using android studio and give names as RadioButtonExample. It is used to set the padding from left, right, top and bottom. 4. It is used to specify the size of the text. As another quick Android example, this Java source code shows how to add a “click listener” (on click/tap event) to an Android Button:. This is how we can use RadioButton control in android applications to allow users to select only one option from a set of values based on our requirements. RadioGroup. Instead of passing an anonymous inner class to the setOnClickListener method, we will pass the activity itself and implement the OnClickListener interface into our MainActivity. In android, we can create RadioButton control in two ways either in the XML layout file or create it in the Activity file programmatically. RadioButton is generally used with RadioGroup. So, in activity_main.xml file, we have added two radio buttons inside a radio group. Following is the example of creating a RadioButton control dynamically in activity file. The event will give you access to the target, the radio button that raised the event, from there you can check its name to identify the group. − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to … package com.tutlane.radiobuttonexample; import; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RadioButton; import android.widget.Toast; public class MainActivity extends AppCompatActivity {     RadioButton android, java, angular, python;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         android = (RadioButton)findViewById(;         angular = (RadioButton)findViewById(;         java = (RadioButton)findViewById(;         python = (RadioButton)findViewById(;         Button btn = (Button)findViewById(;         btn.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 String result = "Selected Course: ";                 result+= (android.isChecked())?"Android":(angular.isChecked())?"AngularJS":(java.isChecked())?"Java":(python.isChecked())? The Activity hosting the layout must then implement the corresponding method. You can click on a Button, long press, etc. The user can press or click the radio button to select it. options side-by-side. For example, bold, italic, bolditalic etc. click event for both radio buttons: The method you declare in the android:onClick attribute In android, we can define RadioButton click event programmatically in Activity file rather than XML layout file. Here our xml file name is activity_main.xml so we used file name activity_main and we are getting the status of RadiButton controls when they Select / Deselect and getting selected RadioButton control value on Button click. To make click event work add android:onClick attribute to the Button element in your XML layout. : 2: Modify the default content of res/layout/activity_main.xml file to include Android UI control. Generally, whenever the user click on RadioButton to Select or Deselect the RadioButton object will receives an on-click event. Following is the example of defining a multiple RadioButton controls, one TextView control and one Button control in RelativeLayout  to get the selected values of RadioButton controls when we click on Button in the android application. element in your XML When the user clicks a button, the Button object receives an on-click event. to a click event. The example uses the CheckedChanged event to track which RadioButton is selected and reports the text of the selected RadioButton when the user clicks a Button. public void onRadioButtonClicked(View view) {     // Is the view now checked? It is used to control the visibility of control. Following are the some of commonly used attributes related to RadioButton control in android applications. So I recommend to refer official documentation for Android application development in case you are going to develop a sophisticated apps. Inherited from android.view.ViewClass − In this section, you will create two mutually-exclusive radio buttons (enabling one disables the other), using the RadioGroup and RadioButton widgets. For example, here are a couple RadioButton objects: Note: The RadioGroup is a subclass of The value for this attribute must be the name of the method you want to call in response to a click event. Tutlane 2020 | Terms and Conditions | Privacy Policy, "". By grouping them together, the system ensures that only one To define RadioButton click event programmatically, create View.OnClickListener object and assign it to the button by calling setOnClickListener(View.OnClickListener) like as shown below. To define the click event handler for a button, add the android:onClick attribute to the To define the click event handler for a button, add the android:onClick attribute to the element in your XML layout. The onchange event is not triggered when turning a radio button on and off but only one or the other. LinearLayout layout = (LinearLayout)findViewById(; RadioButton rd = new RadioButton(this); rd.setText("Tutlane"); rd.setChecked(true); layout.addView(rd); This is how we can define RadioButton in XML layout file or programmatically in activity file based on our requirements. Generally, during the launch of our activity, onCreate() callback method will be called by the android framework to get the required layout for an activity. "Python":"";                 Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();             }         });     }     public void onRadioButtonClicked(View view) {         boolean checked = ((RadioButton) view).isChecked();         String str="";         // Check which radio button was clicked         switch(view.getId()) {             case                 if(checked)                 str = "Android Selected";                 break;             case                 if(checked)                 str = "AngularJS Selected";                 break;             case                 if(checked)                 str = "Java Selected";                 break;             case                 if(checked)                 str = "Python Selected";                 break;         }         Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();     } }. Used to specify the current state of radio button: android:onClick: It’s a name of the method to invoke when the radio button clicked. Now open an activity_main.xml file from \res\layout path and write the code like as shown below,