image1 image2 image3

HELLO I'M NITENDRA THAKUR|WELCOME TO MY PERSONAL BLOG|I LOVE TO DO CREATIVE THINGS|I'M PROFESSIONAL ANDROID APPLICATION DEVELOPER

how to use RadioButton in android programming?

1. RadioButton

RadioButton allow the user to select one options from a set. Typically, you should present each CheckBox option in a vertical list.A RadioButton is a specific type of two-states button that can be either checked or unchecked.

2.How to use RadioButton?


  • To use RadioButton ,create or drag and drop the RadioButton view into the Application layout.
  • However, radio buttons are mutually exclusive, you must group them together inside a RadioGroup. By grouping them together, the system ensures that only one radio button can be selected at a time.
  • Define a generic listener for all RadioButtons in the RadioGroup.

          final onClickListener radioListener = new onClickListener(){

              }

  • Implements the method.{

         protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv = (TextView)findViewById(R.id.textView1);
//define the generic listener for all three radioButtons in RadioGroup
final OnClickListener radioListener= new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
RadioButton rb = (RadioButton) view;
tv.setText(rb.getText()+"  chosen");
}
};
  final RadioButton check1=(RadioButton)findViewById(R.id.check1);
          check1.setOnClickListener(radioListener);//called when radiobutton1 is clicked

          final RadioButton check2=(RadioButton)findViewById(R.id.check2);
          check2.setOnClickListener(radioListener);//called when radiobutton2 is clicked

          final RadioButton check3=(RadioButton)findViewById(R.id.check3);
          check3.setOnClickListener(radioListener);//called when radiobutton3 is clicked
}



3. Methods of RadioButton




  • public void onIntializedAccessibilityEvent(AccessibiltyEvent event) called when the source of an accessibility event is the view whose state change triggered firing the event.
  • public void onIntializedAccessibilityNodeInfo(AccessibilityNodeInfo info) called to initialize an AccessibilityNodeInfo with information about the view. the base implementation sets :

  1. setParent(View),
  2. setBoundsInParent(Rect),
  3. setBoundsInScreen(Rect),
  4. setPackageName(CharSequence),
  5. setClassName(CharSequence),
  6. setContentDescription(CharSequence),
  7. setEnabled(boolean),
  8. setClickable(boolean),
  9. setFocusable(boolean),
  10. setFocused(boolean),
  11. setLongClickable(boolean),
  12. setSelected(boolean)

  • public void toggle() called to change the checked state of the view to the inverse of its current state. 

4.Download RadioButton Example :RadioButtonDemo.zip

Share this:

CONVERSATION

0 comments:

Post a Comment