Sunday, August 19, 2012

Android - Radio Button Example


Below is example of RadioButton, it is used to select option from list of options.

I used Eclipse as development tool and tested the sample in Emulator for Android 4.0.3.

There are lot of resources are used to develop android app. but for this example, we are using three basis components like code base file, layout file and string file.

Layout file (XML):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RadioGroup 
        android:id="@+id/rdogroupsex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <RadioButton 
            android:id="@+id/rdomale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rdomale"
            />
        <RadioButton 
            android:id="@+id/rdofemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rdofemale"
            />
 
    </RadioGroup>
<Button
   android:id="@+id/btnok"
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:text="@string/btnok"
   />

</LinearLayout>

String file(XML file):

<resources>

    <string name="app_name">RadioButtonExample</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_radio_button_example">Radio Button Example</string>
<string name="rdomale">Male</string>
<string name="rdofemale">Female</string>
<string name="btnok">OK</string>
</resources>

Code base file (.JAVA):

package com.example.radiobuttonexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class RadioButtonExample extends Activity {

private RadioGroup rdoGroup;
private RadioButton rdoMale, rdoFemale;
private Button btnOk;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_button_example);
        
        rdoGroup = (RadioGroup) findViewById(R.id.rdogroupsex);
        rdoFemale = (RadioButton) findViewById(R.id.rdofemale);
        rdoMale = (RadioButton) findViewById(R.id.rdomale);
        btnOk = (Button) findViewById(R.id.btnok);
        
        onButtonOkClick();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_radio_button_example, menu);
        return true;
    }
    
    
    public void onButtonOkClick(){
    btnOk.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if ( rdoGroup.getCheckedRadioButtonId() == R.id.rdomale){
Toast.makeText(getApplicationContext(), "Male Selected", Toast.LENGTH_LONG).show();
}
if (rdoGroup.getCheckedRadioButtonId() == R.id.rdofemale){
Toast.makeText(getApplicationContext(), "Female Selected", Toast.LENGTH_LONG).show();
}
}
});
    }
}

Note: By clicking on "Ok" button, it will show selected option.


No comments:

Post a Comment