My layout xml looks like
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollview1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearlayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
I am creating radiogroup and adding radiobuttons through code
LinearLayout ll = (LinearLayout)findViewById(R.id.linearlayout1);
RadioGroup rg = new RadioGroup(this);
rg.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
rg.setOrientation(0);
while(itr.hasNext()){
final String drinkname = itr.next();
System.out.println("drinkname: "+drinkname);
RadioButton rb = new RadioButton(this);
rb.setText(drinkname);
rg.addView(rb);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//group.clearCheck();
int id = group.getCheckedRadioButtonId();
RadioButton selectrb = (RadioButton) findViewById(id);
System.out.println(selectrb.getText().toString());
WebMenu.drinkname = selectrb.getText().toString();
}
});
ll.addView(rg);
It is supposed to show 6 values; But it is showing only 3 properly and remaining looks like hidden. Can someone help me out?
Thanks in advance!!
With the current RadioGroup class implementation, you cannot have two rows of RadioButtons.
So, either you set an HorizontalScrollView to your layout and allow the user to scroll to see all the RadioButtons or you use two RadioGroups, each of them containing 3 RadioButtons.
So, using your example (with little variations), this last solution can be implemented like this:
LinearLayout ll = (LinearLayout)findViewById(R.id.linearlayout1);
RadioGroup rg = new RadioGroup(this);
RadioGroup rg2 = new RadioGroup(this);
rg.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
rg.setOrientation(LinearLayout.HORIZONTAL);
rg2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
rg2.setOrientation(LinearLayout.HORIZONTAL);
String [] drinks = new String [] {"Mountain Dew", "7Up", "Root Beer", "Pepsi", "Cola", "Ice Tea"};
int i = 0;
while(i<drinks.length){
final String drinkname = drinks[i];
Log.i(TAG, "drinkname: "+drinkname);
RadioButton rb = new RadioButton(this);
rb.setText(drinkname);
if(i %2==0)
rg.addView(rb);
else
rg2.addView(rb);
i++;
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//unselect all radios in the other RadioGroup
if(rg2.getCheckedRadioButtonId()!=-1){
rg2.setOnCheckedChangeListener(null);
rg2.clearCheck();
rg2.setOnCheckedChangeListener(this);
}
int id = group.getCheckedRadioButtonId();
RadioButton selectrb = (RadioButton) findViewById(id);
if(selectrb != null){
Log.i(TAG,selectrb.getText().toString());
WebMenu.drinkname = selectrb.getText().toString();
}
}
});
rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//unselect all radios in the other RadioGroup
if(rg.getCheckedRadioButtonId()!=-1){
rg.setOnCheckedChangeListener(null);
rg.clearCheck();
rg.setOnCheckedChangeListener(this);
}
int id = group.getCheckedRadioButtonId();
RadioButton selectrb = (RadioButton) findViewById(id);
if(selectrb != null){
Log.i(TAG,selectrb.getText().toString());
WebMenu.drinkname = selectrb.getText().toString();
}
}
});
ll.addView(rg);
ll.addView(rg2);
You just have to be careful with the fact that your two RadioGroups keep track of only 3 RadioButtons each. They do not communicate with each other and do not affect each other. So if you select one RadioButton from the first row (first RadioGroup), you still have your selection on the second RadioGroup and vice-versa. You have to manage this situation yourself.
Edit: One way to manage this is unselecting all the radioButtons in RadioGroup1, when we select one RadioButton in RadioGroup2 and vice-versa. I have added code to do that.
Related
I need to add RadioGroups and RadioButtons dynamically and generate desired view and implement onCheckChangedListener() on my dynamically created RadioGroup . The problem is, when a newly added button is checked, it is not unchecked when I am clicking other button. I have already checked this - Issue in radiogroup while adding radiobuttons dynamically , but to no avail.
I am working in a fragment and adding views dynamically inside it. The radiobuttons do not exhibit check changed behavior. Please help me to sort this out.
//optionsGroup(RadioGroup),optionButton(RadioButton)
optionsGroup = new RadioGroup(getActivity());
optionsGroup.setId(radioGroupCount);
optionsGroup.setPadding(20, 20, 20, 20);
optionsGroup.setLayoutParams(layparams);
radioGroupList.add(optionsGroup);
ArrayList<Options> RadioButtonOptions = question.getQuestionOptions().getOptions();
for (int i = 0; i < RadioButtonOptions.size(); i++) {
optionButton = new RadioButton(getActivity());
optionButton.setId(i);
optionButton.setText(RadioButtonOptions.get(i).getOptionValue());
optionsGroup.addView(optionButton);
optionsGroup.clearCheck();
}
optionsGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
for(int rb=0;rb<optionsGroup.getChildCount();rb++){
RadioButton btn = (RadioButton) optionsGroup.getChildAt(rb);
if(checkedId==btn.getId()){
radioGroupAnswerList.clear();
radioGroupAnswerList.add(btn.getText().toString());
return;
}
}
}
});
holderlayout.addView(optionsGroup);
I think problem is you are adding view before assigning its listener . so please try this.
for (int i = 0; i < RadioButtonOptions.size(); i++) {
optionButton = new RadioButton(getActivity());
optionButton.setId(i);
optionButton.setText(RadioButtonOptions.get(i).getOptionValue());
optionsGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
for(int rb=0;rb<optionsGroup.getChildCount();rb++){
RadioButton btn = (RadioButton) optionsGroup.getChildAt(rb);
if(checkedId==btn.getId()){
radioGroupAnswerList.clear();
radioGroupAnswerList.add(btn.getText().toString());
return;
}
}
}
});
optionsGroup.addView(optionButton);
}
You need to uncheck other radiobutton if one radio button clicked right? and all this will be dynamically created radiobutton and radio groupe.. then you have to do this..
//optionsGroup(RadioGroup),optionButton(RadioButton)
optionsGroup = new RadioGroup(getActivity());
optionsGroup.setId(radioGroupCount);
optionsGroup.setPadding(20, 20, 20, 20);
optionsGroup.setLayoutParams(layparams);
radioGroupList.add(optionsGroup);
ArrayList<Options> RadioButtonOptions = question.getQuestionOptions().getOptions();
for (int i = 0; i < RadioButtonOptions.size(); i++) {
optionButton = new RadioButton(getActivity());
optionButton.setId(i);
optionButton.setText(RadioButtonOptions.get(i).getOptionValue());
optionsGroup.addView(optionButton);
optionsGroup.clearCheck();
}
optionsGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
for(int rb=0;rb<optionsGroup.getChildCount();rb++){
RadioButton btn = (RadioButton) optionsGroup.getChildAt(rb);
if(checkedId==btn.getId()){
radioGroupAnswerList.clear();
radioGroupAnswerList.add(btn.getText().toString());
optionsGroup.clearCheck();
if (isChecked) {
optionsGroup.check(btn.getId());
}
return;
}
}
}
});
holderlayout.addView(optionsGroup);
I have a custom radio group dialog and i am inflating it dynamically.
In my onCreate() I am doing this
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
alarmType1 = (TextView) findViewById(R.id.tvAlarmType);
View view = LayoutInflater.from(this).inflate(R.layout.custom_layout, null, false);
radioAlarmtype = (RadioGroup) view.findViewById(R.id.radiogroup);
initRadioDialog();
then initializing the dialog with the following code
String string_Alarmtype[]={"Melody","Vibration","Melody and Vibration"};
public void initRadioDialog()
{
Context context=AlarmActivity.this;
dialog_radio=new Dialog(context);
dialog_radio.requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = LayoutInflater.from(context).inflate(R.layout.custom_layout, null, false);
radioAlarmtype = (RadioGroup) view.findViewById(R.id.radiogroup);
RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
// add 3 radio buttons to the group
RadioButton rb;
for (int i = 0; i < 3; i++){
rb = new RadioButton(context);
rb.setText(string_Alarmtype[i]);
rb.setId(i);
radioAlarmtype.addView(rb, layoutParams);
}
radioAlarmtype.check(2);
radioAlarmtype.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
RadioButton rb = (RadioButton) findViewById(checkedId);
//alarmType1.setText(rb.getText());
Toast.makeText(AlarmActivity.this, checkedId + "", Toast.LENGTH_SHORT).show();
}
});
dialog_radio.setContentView(view);
}
but above commented line //alarmType1.setText(rb.getText());is causing a nullpointerException
so i tried it on my textview onClickListener
like this
View.OnClickListener alarmTypeListener = new View.OnClickListener() {
public void onClick(View v) {
dialog_radio.show();
RadioButton rb = (RadioButton) findViewById(radioAlarmtype.getCheckedRadioButtonId());
alarmType1.setText(rb.getText());//NUllpointerException here
Toast.makeText(AlarmActivity.this,"HEllo"+rb.getText(),Toast.LENGTH_SHORT).show();
//here also nullpointerException if I comment above line
}
};
But the problem still persist so how can i change the textview as change in the selection of radio button in my dialog.
Hi i created dynamic radiogroup with radiobutton.Now i want to get values from dynamic radiogroup. Below is my code
final RadioGroup rg = new RadioGroup(this);
rg.setId(questionNo);
RadioButton[] rb = new RadioButton[answers.length()];
for (int i = 0; i < answers.length(); i++) {
JSONObject answerObject = answers.getJSONObject(i);
rb[i] = new RadioButton(this);
rb[i].setText(answerObject.getString("AnswerValue"));
rb[i].setId(answerObject.getInt("AnswerId"));
rg.addView(rb[i]);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
int selectedId = rg.getCheckedRadioButtonId();
Log.i("ID", String.valueOf(selectedId));
}
});
Button click event
submit_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(rg.getCheckedRadioButtonId()!=-1){
int id= rg.getCheckedRadioButtonId();
View radioButton = rg.findViewById(id);
int radioId = rg.indexOfChild(radioButton);
RadioButton btn = (RadioButton) rg.getChildAt(radioId);
String selection = (String) btn.getText();
Log.i("selection", selection);
}
}
});
I am getting only last index of the radio group.
I know it's too late, But this may help someone else
While creating your Dynamic Radiobutton Group, Create a parent layout(Linear layout or Relative Layout)
All radio groups will be the child of that Layout and all radio button will be a child of the specific radio group.
Creating No of Dynamic Radio Group
Create all Dynamic radio group in a parent layout
Add all radio button to a group
Add all radio group to the parent layout
public RadioButton[] rb;
public RadioGroup grp;
public LinearLayout layoutmain; //Parent layout
for(int i=0;i<qus.size();i++) //No of Questions
{
rb = new RadioButton[size];
grp = new RadioGroup(this);
grp.setId(a+qus.get(i).qusetionId);
for(int j=0;j<qus.get(i).choices.size();j++) //No of Radio button in radio group
{
rb[j] = new RadioButton(this);
rb[j].setText(qus.get(i).choices.get(j).getChoiceText());
grp.addView(rb[j]); // adding button to group
}
layoutmain.addView(grp); // adding group to layout
}
Retrieve All selected button task
From parent, layout get all child
Find all radio group using the child Instance
Create an object for that radio group
using that object get the selected radio button
Button bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for(int i=0;i<layoutmain.getChildCount();i++) //From the parent layout (Linear Layout) get the child
{
View child = layoutmain.getChildAt(i);
if(child instanceof RadioGroup) //Check weather its RadioGroup using its INSTANCE
{
RadioGroup rg = (RadioGroup) child; //create a RadioButton for each group
int selectedId = rg.getCheckedRadioButtonId(); // get the selected button
RadioButton radiochecked = (RadioButton) findViewById(selectedId);
Log.e("Radio",radiochecked.getText().toString()); //from the button get the selected text
}
}
} });
I made a layout which contains radio button. I want to pass value of selected radio button to database so how can I get this value.?
Code is given below..Please help me..
private View drawRadioTypeQuestion(Question question, ArrayList<QuestionOption> questionOptions) {
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(5, 5, 5, 5);
TextView tv = new TextView(this);
tv.setText(question.Question_Order + ". " + question.Question_Title);
tv.setTextSize(16);
// tv.setTypeface(PhoneStatus.getRobotoMediumTypeface(this));
layout.addView(tv);
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.VERTICAL);
for (QuestionOption opt : questionOptions) {
RadioButton rb = new RadioButton(this);
rb.setText(opt.Question_Answer_Value);
rb.setTextSize(16);
rg.addView(rb);
}
layout.addView(rg);
PhoneStatus.overrideFonts(QuestionListActivity.this, layout);
tv.setTypeface(PhoneStatus.getRobotoMediumTypeface(this));
return layout;
}
try this code
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
RadioButton rb = (RadioButton) findViewById(checkedId);
String value = rb.getText().toString();
}
});
Implement click listener on radio button. On onclick event add value of selected radio button to database..
Visit Here for official document
http://developer.android.com/guide/topics/ui/controls/radiobutton.html
and
http://android-er.blogspot.in/2009/11/radiogroup-and-radiobutton.html
I want to create a series of radio buttons which correspond to an array of strings within an Android app. The radio buttons should toggle content to be displayed from the array. How do I do this?
You must add the radio buttons to a RadioGroup and then the RadioGroup to the layout
I miss some information like what is submit, but your code should look like:
private void createRadioButton() {
final RadioButton[] rb = new RadioButton[5];
RadioGroup rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
for(int i=0; i<5; i++){
rb[i] = new RadioButton(this);
rg.addView(rb[i]); //the RadioButtons are added to the radioGroup instead of the layout
rb[i].setText("Test");
}
ll.addView(rg);//you add the whole RadioGroup to the layout
ll.addView(submit);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for(int i = 0; i < 5; i++) {
rg.removeView(rb[i]);//now the RadioButtons are in the RadioGroup
}
ll.removeView(submit);
Questions();
}
});
}
Another code for dynamically creating the radiobutton
<TableRow>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/radiobuttons">
</RadioGroup>
</TableRow>
Code:
public void makeRadioButtons(Vector tmpVector, int i, LinearLayout.LayoutParams lp)
{
RadioButton rb = new RadioButton(this);
rb.setText((String) tmpVector.elementAt(i));
//rg is private member of class which refers to the radio group which I find
//by id.
rg.addView(rb, 0, lp);
}