Basics for Using Radio Buttons + Buttons + Message Box

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Basics for Using Radio Buttons + Buttons + Message Box

Postby code » Tue Feb 12, 2008 10:32 am

[TinyTut] - Basics for Using Radio Buttons + Buttons + Message Box


What you learn: You are going learn how to use radio buttons, radio group, buttons and Action Listeners for them. Also you are going to learn how to display a MessageBox ^_^


Difficulty: 0 of 5

:?: The Problem:

in this tutorial you are going to implement a solution for the following prototype :arrow:
[align=center]

Image[/align]


:idea: The Solution:

Step 1: Define your UI elements in your XML layout file


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2.  
  3.  
  4. <?xml version="1.0" encoding="utf-8"?>
  5.  
  6. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  7.  
  8.         android:orientation="vertical" android:layout_width="fill_parent"
  9.  
  10.         android:layout_height="fill_parent">
  11.  
  12.         <RadioGroup android:layout_width="fill_parent"
  13.  
  14.                 android:layout_height="wrap_content" android:orientation="vertical"
  15.  
  16.                 id="@+id/group1">
  17.  
  18.                 <TextView android:layout_width="fill_parent"
  19.  
  20.                         android:layout_height="wrap_content"
  21.  
  22.                         android:text="Q1: What is Android?" android:textStyle="bold" />
  23.  
  24.                 <RadioButton android:checked="false"
  25.  
  26.                         android:text="@string/radio_group_1_option1" id="@+id/option1" />
  27.  
  28.                 <RadioButton android:checked="false"
  29.  
  30.                         android:text="@string/radio_group_1_option2" id="@+id/option2" />
  31.  
  32.                 <RadioButton android:checked="false"
  33.  
  34.                         android:text="@string/radio_group_1_option3" id="@+id/option3" />
  35.  
  36.         </RadioGroup>
  37.  
  38.         <LinearLayout android:orientation="horizontal"
  39.  
  40.                 android:layout_width="fill_parent"
  41.  
  42.                 android:layout_height="fill_parent">
  43.  
  44.                 <Button android:layout_width="100px"
  45.  
  46.                         android:layout_height="wrap_content" android:text="Answer"
  47.  
  48.                         id="@+id/answer" />
  49.  
  50.  
  51.  
  52.                 <Button android:layout_width="100px"
  53.  
  54.                         android:layout_height="wrap_content" android:text="Clear"
  55.  
  56.                         id="@+id/clear" />
  57.  
  58.         </LinearLayout>
  59.  
  60. </LinearLayout>
  61.  
  62.  
  63.  
  64.  
Parsed in 0.005 seconds, using GeSHi 1.0.8.4



Step 2: Define your string variables that was used in your XML layout file in the previous step

here I tried to define some string variables, to see how we can use the variables in our Android XML files :D , which was easy.
The String variables are defined in String.xml, which is located in res/values directory. :arrow:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2.  
  3.  
  4.  
  5. <?xml version="1.0" encoding="utf-8"?>
  6.  
  7. <resources>
  8.  
  9.     <string name="app_name">Exercise 1</string>
  10.  
  11.     <string name="radio_group_1_option1">Operating System</string>    
  12.  
  13.     <string name="radio_group_1_option2">Mobile Platform</string>    
  14.  
  15.     <string name="radio_group_1_option3">Development Tool</string>    
  16.  
  17. </resources>
  18.  
  19.  
  20.  
  21.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4




Step 3: Program your solution :lol:

After we design our User Interface, we can start with our solution logic. Your project Java file contains all the logic behind your solution. The Solution for this problem can be done in diffrent ways.
Here you my solution for the problem :) ... I know its not perfect :wink: but am learning Android basics now

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. import android.app.Activity;
  5.  
  6. import android.os.Bundle;
  7.  
  8. import android.view.View;
  9.  
  10. import android.widget.Button;
  11.  
  12. import android.widget.RadioButton;
  13.  
  14. import android.widget.RadioGroup;
  15.  
  16.  
  17.  
  18. public class Exercise1 extends Activity implements
  19.  
  20.                 RadioGroup.OnCheckedChangeListener, View.OnClickListener {
  21.  
  22.         /** Called when the activity is first created. */
  23.  
  24.         protected static int ans = -1;
  25.  
  26.         protected static int choice = -2;
  27.  
  28.         protected static RadioGroup mRadioGroup;
  29.  
  30.  
  31.  
  32.         @Override
  33.  
  34.         public void onCreate(Bundle icicle) {
  35.  
  36.                 super.onCreate(icicle);
  37.  
  38.                 setContentView(R.layout.main);
  39.  
  40.                 // creating references to the UI Commands
  41.  
  42.                 mRadioGroup = (RadioGroup) findViewById(R.id.group1);
  43.  
  44.                 RadioButton r2 = (RadioButton) findViewById(R.id.option2);
  45.  
  46.                 Button clearButton = (Button) findViewById(R.id.clear);
  47.  
  48.                 Button answerButton = (Button) findViewById(R.id.answer);
  49.  
  50.                 // save the correct answer id
  51.  
  52.                 ans = r2.getId();
  53.  
  54.                 mRadioGroup.setOnCheckedChangeListener(this);
  55.  
  56.                 clearButton.setOnClickListener(this);
  57.  
  58.                 answerButton.setOnClickListener(new Button.OnClickListener() {
  59.  
  60.                         public void onClick(View v) {
  61.  
  62.                                 showAlert(
  63.  
  64.                                                 "Result",
  65.  
  66.                                                 (Exercise1.choice == Exercise1.ans) ? "Yes, you are right"
  67.  
  68.                                                                 : "No, your answer is worgn ", "Close", true);
  69.  
  70.                         }
  71.  
  72.                 });
  73.  
  74.         }
  75.  
  76.  
  77.  
  78.         @Override
  79.  
  80.         public void onCheckedChanged(RadioGroup arg0, int checkedId) {
  81.  
  82.                 choice = checkedId;
  83.  
  84.         }
  85.  
  86.  
  87.  
  88.         @Override
  89.  
  90.         public void onClick(View arg0) {
  91.  
  92.                 mRadioGroup.clearCheck();
  93.  
  94.         }
  95.  
  96. }
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4



What it will look like:


[align=center]
Image


Image[/align]


This was my first tutorial :D hope it will be useful for bigenners -- like ME :lol:
Your feedbacks & comments are very welcome

Regards,
code
User avatar
code
Junior Developer
Junior Developer
 
Posts: 21
Joined: Sat Feb 09, 2008 6:24 pm
Location: UAE

Top

Postby plusminus » Tue Feb 12, 2008 10:52 am

Hello code,

I like it. It seems useful for learning UI basics, maybe I can use derived code from it in the Android-book I am writing :?:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby code » Tue Feb 12, 2008 11:03 am

Nice Mr. plusminus :) you are writing a book about Android. Sure you can use it if its will be useful :wink:
Tell us about the book when its ready :)

Regards,
code
User avatar
code
Junior Developer
Junior Developer
 
Posts: 21
Joined: Sat Feb 09, 2008 6:24 pm
Location: UAE

Postby plusminus » Tue Feb 12, 2008 11:07 am

Of course I will :)
Will be "published" on-the-fly while I am writing it. (As pdf).

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Ghalya » Tue Feb 12, 2008 1:47 pm

thx thx code

i already learn from it =P


JUST as i said to u before, y the first one is colored... its belong to focus??!!
Ghalya
Freshman
Freshman
 
Posts: 9
Joined: Sat Feb 09, 2008 4:07 pm
Location: Dubai, UAE

Postby code » Tue Feb 12, 2008 1:52 pm

you are welcome Ghalya :)

about the colored option :( i still d not know WHY the first option is colored, I will search again for that and tell you :D

Regards,
code
User avatar
code
Junior Developer
Junior Developer
 
Posts: 21
Joined: Sat Feb 09, 2008 6:24 pm
Location: UAE

Top

Postby plusminus » Tue Feb 12, 2008 2:26 pm

Hello code,

the first option is colored, because it currently has the focus.
There is a difference between: tapping the Screen with the mouse and navigating using the Emulator-"Joystick"-Pad!

Only the 2[sup]nd[/sup] one will actually move the focus.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby code » Tue Feb 12, 2008 2:48 pm

THANX plusminus for this information :D
I really was wondering about that, so Ghaya gess was correct :)

Regards,
code
User avatar
code
Junior Developer
Junior Developer
 
Posts: 21
Joined: Sat Feb 09, 2008 6:24 pm
Location: UAE

Postby plusminus » Tue Feb 12, 2008 3:33 pm

JUST as i said to u before, y the first one is colored... its belong to focus??!!

:lol: I just read over it, it looked like his signature ^^

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby FilipB » Sun Feb 17, 2008 1:05 pm

plusminus wrote:
JUST as i said to u before, y the first one is colored... its belong to focus??!!

:lol: I just read over it, it looked like his signature ^^

Regards,
plusminus


Hi there,

I find your example very useful, I suggest that you insert the following:

answerButton.requestFocus();

inside OnCreate. That will make focus issue less confusing (who got the focus and what is user choice if mouse is used to select answer). Otherwise, I guess that change of focus on click is not automatically performed due to good understanding of restricted hardware on which Android will be deployed.

Cheers
FilipB
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Feb 17, 2008 12:44 pm
Location: South Africa

Postby xayide » Tue Feb 26, 2008 11:26 am

Hello!

I have used this code to have 5 radio buttons. I want just one of them to be checked. I have debugged and when it arrives to vgood.setChecked(true); it gives a javaNullPointerException. I want to be the first option checked and then the user will select the one it wants. Then i want to know which option has the user selected. How can i know it?

I put here the xml and the code. Maybe someone can see what i am doing wrong!!

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.     android:orientation="vertical" android:layout_width="fill_parent"
  6.  
  7.     android:layout_height="fill_parent">
  8.  
  9.      
  10.  
  11.      
  12.  
  13.      <RadioGroup android:layout_width="fill_parent"
  14.  
  15.          android:layout_height="wrap_content"
  16.  
  17.          android:orientation="vertical"
  18.  
  19.          id="@+id/group1">
  20.  
  21.      
  22.  
  23.           <RadioButton android:checked="false"
  24.  
  25.               android:text="@string/verygood" id="@+id/verygood" />
  26.  
  27.           <RadioButton android:checked="false"
  28.  
  29.               android:text="@string/good" id="@+id/good" />
  30.  
  31.           <RadioButton android:checked="false"
  32.  
  33.               android:text="@string/normal" id="@+id/normal" />
  34.  
  35.           <RadioButton android:checked="false"
  36.  
  37.               android:text="@string/bad" id="@+id/bad" />
  38.  
  39.           <RadioButton android:checked="false"
  40.  
  41.               android:text="@string/verybad" id="@+id/verybad" />
  42.  
  43.      </RadioGroup>
  44.  
  45.      
  46.  
  47.      <LinearLayout android:orientation="horizontal"
  48.  
  49.          android:layout_width="fill_parent"
  50.  
  51.          android:layout_height="fill_parent">
  52.  
  53.           <Button android:layout_width="100px"
  54.  
  55.               android:layout_height="wrap_content" android:text="OK"
  56.  
  57.               id="@+id/answer" />
  58.  
  59.  
  60.  
  61.           <Button android:layout_width="100px"
  62.  
  63.               android:layout_height="wrap_content" android:text="Clear"
  64.  
  65.               id="@+id/clear" />
  66.  
  67.      </LinearLayout>
  68.  
  69. </LinearLayout>
Parsed in 0.006 seconds, using GeSHi 1.0.8.4


and the code
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public void onCreate(Bundle icicle){   
  3.  
  4.                
  5.  
  6.                 super.onCreate(icicle);
  7.  
  8.                 this.setContentView(R.layout.votes);
  9.  
  10.  
  11.  
  12.                
  13.  
  14.                 mRadioGroup = (RadioGroup) findViewById(R.id.group1);
  15.  
  16.                
  17.  
  18.                 RadioButton vgood = (RadioButton) findViewById(R.id.verygood);
  19.  
  20.                 vgood.setChecked(true);
  21.  
  22.                 RadioButton good = (RadioButton) findViewById(R.id.good);
  23.  
  24.                 RadioButton normal = (RadioButton) findViewById(R.id.normal);
  25.  
  26.                 RadioButton bad = (RadioButton) findViewById(R.id.bad);
  27.  
  28.                 RadioButton vbad = (RadioButton) findViewById(R.id.verybad);
  29.  
  30.                
  31.  
  32.         Button clearButton = (Button) findViewById(R.id.clear);
  33.  
  34.         Button answerButton = (Button) findViewById(R.id.answer);
  35.  
  36.         // save the correct answer id
  37.  
  38.         ans_1 = vgood.getId();
  39.  
  40.         ans_2 = good.getId();
  41.  
  42.         ans_3 = normal.getId();
  43.  
  44.         ans_4 = bad.getId();
  45.  
  46.         ans_5 = vbad.getId();
  47.  
  48.        
  49.  
  50.         mRadioGroup.setOnCheckedChangeListener(this);
  51.  
  52.        
  53.  
  54.         answerButton.setOnClickListener(new Button.OnClickListener() {
  55.  
  56.              public void onClick(View v) {
  57.  
  58.                          
  59.  
  60.                   showAlert("Notification",0,"Votation done" , "Close", true);
  61.  
  62.              }
  63.  
  64.         });
  65.  
  66.    }
  67.  
  68.  
  69.  
  70.    @Override
  71.  
  72.    public void onCheckedChanged(RadioGroup arg0, int checkedId) {
  73.  
  74.         choice = checkedId;
  75.  
  76.    }
  77.  
  78.  
  79.  
  80.    @Override
  81.  
  82.    public void onClick(View arg0) {
  83.  
  84.         mRadioGroup.clearCheck();
  85.  
  86.    }
  87.  
  88.    
  89.  
  90. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
xayide
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Feb 05, 2008 1:23 pm

Postby xayide » Tue Feb 26, 2008 5:19 pm

Never mind!

i have copied the xml code and didn´t realise that it wasn´t updated for the new sdk. The xml attributes must be changed to android:id

Thank you!
xayide
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Feb 05, 2008 1:23 pm

Postby Rakoun » Tue Feb 26, 2008 7:03 pm

plusminus wrote:Of course I will :)
Will be "published" on-the-fly while I am writing it. (As pdf).

Regards,
plusminus


Good initiative.

Rakoun
/°\
Pa konèt mové, dèyè do sé on péyi.
Rakoun
Freshman
Freshman
 
Posts: 7
Joined: Fri Jan 04, 2008 2:49 am
Location: Saint-François (Guadeloupe)

dynmatically create radiobutton group

Postby Andriod-Berry » Tue Apr 01, 2008 9:06 pm

Here is the basical KB, start Activity, and setContent( RadioGroupView) to check what will happen :D

if there is something wrong, Sorry :oops:


[syntax="java"]
public class RadioGroupView extends RadioGroup implements OnCheckedChangeListener{

private RadioAdapter mAdapter;
private Context mCtx;

public RadioGroupView(Context ctx, RadioAdapter adapter)
{
super(ctx);
mCtx = ctx;
mAdapter = adapter;
initRadio();
this.setOrientation(VERTICAL);
setOnCheckedChangeListener(this);
}
public void setAdapter(RadioAdapter adapter)
{
mAdapter = adapter;
}
public void initRadio()
{
LinearLayout.LayoutParams
lp = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
int i=0;
while(adapter.hasElement())
{
i++;
ItemObject obj = adapter.next();
RadioButton rb = new RadioButton(mCtx);
rb.setChecked(obj.getSelected());
rb.setText(obj.getName());
rb.setId(i);
addView(rb,i,lp);
}
}
public void onCheckedChanged(RadioGroup group, int checkedId) {
mAdapter.setSelectionIndex(checkedId);
}
public Object getSelectedItem()
{
return mAdpater.getCurrentSelection();
}
public int getSelectedIndex()
{
return mAdapter.getSelectedIndex();
}
}

public interface RadioAdapter
{
public boolean hasElement();
public Object getNext();
public int getCount();
public Object getItem(int index);
public Object getCurrentSelection();
public void setSelectionIndex(int index);
public Object getSelectionIndex();
}
</syntax>
Andriod-Berry
Freshman
Freshman
 
Posts: 2
Joined: Thu Mar 20, 2008 8:54 pm

Re: Basics for Using Radio Buttons + Buttons + Message Box

Postby bjreddi » Thu Apr 03, 2008 1:34 pm

Hi Plusminus,

I tried your RadioButtons Application,
I am getting error in in this line of code.

showAlert(
"Result",
(Exercise1.choice == Exercise1.ans) ? "Yes, you are right"
: "No, your answer is worgn ", "Close", true

Can you tell me why??
bjreddi
Junior Developer
Junior Developer
 
Posts: 17
Joined: Thu Apr 03, 2008 1:24 pm

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests