RadioGroup enables to check all answers - android

I've made an RadioGroup and I'm adding RadioButtons to it all programmatically.
The problem is, when running I can check all options and cannot uncheck them.
Here is my code:
optionsContainer = new RadioGroup(_myActivity);
this.setId(IdDispencer.DispenceID());
for (int i=0; i < options.size(); i++){
RadioButton asw = new RadioButton(_myActivity);
asw.setText(options.get(i));
asw.setId(IdDispencer.DispenceID());
optionsContainer.addView(asw);
}
P.S. 1 - I've tried to setup an array to hold the radio buttons as showed here:
Android RadioGroup checks more than one RadioButton?
But with no success.
P.S. 2 - This is very weird, the behavior is inconsistent. Sometimes it works sometimes it doesn't.
On my emulator running 2.3.3 it show this behavior but on my SGS1 with CM9 it shows the problematic behavior all the time.

I had the same problem and the solution I found is to set an id to each radio button (I didn't do it at all).
In your code, it seems that you give at each button the same id. Maybe the problem is here.

Related

Android - How do I make sure a widget is the last in that line?

I'm trying to export all widgets on screen to a text. To do that, I'm cycling through all widgets inside the RelativeLayout I have on screen. How do I make sure the view I'm currently looking at is the rightmost (or last) in that line?
for(int i = 0; i < relLayout.getChildCount(); i++) {
View view = relLayout.getChildAt(i);
(...)
}
And then I'm checking if the view I'm looking at is a TextView, EditText, Spinner, etc. So far, so good. But in order to append the new lines to the string, I'd like to programmatically check if that view is the last in that row. Is there a clear/simple way to do that (other than enumerating the id's of those widgets, which I'm doing right now? :D)
Thanks in advance!
PS1: For the purpose of the question, assume there are no compilation errors in the code. I just don't know how to achieve this.
Fixed, but in a curious way.
In my case, all the last fields on screen had Align Parent End flagged. So I found online this would be the way to do it:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
int[] rules = params.getRules();
if ((rules[RelativeLayout.ALIGN_PARENT_END] == RelativeLayout.TRUE)) {
// Do something about it
}
Where RelativeLayout.ALIGN_PARENT_END stands for 21, according to http://developer.android.com/reference/android/widget/RelativeLayout.html
However, my code was returning false for that check. So I started displaying the value for the property, and it really was false. I tried this for a while, then I saw one of my edittexts returned true for it. But the activity xml showed this
android:layout_alignParentRight="true"
in addition to alignParentEnd. Which didn't make sense, since alignParentRight is supposed to be rule 11, according to the same link above.
So I devised this code to expose which rules where true, and put it in my code:
for(int x = 0; x < 22; x++) {
if (rules[x] == RelativeLayout.TRUE) Log.i("Property true:", String.valueOf(x));
}
Which gave me 11 for all fields, and 11 and 21 for the field that responded TRUE before. Remember, all fields had android:layout_alignParentEnd="true" in the activity xml.
So to fix my code, I'm now checking rules[RelativeLayout.ALIGN_PARENT_RIGHT] and it works, but I strongly believe those two properties are switched somehow. Can anybody confirm? Or please englighten me on where I screwed up, which is more likely :)

ANDROID Handle large amount of buttons with array?

Hey I am making an android app that will have ~256 buttons.
Because I dont want to write the very same code for everyone of these I thought it might be possible to realize an easier solution via arrays. My approach in the onCreate to set the listeners was:
1 for (int i=1; i<32; i++)
2 {
3 button[i] = (Button)findViewById(R.id.button[i]);
4 button[i].setOnTouchListener(this);
5 }
I set the Button[] like that: Button[] button=new Button [64];
Now, eclipse tells me in line 3 "button cannot be resolved or is not a field" and it just underlines the word "button", so I think it ignores/just does not recognize the [i] (array)-stuff.
The rest of my code seems to get on with that perfectly because it gets recognized as an object (correct me if I said that wrong) but the findViewById() doesn't get on with it ..
Thanks for the replies, Alex
You can't do what you proposed in your solution. A better way to go about it is to add the buttons dynamically in code. For instance,
View parentView = (LinearLayout) findViewById(R.id.parentView);
// declare button array above
for (int i=1; i<32; i++)
{
Button btn = new Button(context);
// EDIT: adding a background resource
btn.setBackgroundResource(R.layout.button_layout);
btn.setText("This is my text");
btn.setOnTouchListener(this);
button[i] = btn;
}
User "Horschtele" answered it in a perfect way but he deleted his answer on his own (don't know why).
Horschtele, if you read that, I just want to say that this solution is just perfect!
I have to (or at least I think I have to) do this for every tableRow but this saves me an infinite amount of time. Thanks again Horschtele (are you german? :))
My modified version of Horschtele's answer if you already have your buttons in a table:
ViewGroup container = (ViewGroup) findViewById(R.id.tableRow1);
for(int i=0; i<container.getChildCount();i++){
System.out.println(container.getChildCount());
Button button = (Button)container.getChildAt(i);
button.setOnTouchListener(this);
}
(don't wonder about the println, you can easily check if the system correctly recognizes the container you are refering to).
If you did it my way with an array of Button then this is the way to go:
button[i] = (Button)container.getChildAt(i);
button[i].setOnTouchListener(this);

Three Questions

First, I want set unchecked all the 8 checkboxes using a loop like this:
for (int i=1;i<8;++i){
CheckBox view1 = (CheckBox) findViewById(R.id.CheckBox0+String.valueOf(i));
view1.setChecked(false);
}
It Doesn't work, but you get the idea what I mean. How can solve it?
Second: With Eclipse I set a form list. What is making that when application starts, it immediately shows the keyboard and is focused in a editing field?. I want that the keyboard appears only after the user touches an editing field.
Third: How I set the properties of the editing field that when user touches enter, the focus doesn't pass to the next editing field. Thanks in advance.
Firstly, asking multiple questions together isn't good; it prevents people from answering when they know only one of the solutions.
1 - Relying on the IDs CheckBox0, CheckBox1, CheckBox2, ... to be in order is very risky and is bad practice. In this case, you should be using getIdentifier; this will fetch IDs CheckBox1, then CheckBox2, etc. reliably.
for (int i=1;i<8;++i){
CheckBox view1 = (CheckBox) findViewById(getResources().getIdentifier("CheckBox" + i, "id", getPackageName()));
view1.setChecked(false);
}
2 - You need to use a stateHidden modifier for this:
<activity
android:name="com.example.NoKeyboardActivity"
android:windowSoftInputMode="stateHidden" />
3 - Use imeOptions for this; actionNone is the one you're looking for, or (as per comments), actionDone to enable the "Done" button.
<TextView
...
android:imeOptions="actionNone" />
For the resources to have fixed ids after each build, you will have to declare the resources in public.xml, then you can access the ids sequentially. Check here
Also R.id.CheckBox0 is an int so do
findViewById(R.id.CheckBox0+ i)
after you have declared all checkboxes in public.xml
For the second question in the activity manifest add
android:windowSoftInputMode="stateHidden"
Third:
I think you will have to reference the same edittext in layout for android:nextFocusDown.
Not sure if this will work give it a try
1 - Remember that this is Java not Javascript, you can't do this (R.id.CheckBox0+String.valueOf(i)). Normally I create an array of int with all the R.id inside and loop through them.
2 - You could use also android:windowSoftInputMode="stateUnchanged" to not hide the keyboard if it's already showing.
3 - That's the way it's supposed to be, but you can use on the edittext the property android:imeOptions="actionNone", so the enter button will do nothing.

How do I loop through all dynamically created radiobuttons and how do I identify them?

I've got an app that dynamically adds radiobuttons from json data. I don't really know how to find out which ones are selected though. There are the radioButton.isSelected() and the radioGroup.getCheckedRadioButtonId() but since it's dynamically created I cannot use names for all the objects.
Is there a way to add them to a group upon creation and cycle through all of them later? I've got multiple radiogroups and not all of them but most are supposed to be checked.
I'm using api lvl 7 (2.1) and I'm fairly new to this. Please explain in detail.
You can use an ArrayList.
ArrayList<RadioButton> radioButtons = new ArrayList<RadioButton>();
//create your new button and add it here.
radioButtons.add(radioButton);
Then to iterate through the list if you need to retrieve each button, use a for loop:
for (int i=0;i<radioButtons.size();i++){
RadioButton button = radioButtons.get(i);
//do what you need with the button
}
And if you need each RadioButton to have certain data, then you could do:
radioButton.setTag("tag");
and then when you are iterating through the loop, you can do button.getTag();
Or if you just want to get the selected one:
RadioButton button = (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());
I think I covered everything from your question and maybe more. Let me know if I missed anything or need to give any additional explanation.

How to count yes and no answers using radio buttons

I don't know if this was asked or not (I searched a little but no result) but I'm kinda time pressed and I need help
I've been developing an application in Android and I've only started under this platform for 3 months
I have a choice test with different questions and yes and no answers using radio buttons
I want to count the "yes" answers but even like this if I put r1.isChecked() instead of for and buttons[i].isChecked() it counts the clicks
Here is what I tried until now and I get force close everytime I click on the first radiobutton RadioButton[]buttons={rb1, rb3,rb5} ;
public void onClick(View view){
checkStates(buttons);
}
private void checkStates(RadioButton[] buttons) {
for (int i=0; i<buttons.length; i++) {
if (buttons[i].isChecked())
da++;}
tv.setText("Result:"+yes);
}
How can I tell my appplication that the radiobuttons are already checked and no need to increment it on a second click (on the same radio button?)
Is there a way to count using the ids or the name of the radiobuttons?
If so how should I do it? Some tutorials (I love them) and tips would help a lot.
Thanks.
You wouldn't want to count each click or even each "state change" because that wouldn't be accurate.
The best thing to do would be to create a function called checkStates(RadioButton[] buttons) { }
You would call this function each time the user clicks a RadioButton.
You would have all your RadioButton objects stored in an array, which you pass to the function, and then the function goes through the list of RadioButtons and checks their state. If it counts 6 as being Yes, then go to your other activity.

Categories

Resources