Hai All,

I am new to Android programming and got a problem in radioButton which thru constant effort not able to fix it.
Requirement: My project is creating 4 radio buttons at run time and on click of 1st to 3rd radio button, i must display the radio button ID and on click of 4th RD Button, i must create a edit text box where the user has to enter a data.
Now able to create d radio buttons but when i try to choose any buttons, am not able to display its ID .
Whr am i wrong in d coding. I have created a RadioGroup ID in my layout.

public void onCreate(Bundle savedInstanceState)
    {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.showradioscreen);
      View linearLayout = findViewById(R.id.showradioscreeninfo);           

      //Dynamically create radio buttons
      radioGroup = new RadioGroup(this);
      radioGroup.setOrientation(RadioGroup.VERTICAL);
      for(int i = 0; i < 4; ++i)
      {
          radioButton[i]  = new RadioButton(this);          
          radioGroup.addView(radioButton[i]);
          radioButton[i].setId(i);
          radioButton[i].setText("RD_" + i);   
      }
      ((LinearLayout) linearLayout).addView(radioGroup);      

      radio_group = (RadioGroup) findViewById(R.id.radio_group1);
      radio_group.setOnClickListener(new OnClickListener() 
      {
          public void onClick(View view)
          {
             onRadioButtonClick(view); 
          }
      });     
   }//end of onCreate() 

  public void onRadioButtonClick(View v)
  { 
     int _selectedIP = radioGroup.getCheckedRadioButtonId();
      RadioButton checkedButton = (RadioButton)radio_group.findViewById(_selectedIP);
      boolean isChecked = ((RadioButton) v).isChecked();
      switch(v.getId())
      {
        case 1:
            EditText lEditText = new EditText(this);
            lEditText.setText(1);//
        break;

        case 2:
            EditText lEditText1 = new EditText(this);
            lEditText1.setText(2);//
        break;    
      }
 }

From what I see you have two RadioGroups named radioGroup (one dynamically created) and then second that you created somewhere at the top of class radio_group. Not sure if that is mistake in naming or you really want to use two...
If you want to use both groups you need to also add onClickListener to dynamic group, then on listener you have to call for RadioButton id from View not existing radiogroup as you did on line 31

Thank u Peter.. came across an article which has stated that a radio group id have to be created in the layout section. So based on it have created this id "radio_group".
and now on click of 4th button, i can open the Edit Box and also able to display the Radio button Ids. So far good. But problem is in alignment of the Buttons.

public class MyFirstAppl extends Activity
{
  public static RadioButton ro;
  public static RadioButton[] radioButton = new RadioButton[7];
  private static RadioGroup radioGroup;    
  View linearLayout;
  RadioGroup radio_group;

  public void onCreate(Bundle savedInstanceState)
    {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.screensettings);
      View linearLayout = findViewById(R.id.myinfoview);    

     //Dynamically create radio buttons
      radioGroup = new RadioGroup(this);
      radioGroup.setOrientation(RadioGroup.VERTICAL);
      for(int i = 0; i < 4; ++i)
      {
        radioButton[i]  = new RadioButton(this);          
        radioGroup.addView(radioButton[i]);       
        radioButton[i].setId(i);
        if (i < 2)
           radioButton[i].setText("Grade_" + i);
        else
          {
            radioButton[i].setText("User Choice");
            ro = radioButton[i];
          }
      }

      //create two dynamic buttons here
       Button AddButton = new Button(this);
       AddButton.setText("ADD");
       Button ExitButton = new Button(this);
       Exitbutton.setText("EXIT");

        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
      ((LinearLayout) linearLayout).setGravity((Gravity.BOTTOM));
      ((LinearLayout) linearLayout).setGravity(Gravity.CENTER_HORIZONTAL);  
      ((LinearLayout) linearLayout).addView(AddButton,lp);
      ((LinearLayout) linearLayout).addView(ExitButton,lp);

      AddButton.setOnClickListener(new OnClickListener()      
    //  radio_group = (RadioGroup) findViewById(R.id.radio_group1);
      //radio_group.setOnClickListener(new OnClickListener() 
      {
          public void onClick(View view)
          {
             //calling a func here
          }
      });
}

func1()
{
}
func2()
{
}

Problem: ADD and EXIT button not displayed on same row. Where is the coding error here.
Thanks

This is one example of why it's a good idea to separate design from code. When stuff like this happens it can be hard to find the problem. Anyways, maybe the following code will point you in the right direction. Notice 'android:layout_toRightOf="@id/addButton"'

<Button
        android:layout_below="@id/otherButton"
        android:layout_alignParentLeft="true"
        android:id="@+id/addButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ADD" />

    <Button
        **android:layout_toRightOf="@id/addButton"**
        android:layout_alignTop="@id/"
        android:id="@+id/exitButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="EXIT" />
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.