Im newbiew to programming, i want create some quiz app.
all the answer just use radio button user must choose : 1 or 2 or 3 or 4, all the question have same radiobutton text, the answer just from 1 to 4.
i already read how to loop dynamic just radiobutton but how to loop RadioGroup because i just need the number for each question and insert into array,
this is my xml :
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#color/black"
android:text="0"
android:button="#null"
android:drawableTop="#android:drawable/btn_radio"
android:gravity="center"
android:layout_weight="1"/>
<RadioButton
android:id="#+id/radio1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#color/black"
android:text="1"
android:button="#null"
android:drawableTop="#android:drawable/btn_radio"
android:gravity="center"
android:layout_weight="1"/>
<RadioButton
android:id="#+id/radio2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#color/black"
android:text="2"
android:button="#null"
android:drawableTop="#android:drawable/btn_radio"
android:gravity="center"
android:layout_weight="1"/>
<RadioButton
android:id="#+id/radio3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#color/black"
android:text="3"
android:button="#null"
android:drawableTop="#android:drawable/btn_radio"
android:gravity="center"
android:layout_weight="1" />
</RadioGroup>
and this is the result : http://tinypic.com/r/2uzs50g/8
i thought if i can just loop radiogroup dynamic with same radiobutton for each question i can just get radiogroup id and insert into array. if you have other sugestion how i should create something like that please tell. thanks for your help all
Try this :-
RadioGroup rg = (RadioGroup) findViewById(radiogroupid);
String rg_selected_data = "";
int radioButtonId= rg.getCheckedRadioButtonId();
switch (radioButtonId) {
case YouRadioButtonIdOne:
rg_selected_data = "1";
break;
case YouRadioButtonIdtTwo:
rg_selected_data = "2";
break;
case YouRadioButtonIdtThree:
rg_selected_data = "3";
break;
case YouRadioButtonIdtFour:
rg_selected_data = "4";
break;
}
Now you can find which radiobutton is selected.
Try this code if you face any problem I will help you. :)
This solution will run code every time a radio button is checked. It goes in your onCreate(). Source is from here.
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
int questionCounter = 0;
int numberCorrect = 0;
//Question 1 is A, Q2 is C, Q3 is D, Q4 is B, Q5 is A again
final char[] correctAnswersList = {'a', 'c', 'd', 'b', 'a'};
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
switch (checkedId) {
case (R.id.radio_button_a):
if (questionCounter==0 || questionCounter==4){
numberCorrect++;
}
break;
}
case (R.id.radio_button_b):
if (questionCounter==3){
numberCorrect++;
}
break;
}
case (R.id.radio_button_c):
if (questionCounter==1){
numberCorrect++;
}
break;
}
case (R.id.radio_button_d):
if (questionCounter==2){
numberCorrect++;
}
break;
}
}
});
There is a problem with my code, which was on purpose, since it's finals season in America, and students like to come here last minute to cheat. You'll find it if you look. You wouldn't use looping with this kind of problem, I'm not sure why that's a requirement.
for all who want create some quesioner like my self this code will help you, this code will loop RadioGroup and get the value from allradiogroup use List. and thanks for all people who help
private List<RadioGroup> allradioGroup = new ArrayList<RadioGroup>();
private RadioGroup radioGroup;
private List<RadioButton> allRadio = new ArrayList<RadioButton>();
private RadioButton radioButton;
// place inside oncreate or whatever you want
//your linear layout ID
LinearLayout linear = (LinearLayout) findViewById(R.id.lintes);
LinearLayout.LayoutParams layoutParams = new
LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < 20; i++) {
/* Defining RadioGroup */
radioGroup = new RadioGroup(this);
radioGroup.setOrientation(RadioGroup.HORIZONTAL);
allradioGroup.add(radioGroup);
/* Displaying Radio Buttons */
for (int j = 0; j < 4; j++) {
radioButton = new RadioButton(this);
//you can set your button ID here
radioButton.setId(j);
allRadio.add(radioButton);
//set text for each ID
if (allRadio.get(j).getId() == 0) {
radioButton.setText("0");
} else if (allRadio.get(j).getId() == 1) {
radioButton.setText("1");
} else if (allRadio.get(j).getId() == 2) {
radioButton.setText("2");
} else if (allRadio.get(j).getId() == 3) {
radioButton.setText("3");
}
//number 4 is total radiobutton
allradioGroup.get(i).addView(allRadio.get(i*4+j),j,layoutParams);
}
// set text
TextView soal = new TextView(this);
soal.setText("\n"+i+".loop your question here");
linear.addView(soal);
linear.addView(allradioGroup.get(i));
}
and this code will get value from list, just loop (i) and you will get all the value
int id[i] = allradioGroup.get(i).getCheckedRadioButtonId();
Related
How I reset a radio button inside a radio group like the image example below
All my radio groups and its radio buttons are created programmatically.
I tried to to set OnClickListener to get the radio button value before getting changed but, it didn't help.
Edit: I posted the answer below, please check it.
try this :
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, #IdRes int checkedId) {
if (checkedId == R.id.radioButton){
//do something
}
else if(checkedId == R.id.radioButton2){
}
else{
}
});
xml layout
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="90dp"
android:layout_below="#+id/imageView"
android:layout_marginTop="58dp"
android:weightSum="1"
android:id="#+id/radioGroup"
android:layout_alignLeft="#+id/textView2"
android:layout_alignStart="#+id/textView2"
android:layout_alignRight="#+id/textView3"
android:layout_alignEnd="#+id/textView3">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="55dp"
android:text="Male"
android:id="#+id/radioButton"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="25dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:id="#+id/radioButton2"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="25dp"
android:layout_weight="0.13" />
</RadioGroup>
You have to uncheck one radiobutton while checking the other.
Simple way.
public void clearRadioChecked() {
alt1.setChecked(false);
blt2.setChecked(false);
}
if you want to select alt1 then on click of alt1 do as below.
clearRadioChecked()
alt1.setChecked(true);
This is how I did it:
It is in C# but I think it is easy to convert it to Java.
I used tag to know if this radio button checked before or not.
False => It is not checked
True => It is checked
radiobutton.Tag = false;
radiobutton.Click += SingleChoiceQuestionAlternativeClick;
Then:
private void SingleChoiceQuestionAlternativeClick(object sender, EventArgs e)
{
RadioButton questionAlternative = sender as RadioButton;
if (questionAlternative != null)
{
RadioGroup questionAlternatives = questionAlternative.Parent as RadioGroup;
if (questionAlternatives != null)
{
if (questionAlternative.Tag.Equals(false))
{
for (int i = 0; i < questionAlternatives.ChildCount; i++)
{
RadioButton childRadioButton = questionAlternatives.GetChildAt(i) as RadioButton;
if (childRadioButton != null)
{
childRadioButton.Tag = false;
}
}
questionAlternative.Tag = true;
}
else
{
questionAlternative.Tag = false;
questionAlternatives.ClearCheck();
}
}
}
}
I am trying to create a button for each year since the person started using my app. So in my xml document I have
<HorizontalScrollView
android:id="#+id/yearScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:layout_gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/currentYear"
android:tag="01"
android:text="2015"
android:paddingLeft="8.0dip"
android:paddingRight="8.0dip"
android:height="24dp"
android:textSize="18sp"
android:textColor="#333333"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
>
</Button>
</LinearLayout>
</HorizontalScrollView>
Then I have the following code
private List<Button> yearButtons;
private static final int[] YEAR_BUTTON_IDS = {
R.id.currentYear,
};
Then I must find out what year it is and overwrite my current button
int firstYear = Integer.parseInt(year);
yearButtons.get(0).setText(String.valueOf(CurrentYear));
then in my init class I substantiate the buttons, I understand I do not need a loop for only 1 button but leaving it like this for consistency with how the months buttons work
for(int id : YEAR_BUTTON_IDS) {
Button button = (Button)findViewById(id);
button.setOnClickListener(this);
yearButtons.add(button);
}
Then I have some logic to find out the first year they started called yearsOfTransactions
Then I have the following loop where I try create the buttons
for (int i =0; i< yearsOfTransactions; i++){
int yearToAdd = CurrentYear-i-1;
Button myButton = new Button(this);
myButton.setText(String.valueOf(yearToAdd));
yearButtons.add(myButton);
}
However this is not working..
Thanks for any help
I am making slight modification in your code :
<HorizontalScrollView
android:id="#+id/yearScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:layout_gravity="center">
<LinearLayout
android:id="#+id/button_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/currentYear"
android:tag="01"
android:text="2015"
android:paddingLeft="8.0dip"
android:paddingRight="8.0dip"
android:height="24dp"
android:textSize="18sp"
android:textColor="#333333"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
>
</Button>
</LinearLayout>
</HorizontalScrollView>
Now make an array of years to add (in onCreate())
int[] yearToAdd = {2000, 2001, 2002, 2003};
LinearLayout parentLayout = (LinearLayout)findViewById(R.id.button_parent);
for (int i =0; i< yearToAdd.lenght; i++){
int yearToAdd = yearToAdd[i];
Button myButton = new Button(this);
myButton.setText(String.valueOf(yearToAdd));
yearButtons.add(myButton);
yearButtons.setOnClickListener(this);
parentLayout.addView(myButton);
}
Let me know in case of more clearation you need, hope it will help :)
you have to add button to a linear layout.
This is a function i use to add dynamic buttons in a linear layout.
public Button createButton(String label) {
Button button = new Button(mContext);
button.setText(label);
mDynamicLayout.addView(button, mLayoutParam);
return button;
}
You should add every button you create to the LinearLayout. First set and id in xml to LinearLayout. Find the view in your class and, finally, add the buttons.
LinearLayout container = findViewById(R.id.container);
//...
for (int i =0; i< yearsOfTransactions; i++){
int yearToAdd = CurrentYear-i-1;
Button myButton = new Button(this);
myButton.setText(String.valueOf(yearToAdd));
yearButtons.add(myButton);
// you missed that
container.addView(myButton);
}
please try this inside your for loop
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
//set the properties for button
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText("Button");
btnTag.setId(some_random_id);
//add button to the layout
layout.addView(btnTag);
I'm trying to use radio groups where it has 2 radio buttons inside. I wanted to set a value for each radio buttons like for example the value is male and female. After that when I have already click on of my radio button it will display the value selected. How can you achieve this? Thank you for the help.
i have tried something like this just to test on how will i set a value
public void onRadioButtonClicked(View radioButton) {
int count = radioGroup.getChildCount();
for (int i = 0; i < count; i++) {
View o = radioGroup.getChildAt(i);
if (o instanceof RadioButton) {
RadioButton radioBtn = (RadioButton)o;
// get the state
boolean isChecked = radioBtn.isChecked();
// to set the check
radioBtn.setChecked(true);
}
}
}
but i think its not what i'm looking for.
i have already tried radioGroup.setId() assuming that the radio buttons have values already but it just display nonsense number.
You should use a RadioGroup and an OnCheckedChangeListener in your code to detect when a RadioButton is selected. In your layout XML:
<RadioGroup
android:id="#+id/radioGroup"
... >
<RadioButton
android:id="#+id/radioButton1"
... />
<RadioButton
android:id="#+id/radioButton2"
... />
</RadioGroup>
In your Activity/Fragment, set up like so:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangedListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
<type> value = <default value>;
switch (checkedId) {
case R.id.radioButton1:
value = ...;
break;
case R.id.radioButton2:
value = ...;
break;
}
// do something with value
}
});
Make sure that your radio buttons are wrapped in a radio group like below:
<RadioGroup
android:id="#+id/myRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radioGroupButtonMale"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/radioGroupButtonFemale"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</RadioGroup>
And in code, to set the value,
RadioGroup mySelection = (RadioGroup)findViewById(R.id.myRadioGroup);
int radioButtonId = mySelection.getCheckedRadioButtonId();
String selectedValue;
switch (radioButtonId)
{
case R.id.radioGroupButtonMale:
selectedValue = "Value for Male";
break;
case R.id.radioGroupButtonFemale:
selectedValue = "Value for Female";
break;
}
Is there a way to set the selected index of a RadioGroup in android, other than looping through the child radiobuttons and selecting checking the radio button at the selected index?
Note: I am populating the Radio Button Group at run time.
If your radio group is defined in a layout xml file, each button can be assigned an id. Then you just check a button like this
radioGroup.check(R.id.myButtonId);
If you created your radio group programmatically (I'm not even sure how you do this...), you might want to consider creating a special layout xml file just for the radio group so that you can assign R.id.* ids to the buttons.
Please see the answer below if you are, in fact, looking to set the radio button group by index, see the answer below.
((RadioButton)radioGroup.getChildAt(index)).setChecked(true);
Question said "set selected INDEX", here's how to set it by index:
((RadioButton)radioGroup.getChildAt(index)).setChecked(true);
........
Additional info: It seems that Google wants you to use id instead of index, because using id is more bug proof. For example, if you have another UI element in your RadioGroup, or if another developer re-orders the RadioButtons, the indices might change and not be what you expected. But if you're the only developer, this is totally fine.
you can do findViewById from the radio group .
((RadioButton)my_radio_group.findViewById(R.id.radiobtn_veg)).setChecked(true);`
Siavash's answer is correct:
((RadioButton)radioGroup.getChildAt(index)).setChecked(true);
But be aware that a radioGroup can contain views other than radioButtons -- like this example that includes a faint line under each choice.
<RadioGroup
android:id="#+id/radioKb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/kb1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:button="#null"
android:drawableRight="#android:drawable/btn_radio"
android:text="Onscreen - ABC" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#33000000" />
<RadioButton
android:id="#+id/kb2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:button="#null"
android:drawableRight="#android:drawable/btn_radio"
android:text="Onscreen - Qwerty" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#33000000" />
<RadioButton
android:id="#+id/kb3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:button="#null"
android:drawableRight="#android:drawable/btn_radio"
android:text="Standard softkey" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#33000000" />
<RadioButton
android:id="#+id/kb4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:button="#null"
android:drawableRight="#android:drawable/btn_radio"
android:text="Physical keyboard" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#33000000" />
</RadioGroup>
In this case using an index of 1, for example, would generate an error. The item at index 1 is the first separator line -- not a radioButton. The radioButtons in this example are at indexes 0, 2, 4, 6.
This Worked For me, I created radio button dynamically by
private void createRadioButton() {
RadioButton[] rb = new RadioButton[5];
RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
1.0f);
radioGroup.setOrientation(RadioGroup.HORIZONTAL);
for (int ID = 0; ID < 5; ID++) {
rb[ID] = new RadioButton(this);
rb[ID].setLayoutParams(layoutParams);
rb[ID].setText("Button_Text");
radioGroup.addView(rb[ID]); //the RadioButtons are added to the radioGroup instead of the layout
}
}
Now Check a button using,
int radio_button_Id = radioGroup.getChildAt(index).getId();
radioGroup.check( radio_button_Id );
Inside onBindViewHolder set the tag to Button Group
#Override
public void onBindViewHolder(final CustomViewHolder holder, final int position) {
...
holder.ButtonGroup.setTag(position);
}
and in the ViewHolder
ButtonGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
...
int id = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) radioGroup.findViewById(id);
int clickedPos = (Integer) radioGroup.getTag();
packageModelList.get(clickedPos).getPoll_quest()
}
Using Kotlin you can make it by
(radio_group_id.getChildAt(index) as RadioButton).isChecked = true
or
radio_group_id.check(R.id.radio_button_id)
I am trying to use this Android: How to get a radiogroup with togglebuttons? code of the given answer
but in
static final RadioGroup.OnCheckedChangeListener ToggleListener = new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(final RadioGroup radioGroup, final int i) {
for (int j = 0; j < radioGroup.getChildCount(); j++) {
final ToggleButton view = (ToggleButton) radioGroup.getChildAt(j);
view.setChecked(view.getId() == i);
}
}
};
in line
final ToggleButton view = (ToggleButton) radioGroup.getChildAt(j);
it always crashes. In Logcat I don't see any message.
I tried everything I could think of , but can't fid the problem -
Many thanks!
ps this is my xml for the radiogroup:
<RadioGroup android:id="#+id/radioGroup2" android:layout_width="150sp" android:layout_height="wrap_content"
android:paddingLeft = "10sp" android:layout_alignBottom="#+id/a2" >
<RadioButton android:layout_width="wrap_content" android:id="#+id/Settings_otherSettingsT2Yes" android:layout_height="wrap_content"
android:textColor="#000000" android:textSize="18sp"
android:text="#string/Settings_otherSettingsT2Yes" android:checked="false"></RadioButton>
<RadioButton android:layout_width="wrap_content" android:id="#+id/Settings_otherSettingsT2No" android:layout_height="wrap_content"
android:textColor="#000000" android:textSize="18sp"
android:text="#string/Settings_otherSettingsT2No"></RadioButton>
</RadioGroup>
...nothing special here
I checked the number of children by logging radioGroup.getChildCount() and it gives 2 as expected
In your code you cast RadioButtons to ToggleButton. This is likely to be the cause of your crashes. I don't understand why you can't find an exception in logcat.
However, you say you want to use code like in the link, but your XML is not close to the link. You use RadioButtons where the link uses ToggleButtons.
If you just want RadioButtons, then completely disregard that link.
If you want to use ToggleButtons then that link isn't the best way anyway. Adding ToggleButtons to a RadioGroup just confuses the intent of the code.