Implementing Android Radio Button To Enhance App User-Interface

0
Shares
Implementing Android Radio Button To Enhance App User-Interface
5 (100%) 1 vote

android radio button

An Android radio button is generally a two-state button which can be either unchecked or checked. When it is unchecked, a user can click to check it. These radio buttons are usually used together as a RadioGroup. And when these radio buttons live inside one radio group, checking one of the radio button unchecks the rest of them.

In simple terms, Android radio buttons are arranged in groups of two or more and displayed on screen as, for example, a list of circular holes that can contain white space (for unselected) or a dot (for selected). Each radio button is normally accompanied by a label describing the choice that the radio button represents. The choices are mutually exclusive; when the user selects a radio button, any previously selected radio button in the same group becomes deselected (making it so only one can be selected).

In today’s Android app tutorial, we’ll learn the complete process of radio button integration with Android radio button example.

Let’s Get Started

Create a new project under file menu and modify the project details.

Click on next and choose Mini Support SDK for this project.

ssradiobuttondemo2

In the next tab, select Add No Activity.

ssradiobuttondemo3

Now create a XML file and use custom view button in it.

ssradiobuttondemo4

Now, we’ll create a class for MainActivity.

public class MainActivity extends AppCompatActivity{

    private RadioGroup radioGroupOpinion;

private RadioButton radioPoll;

private Button btnResult;

@Override

protected void onCreate(@Nullable Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    radioGroupOpinion = (RadioGroup) findViewById(R.id.radioOpinion);

    btnResult = (Button) findViewById(R.id.btnResult);

    btnResult.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View view) {

            // get selected radio button from radioGroup

            int selectedId = radioGroupOpinion.getCheckedRadioButtonId();

            // find the radiobutton by returned id

            radioPoll = (RadioButton) findViewById(selectedId);

            if(radioPoll.getText().toString().equalsIgnoreCase("Yes")) {

                Toast.makeText(MainActivity.this,

                        "Aha..! Thank you very much..!!", Toast.LENGTH_SHORT).show();

            }else{

                Toast.makeText(MainActivity.this,

                        "Ohh...What is the problem?", Toast.LENGTH_SHORT).show();

            }

        }

    });

}

}

Next, Manage the click event in Main Class.

btnResult.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View view) {

        // get selected radio button from radioGroup

        int selectedId = radioGroupOpinion.getCheckedRadioButtonId();

        // find the radiobutton by returned id

        radioPoll = (RadioButton) findViewById(selectedId);

        if(radioPoll.getText().toString().equalsIgnoreCase("Yes")) {

            Toast.makeText(MainActivity.this,

                    "Aha..! Thank you very much..!!", Toast.LENGTH_SHORT).show();

        }else{

            Toast.makeText(MainActivity.this,

                    "Ohh...What is the problem?", Toast.LENGTH_SHORT).show();

        }

    }

});

After managing click event, it’s time to run the demo.

ssradiobuttondemo5

Select one by one radio button and click on OK button.

ssradiobuttondemo6

ssradiobuttondemo7

And Done!

Simple, right? However, you can also enhance the user experience by implementing custom Android radio button in an Android app. And in case, you’re building a mobile app from scratch, hire Android app development company which can guide you through the process for building a successful mobile app.

Grab a free copy of radiobutton demo from Github.

 
0
Shares
 

LET'S TALK VALIDATE YOUR IDEA!