Working with android RadioButtons

Today I’m going to explain the process of developing an android program. For the first example I will take a pretty simple quiz-test application. I assume that you are pretty acknowledged with eclipse or any other IDE and have installed ADT on your computer, besides on internet it’s a lot of information and lessons like how to build a “hello world” app.

Right now I’m eating peaches, so sorry if you find some mistakes…

But let’s start

– below is my requirement:

  • First window will list questions and multiple choices of answers (radio button like)
  • Nice background with scrolling list
  • Second window with the results calculated by the app
  • Options menu for “new test” , “exit” and other stuff


Our look of the program. As I mentioned, we need a background and some radiobuttons on it. Throw views like imageview, radiobuttons and because we got a lot of content – scrollview in main.xml:

Tip: you can use file valuesstrings.xml for “android:text=”, this will allow in future to modify easily your app.

For the results screen we can create a new activity or do all actions in one activity with “viewflipper”. Android viewflipper is used to add multiple views in a single Flipper – for more explained details look at this example. So now we have “LinearLayout01” and “LinearLayout02” respectively for the first and second window(Fig.1).

First layout should have ScrollView containing the entire contents to scroll (Fig.2) (LinearLayout with Radiobuttons included in Radiogroups):

And add new linearlayout and textview for the second window.





Activity JavaCode

Now, let’s start with java coding. For my example I have 23 question with 4 possible answers each. At the end my application must calculate the most common response and depending on it to display that result on the screen. For answer calc. we have

int rasp[]={0,0,0,0};

and assign listener on each radiogroup we have

for ( int i=1;i<23;i++) {

String radiogroupID = "radioGroup" + i;

int gresID = getResources().getIdentifier(radiogroupID, "id", "com.samples.testbaiat");

RadioGroup radioGroup = (RadioGroup) findViewById(gresID);

radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() }

Use case operator for the buttons and increment our variables respectively. At the end find the max value and get the results on screen with viewflipper.

public void onClick(View view) {

// Get the ViewFlipper from the layout

ViewFlipper vf = (ViewFlipper) findViewById(;

// Set an animation from res/

vf.setAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.push_left_in));


//print variables on screen

TextView t2=(TextView) findViewById(;

if (rasp[0]==getMaxValue(rasp)) {


} else if (rasp[1]==getMaxValue(rasp)) {


} else if (rasp[2]==getMaxValue(rasp)) {


} else if (rasp[3]==getMaxValue(rasp)) {




For the options menu use:


public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

//"new" option


Intent myIntent = new Intent(TestBaiatActivity.this, TestBaiatActivity.class);




//"exit option





return true;



Output screen:

Of course there are still of plenty room to work with database, saved parameters, loading data from the web, online score screen etc…

But hope you enjoyed this simplest tutorial.

Filed under: android. Bookmark the permalink.

3 Responses to Working with android RadioButtons

  1. Lindsey Hindall says:

    This is really interesting, You are a very skilled blogger. I have joined your rss feed and look forward to seeking more of your excellent post. Also, I have shared your web site in my social networks!

  2. Rachele Andrion says:

    I got what you intend, thankyou for putting up.

  3. Cynthia says:

    5 Star Work!

Speak Your Mind

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