Monday, March 3, 2014

How to use RadioButton Group in ANDROID




XML cord for radiobuttons and radiobutton group

 <RadioGroup
        android:id="@+id/rg_I_selectloging_activity_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

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

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




 Java Cord for use radiobuttons   
      
// convert xml id of radiobutton group to java
 
  RadioGroup     rg=(RadioGroup)findViewById(R.id.rg_I_selectloging_activity_a);


loging=(Button)findViewById(R.id.bt_I_loging_activitymain_a);
 loging.setOnClickListener(new View.OnClickListener() {

final String value     =     ((RadioButton)findViewById(rg.getCheckedRadioButtonId())).getText().toString();     
 // get the selected radio buttons value as a string
                         if(value.equalsIgnoreCase("Delear")){
// check the string values is equal for another string value
                                 Intent openStartingPoint = new Intent("android.intent.action.DEALERMENU");
                                 startActivity(openStartingPoint);

// load a page

                           }
                else if(value.equalsIgnoreCase("Rep")){
                                Intent openStartingPoint = new Intent("android.intent.action.REPMENU");
                                startActivity(openStartingPoint);
                  
                }

});

---------------------------------------------------------------------------------------------------------------

if you want trigger some thing in radiobutton click
can use this cord

rg.setOnCheckedChangeListener(new OnCheckedChangeListener() 
        {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch(checkedId){
                    case R.id.radio0:
                        // do operations specific to this selection
                    break;

                    case R.id.radio1:
                        // do operations specific to this selection
                    break;

                    case R.id.radio2:
                        // do operations specific to this selection
                    break;

                }


            }
        });
 


No comments:

Post a Comment