Tuesday, July 26, 2011

Create Radio buttons with Radio Group in Android(Part-2)

Hi.This Post is the Continuation of the Create Radio buttons with Radio Group in Android(Part-1.)In the Previous post ,i am explained about the creation of static controls now , i would like share the procedure of creating a dynamic controls in Android.


First Create one xml file and change the name as Dynamic_radiobuttons.xml

Next,create one linear layout and one Radio group ..for this add the following code



  


  

  


Next,Save the xml file


Next,Create one class file and one method in that file ,like this


private void MakeRadioButtons()
{

LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View actionView = li.inflate(R.layout.static_radiobuttons, null);


//Retreive the radio buttons using id's

final RadioGroup radiogroup = (RadioGroup) actionView.findViewById(R.id.group1);

List parts_list1 = new ArrayList();

        // layout params to use when adding each radio button 

        LinearLayout.LayoutParams layoutParams = new 
         RadioGroup.LayoutParams( 
           RadioGroup.LayoutParams.WRAP_CONTENT, 
           RadioGroup.LayoutParams.WRAP_CONTENT); 
           Log.v("Entered","In radio group");
           for (int i = 0; i < 3; i++){ 
            final RadioButton newRadioButton = new RadioButton(this);
            Log.v("Entered","In radio buttons");
            DBAdapter db=new DBAdapter(getBaseContext());
             db.open();

            Cursor c3 = db.getAllParts_by_product_id_and_project_id(PRID, PID);

            Log.v("getting values","parts");

             try

             {         
     
                    if (c3.moveToFirst()) {

               do {
                parts_list1.add(c3.getString(3)); 

               } while (c3.moveToNext());
              }

                    PARTS = (String[]) parts_list1.toArray(new String[parts_list1.size()]);

                    
              newRadioButton.setText(PARTS[i]);
              newRadioButton.setChecked(true);
                    
                    newRadioButton.setId(i);

                    Log.v("set id","one");

                    radiogroup.addView(newRadioButton,layoutParams);


//Adding an action for performing while user click on radio button

radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

 public void onCheckedChanged(RadioGroup radiogroup, int checkedId)

 {

 for(int i=0; i < radiogroup.getChildCount(); i++) 

 {

  RadioButton btn = (RadioButton) radiogroup.getChildAt(i);


   if(btn.getId() == checkedId) 
   {
     
     {
       Log.v("Ur selected","Yes");                   

                             return;  
     }    
   }
 }
                       

}
}
That's..it ..My friends..if you have any doubts please feel free to ask me...Happy Coding
developercode
About the Author
Sayyad is a Software Engineer, Blogger and Founder of Developers Code from India.His articles mainly focus on .Net (Web and Windows based applications), Mobile Technologies (Android,IPhone,BlackBerry) and SEO.

Labels: , , , , ,

3 Comments:

At August 9, 2011 at 11:02 AM , Anonymous Anonymous said...

Thanks for sharing.....

 
At August 9, 2011 at 11:02 AM , Anonymous Anonymous said...

Gr8 post..it helps me alot

 
At August 12, 2011 at 9:21 AM , Anonymous Anonymous said...

Hi Tanisha..Can you tell me the Checked changed Event for other dynamic controls pls..

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home