harian untung99play.xyz

untung99play.xyz: RadioButton RadioGroup Tutorial With Example In Android Studio


Untung99 menawarkan beragam permainan yang menarik, termasuk slot online, poker, roulette, blackjack, dan taruhan olahraga langsung. Dengan koleksi permainan yang lengkap dan terus diperbarui, pemain memiliki banyak pilihan untuk menjaga kegembiraan mereka. Selain itu, Untung99 juga menyediakan bonus dan promosi menarik yang meningkatkan peluang kemenangan dan memberikan nilai tambah kepada pemain.

Berikut adalah artikel atau berita tentang Harian untung99play.xyz dengan judul untung99play.xyz: RadioButton RadioGroup Tutorial With Example In Android Studio yang telah tayang di untung99play.xyz terimakasih telah menyimak. Bila ada masukan atau komplain mengenai artikel berikut silahkan hubungi email kami di koresponden@untung99play.xyz, Terimakasih.


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. The most common use of radio button is in Quiz Android App code.

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.

Important Note: RadioGroup is a widget used in Android for the grouping of radio buttons and provide the feature of selecting only one radio button from the set. When a user try to select any other radio button within same radio group the previously selected radio button will be automatically unchecked.

RadioGroup And RadioButton code in XML:

Checking Current State Of Radio Button:

You can check the current state of a radio button programmatically by using isChecked() method. This method returns a Boolean value either true or false. if it is checked then returns true otherwise returns false. Below is an example code with explanation in which we checked the current state of a radio button.

/*Add in Oncreate() funtion after setContentView()*/
RadioButton simpleRadioButton = (RadioButton) findViewById(R.id.simpleRadioButton); // initiate a radio button

Boolean RadioButtonState = simpleRadioButton.isChecked(); // check current state of a radio button (true or false).

Attributes of RadioButton In Android:

Now let’s  we discuss important attributes that helps us to create a beautiful radio button in xml file (layout).

1. id: id is an attribute used to uniquely identify a radio button.

2. checked: checked attribute in radio button is used to set the current state of a radio button. We can set it either true or false where true shows the checked state and false shows unchecked state of a radio button. As usual default value of checked attribute is false. We can also set the current state in JAVA.

Below we set true value for checked attribute which sets the current state to checked of a Button

Setting checked State Of RadioButton In Java Class:

Below code set the current state of RadioButton to checked programmatically.

/*Add in Oncreate() funtion after setContentView()*/
// initiate a radio button
RadioButton simpleRadioButton = (RadioButton) findViewById(R.id.simpleRadioButton);

// set the current state of a radio button
simpleRadioButton.setChecked(true);

3. text: text attribute is used to set the text in a radio button. We can set the text both ways either in XML or in JAVA class.

Below is the example code with explanation included in which we set the text “I am a radiobutton” of a  radio button.

Setting text of RadioButton In Java class:

Below we set the text of a radio button programmatically:

/*Add in Oncreate() funtion after setContentView()*/
RadioButton simpleRadioButton=(RadioButton) findViewById(R.id.simpleRadioButton);
simpleRadioButton.setText("I am a radiobutton"); // displayed text of radio button

4. gravity: The gravity attribute is an optional attribute which is used to control the alignment of text like left, right, center, top, bottom, center_vertical, center_horizontal etc.

Below is the example code with explanation included in which we set the center gravity for the text of a radio button.

5. textColor: textColor attribute is used to set the text color of a radio button. Color value is in the form of “#argb”, “#rgb”, “#rrggbb”, or “#aarrggbb”.

Below we set the red color for the displayed text of a radio button.

 
Setting textColor of RadioButton text In Java class:

Below we set the text color of a radio button programmatically.

/*Add in Oncreate() funtion after setContentView()*/
RadioButton simpleRadioButton = (RadioButton) findViewById(R.id.simpleRadioButton);// initiate radio button

simpleRadioButton.setTextColor(Color.RED); //red color for displayed text of radio button

6. textSize: textSize attribute is used to set the size of the text of a radio button. We can set the text size in sp(scale independent pixel) or dp(density pixel).

Below we set the 25sp size for the text of a radio button.

Setting textSize Of RadioButton Text In Java class:

Below we set the text size of a radio button programmatically:

/*Add in Oncreate() funtion after setContentView()*/
RadioButton simpleRadioButton = (RadioButton) findViewById(R.id.simpleRadioButton); // initiate radio button

simpleRadioButton.setTextSize(25); // set 25sp displayed text size of radio button

7. textStyle: textStyle attribute is used to set the text style of the text of a radio button. The possible text styles are bold, italic and normal. If we need to use two or more styles for a text view then “|” operator is used for that.

Below is the example code with explanation included in which we set the bold and italic text styles for text of a radio button.

8. background: background attribute is used to set the background of a radio button. We can set a color or a drawable in the background of a radio button.

Below we set the black color for the background and red color for the displayed text of a radio button.

Setting Background Of RadioButton In Java class:

Below we set the background color of a radio button programmatically.

/*Add in Oncreate() funtion after setContentView()*/
RadioButton simpleRadioButton = (RadioButton) findViewById(R.id.simpleRadioButton);

simpleRadioButton.setBackgroundColor(Color.BLACK);

9. padding: padding attribute is used to set the padding from left, right, top or bottom.

  • paddingRight: set the padding from the right side of the radio button.
  • paddingLeft : set the padding from the left side of the radio button.
  • paddingTop : set the padding from the top side of the radio button.
  • paddingBottom: set the padding from the bottom side of the radio button.
  • Padding: set the padding from the all side’s of the radio button.

Below we set padding attribute of 20dp padding from all the side’s of the radio button.

10. drawableBottom, drawableTop, drawableLeft And drawableRight: These attribute draw the drawable to the below of the text of RadioButton.

Below we set the icon to the right of the text of a RadioButton.


Example Of RadioButton And RadioGroup in Android Studio:

Below is the example of Radiobutton in Android where we display five radio buttons with background and other attributes. The radio buttons are used to select your favorite WWE superstar with one “submit” button. Below is the final output, download code and step by step explanation of tutorial:

Download Code ?

Step 1: Create a new project and name it RadioButtonExample

Select File -> New -> New Project and Fill the forms and click “Finish” button.

Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code:

In this step we open an xml file and add the code for displaying 5 RadioButton and one normal button.

Step 3: Open  src -> package -> MainActivity.java

In this step we open MainActivity and add the code to initiate the RadioButton and normal button. We also perform click event on button and display the selected superstar’s name by using a Toast.

package example.gb.radiobuttonexample;

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 MainActivity extends AppCompatActivity {

    RadioButton johnCena, randyOrton, goldBerg, romanReigns, sheamus;
    String selectedSuperStar;
    Button submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        johnCena = (RadioButton) findViewById(R.id.johnCena);
        randyOrton = (RadioButton) findViewById(R.id.randyOrton);
        goldBerg = (RadioButton) findViewById(R.id.goldBerg);
        romanReigns = (RadioButton) findViewById(R.id.romanReigns);
        sheamus = (RadioButton) findViewById(R.id.sheamus);
        submit = (Button) findViewById(R.id.submitButton);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (randyOrton.isChecked()) {
                    selectedSuperStar = randyOrton.getText().toString();
                } else if (sheamus.isChecked()) {
                    selectedSuperStar = sheamus.getText().toString();
                } else if (johnCena.isChecked()) {
                    selectedSuperStar = johnCena.getText().toString();
                } else if (romanReigns.isChecked()) {
                    selectedSuperStar = romanReigns.getText().toString();
                } else if (goldBerg.isChecked()) {
                    selectedSuperStar = goldBerg.getText().toString();
                }
                Toast.makeText(getApplicationContext(), selectedSuperStar, Toast.LENGTH_LONG).show(); // print the value of selected super star
            }
        });
    }
}

Step 4: Open res -> values -> strings.xml

In this step we open String file which is used to store string data of the app. 


    RadioButtonExample
    Hello world!
    Settings
    Randy Orton
    John Cena
    Roman Reigns
    Gold Berg
    Sheamus

 Run The App:

Now run the App in Emulator and you will see 5 RadioButton in RadioGroup listing WWE superstar name. Now choose your favorite one and click on Submit Button. The name will be displayed on Screen.

Also check: Quiz Android App Source Code

This free eBook will help you master the learning of Android App Development in Android Studio!


  1. in edit text if i enter only space in edit text field then there must not show any toast message . what i should do/????

  2. How to get and set rediobutton value from json data two value from radio button male and female

  3. If I have used more then two radio group, how do I check and print all checked options on the button click?

  4. Really nice teaching, As a beginner I enjoyed your way of teaching

  5. How do you align radio buttons in this order:

    button 1 button 6
    button 2 button 7
    button 3 button 8
    button 4 button 9
    button 5 button 10
    ?????




  6. Great tutorial. Lifesaver. My Andriod Studio book is HORRIBLE.

    I have a question. Rather than determine 50 strings to assign to each button, how can you create an array of names, eg. and assign array[0] to radioButton1, array[1] to radioButton2, etc.? What is the syntax?

  7. thank you, How can we change status of Radio button from Main java file? how to store button state

  8. expalation is very nice n in simple lang. gd job Abhi

  9. could u writte the total android in hindi language…
    i am not understand in so many difficult english word..

    1. Not possible now. Try practising side by side read and concept will clear.

  10. how to add image and array text and radio button single choice in listview

Your email address will not be published. Required fields are marked *

Continue Reading: