3

I need to Create RadioButton Dynamically in a Radio Group, radio Group is already defined in the XML, after I made my research I found some topics to put the android:drawableRight="@android:drawable/btn_radio" and android:button="@null" in the XML how can I do them programmatically?

Here is my code :

 final RadioGroup RG =(RadioGroup) vi.findViewById(R.id.RG);
 RG.removeAllViews();
 String[] answers= null;
 answers=item.Answers.split(";");
 final TextView txtTitle = (TextView) vi.findViewById(R.id.QuestionRow);
 txtTitle.setText(item.Question);
 for (int i=0;i<answers.length;i++){             

      RadioButton bt=null; 
      bt = new RadioButton(parent.getContext());
      bt.setId(i+1);
      bt.setText(answers[i]);    
      bt.setGravity(Gravity.RIGHT);             
      RG.addView(bt);
 }

And here is my XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Questionlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="@color/White" >

 <TextView
     android:id="@+id/QuestionRow"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentLeft="true"
     android:layout_alignParentRight="true"
     android:layout_alignParentTop="true"
     android:gravity="right"
     android:textColor="@color/black_overlay"
     android:textSize="30sp"
     android:textStyle="bold" />

 <RadioGroup 
     android:id="@+id/RG" 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:gravity="right"  
     android:layout_below="@+id/QuestionRow"
     android:layout_alignRight="@+id/QuestionRow"        
     ></RadioGroup>

 <Button
     android:id="@+id/Vote"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_alignParentLeft="true"
     android:layout_below="@+id/RG"
     android:text="Vote" />

 <Button
     android:id="@+id/button1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_centerHorizontal="true"
     android:layout_below="@+id/RG"
     android:text="See Statistics" />

 </RelativeLayout>
llrs
  • 3,308
  • 35
  • 68
Albert
  • 324
  • 1
  • 4
  • 20
  • Welcome to StackOverflow! What did you tried? – llrs Apr 25 '14 at 14:16
  • did you read the doc? `setButtonDrawable`? – njzk2 Apr 25 '14 at 14:18
  • The code is within an adapter that fills a listView and Radiobutton by default is display its text on the right what I need is display the text on the left since arabic writing starts from right to left I need to convert the drawableRight abd android:button into code... – Albert Apr 26 '14 at 20:27
  • and nop I didn't read the doc about setButtonDrawable when can I find it? – Albert Apr 28 '14 at 05:41
  • if you take this link as example (http://stackoverflow.com/questions/18914792/how-to-put-the-text-on-the-left-of-a-radio-button-in-android/18915233#18915233), he is using the RadioButtons predefined in the XML but I need to make the same thing but with Radiobuttons created by code dynamically – Albert Apr 28 '14 at 11:57

1 Answers1

0
protected void onPostExecute(HashMap<String,String> mylist) {
        // TODO Auto-generated method stub
        super.onPostExecute(mylist);
         for(Integer z=0;z<mRG.getChildCount();z++){
            View o =mRG.getChildAt(z);
            ((RadioButton)o).setText(text[z]);







    }

         for(Integer z =0;z< mRG.getChildCount();z++){

             Set set = mylist.entrySet(); 
                Iterator i = set.iterator(); 
             View o = mRG.getChildAt(z);

              NumberFormat formatter = new DecimalFormat("#0.00");

             if (o instanceof RadioButton){


             int counted[] = new int[ mRG.getChildCount()];
             int j=0;
             while(i.hasNext()) { 


    enter code here
                    Map.Entry me = (Map.Entry)i.next(); 
                    String Value=String.valueOf(formatter.format(Double.valueOf(me.getValue().toString())*100/count))+"%";
                    if (Integer.valueOf(me.getKey().toString())==((RadioButton) o).getId())
                    {

                        ((RadioButton)o).setText(text[z]+" "+ Value);
                    }



             }
         }
         }
         progressDialog.hide();
    }
Albert
  • 324
  • 1
  • 4
  • 20