Wednesday 18 October 2017

15. Using RadioButton & RadioGroup in Android

15. Using RadioButton & RadioGroup

in Android


In Android, RadioButton are mainly used together in a RadioGroup. In RadioGroup checking the one radio button out of several radio button added in it will automatically unchecked all the others. It means at one time we can checked only one radio button from a group of radio buttons which belong to same radio group.

RadioButon is a two state button that can be checked or unchecked. If a radio button is unchecked then a user can check it by simply clicking on it. Once a RadiaButton is checked by user it can’t be unchecked by simply pressing on the same button. It will automatically unchecked when you press any other RadioButton within same RadioGroup.

RadioActivity.java

package tutorial.android.sachin4droid.androidtutorials;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

public class RadioActivity extends AppCompatActivity {
  RadioButton radio1,radio2,radio3;
  Button radioSubmit;

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

      radioSubmit = (Button) findViewById(R.id.radioSubmit);
      radio1 = (RadioButton) findViewById(R.id.radio1);
      radio2 = (RadioButton) findViewById(R.id.radio2);
      radio3 = (RadioButton) findViewById(R.id.radio3);
      radioSubmit.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              if(radio1.isChecked()){
                  Toast.makeText(RadioActivity.this, radio1.getText() +" is selected", Toast.LENGTH_SHORT).show();

              }
              if(radio2.isChecked()){
                  Toast.makeText(RadioActivity.this, radio2.getText() +" is selected", Toast.LENGTH_SHORT).show();

              }
              if(radio3.isChecked()){
                  Toast.makeText(RadioActivity.this, radio3.getText() +" is selected", Toast.LENGTH_SHORT).show();

              }

          }
      });



  }
}


Activity_radio.xml

<?xml version="1.0" encoding="utf-8"?>
<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="tutorial.android.sachin4droid.androidtutorials.RadioActivity">

  <RadioGroup
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      tools:layout_editor_absoluteX="103dp"
      tools:layout_editor_absoluteY="94dp" >

      <RadioButton
          android:id="@+id/radio1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="radio1"/>
      <RadioButton
          android:id="@+id/radio2"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="radio2"/>
      <RadioButton
          android:id="@+id/radio3"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="radio3"/>

      </RadioGroup>

  <Button
      android:id="@+id/radioSubmit"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Submit"/>
</LinearLayout>


No comments:

Post a Comment

Extract error records while inserting into db table using JDBCIO apache beam in java

 I was inserting data into postgres db using apache beam pipeline. it works perfectly with JdbcIO write of apache beam library. But, now, i ...