Thursday, September 15, 2011

How to Create a Radio Button

XML layout :



<?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"
    android:weightSum="1">
<TextView  
    android:layout_width="fill_parent" 
    android:text="This is a testing Example" 
    android:layout_height="wrap_content" android:layout_weight="0.04"/>
    
    
<Button android:layout_weight="0.04"
 android:text="Click" 
 android:id="@+id/button1" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content">
 </Button>
<RadioGroup android:layout_weight="0.04" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
>


    <RadioButton android:id="@+id/radio0" 
    android:checked="false"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="YellAdworks,Bangalore" 
   >
    </RadioButton>
    
    <RadioButton android:id="@+id/radio1" 
    android:checked="false"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="YellAdworks,UK">
    </RadioButton>
    
</RadioGroup>


<TextView android:layout_weight="0.04" 
android:text="Selected is:" 
android:id="@+id/textView1"
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content">
 </TextView>

<Button android:layout_weight="0.04" 
android:text="Click for Confirmation" 
android:id="@+id/button2" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</Button>




</LinearLayout>

Java Source Code

package com.sai.sample;



import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

public class YellAdworksActivity extends Activity implements OnClickListener  {
    /** Called when the activity is first created. */
private Button button1,confirm;
private RadioButton radio1,radio2;
    private ProgressDialog dialog;
    private TextView textview;
    
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
         button1=(Button)findViewById(R.id.button1);
         confirm=(Button)findViewById(R.id.button2);
         button1.setOnClickListener(this);
         confirm.setOnClickListener(this);
         radio1=(RadioButton)findViewById(R.id.radio0);
         radio2=(RadioButton)findViewById(R.id.radio1);
         textview=(TextView)findViewById(R.id.textView1);
    }

    
@Override
public void onClick(View v) {
if(button1==v){
dialog = ProgressDialog.show(YellAdworksActivity.this, "!Loading",
        "Please wait for few seconds...", true);
new Thread(){
public void run(){
try{
startActivity(new Intent(YellAdworksActivity.this,SecondScreen.class)); 
}catch(Exception e){
 
}
dialog.dismiss();
}  
}.start();
}
if(confirm==v){
if(radio1.isChecked())
textview.setText("Selected is :" + radio1.getText());
if(radio2.isChecked())
textview.setText("Selected is :" + radio2.getText());
}
//Toast.makeText(YellAdworksActivity.this, "You have clicked the First Button", Toast.LENGTH_SHORT).show();
}
}

Run as Android Application




No comments:

Post a Comment