1,105,585 Community Members

setOnClickListener in RadioButton not working

Member Avatar
anukavi
Light Poster
27 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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;    
      }
 }
Member Avatar
peter_budo
Code tags enforcer
7,935 posts since Dec 2004
Reputation Points: 2,502 [?]
Q&As Helped to Solve: 1,028 [?]
Skill Endorsements: 66 [?]
Moderator
Featured
 
0
 

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

Member Avatar
anukavi
Light Poster
27 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
joshfizzle
Newbie Poster
23 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

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" />
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article