Monday, 4 June 2012

Simple Radio Group & Radio Button Demo


/* This is Simple Radio Group & Radio Button Demo of Android */

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/rdRed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Red" />

        <RadioButton
            android:id="@+id/rdGreen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Green" />

        <RadioButton
            android:id="@+id/rdBlue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Blue" />
    </RadioGroup>

</LinearLayout>

RadioDemoActivity.java


package com.radiodemo;

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

public class RadioDemoActivity extends Activity {
    /** Called when the activity is first created. */
RadioButton rdRed,rdGreen,rdBlue;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        rdRed=(RadioButton)findViewById(R.id.rdRed);
        rdGreen=(RadioButton)findViewById(R.id.rdGreen);
        rdBlue=(RadioButton)findViewById(R.id.rdBlue);
     
        rdRed.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(RadioDemoActivity.this, rdRed.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
     
        rdGreen.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(RadioDemoActivity.this, rdGreen.getText().toString(), Toast.LENGTH_SHORT).show();
}
});

        rdBlue.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(RadioDemoActivity.this, rdBlue.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
    }
}




0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Best Web Host