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
in this tutorial you are going to implement a solution for the following prototype
[align=center]
[/align]
Step 1: Define your UI elements in your XML layout file
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <RadioGroup android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:orientation="vertical"
- id="@+id/group1">
- <TextView android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Q1: What is Android?" android:textStyle="bold" />
- <RadioButton android:checked="false"
- android:text="@string/radio_group_1_option1" id="@+id/option1" />
- <RadioButton android:checked="false"
- android:text="@string/radio_group_1_option2" id="@+id/option2" />
- <RadioButton android:checked="false"
- android:text="@string/radio_group_1_option3" id="@+id/option3" />
- </RadioGroup>
- <LinearLayout android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <Button android:layout_width="100px"
- android:layout_height="wrap_content" android:text="Answer"
- id="@+id/answer" />
- <Button android:layout_width="100px"
- android:layout_height="wrap_content" android:text="Clear"
- id="@+id/clear" />
- </LinearLayout>
- </LinearLayout>
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
, which was easy.
The String variables are defined in String.xml, which is located in res/values directory.
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">Exercise 1</string>
- <string name="radio_group_1_option1">Operating System</string>
- <string name="radio_group_1_option2">Mobile Platform</string>
- <string name="radio_group_1_option3">Development Tool</string>
- </resources>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
Step 3: Program your solution
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
but am learning Android basics now
Using java Syntax Highlighting
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- public class Exercise1 extends Activity implements
- RadioGroup.OnCheckedChangeListener, View.OnClickListener {
- /** Called when the activity is first created. */
- protected static int ans = -1;
- protected static int choice = -2;
- protected static RadioGroup mRadioGroup;
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.main);
- // creating references to the UI Commands
- mRadioGroup = (RadioGroup) findViewById(R.id.group1);
- RadioButton r2 = (RadioButton) findViewById(R.id.option2);
- Button clearButton = (Button) findViewById(R.id.clear);
- Button answerButton = (Button) findViewById(R.id.answer);
- // save the correct answer id
- ans = r2.getId();
- mRadioGroup.setOnCheckedChangeListener(this);
- clearButton.setOnClickListener(this);
- answerButton.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- showAlert(
- "Result",
- (Exercise1.choice == Exercise1.ans) ? "Yes, you are right"
- : "No, your answer is worgn ", "Close", true);
- }
- });
- }
- @Override
- public void onCheckedChanged(RadioGroup arg0, int checkedId) {
- choice = checkedId;
- }
- @Override
- public void onClick(View arg0) {
- mRadioGroup.clearCheck();
- }
- }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
What it will look like:
[align=center]
[/align]
This was my first tutorial
hope it will be useful for bigenners -- like ME
Your feedbacks & comments are very welcome
Regards,
code






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