Android RadioButton Example

RadioButton is used to select any one option from the given group.

In Android by using RadioGroup only we can use RadioButton. We can add any number of RadioButtons to a single RadioGroup. we can add any number of RadioGroup to a single Layout.

main.xml


     
     
     
     
     
     
          
          
          

RadioButtonExample.java
import android.app.Activity;
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 RadioButtonExample extends Activity implements OnClickListener {
     private RadioButton rb1;
     private RadioButton rb2;
     private Button b1;
     private TextView t1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        rb1=(RadioButton)findViewById(R.id.option1);
        rb2=(RadioButton)findViewById(R.id.option2);
        b1=(Button)findViewById(R.id.selected);
        b1.setOnClickListener(this);
        t1=(TextView)findViewById(R.id.TextView01);
    }
     @Override
     public void onClick(View v) {
          // TODO Auto-generated method stub
          if(v == b1){
          if(rb1.isChecked() == true)
              t1.setText("Selected is : "+rb1.getText());
          if(rb2.isChecked() == true)
              t1.setText("Selected is : "+rb2.getText());
          }
     }
}

1 comments:

  1. Anonymous said...:

    Sands Casino Resort | SEGA » | Play Online with SeptCasino
    Play over 3000+ 샌즈카지노 slots, video poker, blackjack, roulette, keno 메리트 카지노 쿠폰 and much more! Spin the reels deccasino for sephasinom games!

Post a Comment