how can i set RadioGroup unchecked by default in android? - android

I am doing an app to conduct simple quiz.I have given the answers are multiple choice using RadioGroup. So on loading each question how can I set RadioGroup unchecked by default ?

Just set the checked Property in your XML for every RadioButton to false:
android:checked="false"
That should solve the issue.

Have you looked on the android developer guide?
http://developer.android.com/reference/android/widget/RadioGroup.html
Intially, all of the radio buttons are unchecked.
Although you can do this in code with the clearCheck() method
Clears the selection. When the selection is cleared, no radio button in this group is selected and getCheckedRadioButtonId() returns null.
Perhaps your problem might be a different from my understanding. Perhaps explain more about how you are loading questions and so forth.

Try
RadioGroup.check(0)
0 is an id which will nevere be generated by adt
upd: from source of RadioGroup
public class RadioGroup extends LinearLayout {
// holds the checked id; the selection is empty by default
private int mCheckedId = -1;
...

Related

Android/Kotlin Material Button checkedId, what is it?

Wonder if anyone can tell me what exactly the checkedId of a Material Button is? The documentation is not very clear. For instance:
button.addOnButtonCheckedListener { group, checkedId, isChecked -> stuff e.g. Toast}
For the checkedId, I get a long integer. For instance, I have a button with XML ID android:id="#+id/paletteF", and when I click it the checkedId returns as 2131362134. I don't see an obvious correlation here, if there is one... Is it an address??
First it is useful to understand that Android has a generated class called R which contains identifiers for all of your XML declared resources. So every time you declare #+id/newId in an XML file, a new static field (an integer) is created in the R class representing that ID, namely R.id.newID.
So in answer to your question, the checkedId refers to the same ID that android:id="#+id/paletteF" does and to check them programmatically you would would something like:
checkedId == R.id.paletteF
Just as a side note, this was an oversimplification of the R class but if you're interested there's plenty of information out there, some of it on this forum.

Listview Custom Adapter with Button Tags

I have been experiencing a problem with my custom adapter and have located the problem solution. However I do not know how to go about it. I have researched and seen a few examples of a listview custom adapter at which buttons are given tags like viewHolder.button.setTag(Tag) and I understand what the tag does but I am unsure as to how to use it. My questions are: When I set the tag on a button, how does the application differentiate my buttons from another if all the tags are set the same? Also, say I have an onClick method in my custom adapter, how do I use the tag that I set to the button to identify the button that was clicked? I've kind of seen similar adapters on the internet but not exactly , a link to an example would be greatly appreciated also.
I am unsure as to how to use it
tag is a mechanism to make your views remember something, that could be an object an integer a string or anything you like.
My questions are: When I set the tag on a button, how does the
application differentiate my buttons from another if all the tags are
set the same?
i do not understand this question but i think if you notice that your button has a memory and it calls tag you can use it in a better way. if all of your buttons memory(tags) are the same so you can not use tags to distinguish the buttons you must use ids.
say I have an onClick method in my custom adapter, how do I use the
tag that I set to the button to identify the button that was clicked?
you must set different tags for your buttons or grouped them logically and set different tags for each group then in your onClick method use the tags to identify your buttons group:
OnClickListener myButtonListener = new OnClickListener() {
#Override
public void onClick(View arg0) {
Object obj = arg0.getTag();
if(obj instanceOf groupOneTagObject){
// do action for group 1
}else if(obj instanceOf groupTwoTagObject){
// do action for group 2
}
}
});

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 to test checkboxes in custom listViews using Robotium in android

I'm testing my android app using robotium, I have used fragments in my activity, can anyone tell me how to test Checkboxes present inside the custom ListView, I'm not able to get the indexes of the CheckBox to check or uncheck them..
Thanks
Something on the similarity of this:
CheckBox cb = (CheckBox) activity.findViewById (R.id.checkboxid);
cb.getVisibility () == View.VISIBLE; //example
Natali, and if the application is multilanguage? And if the time will change the value of the text? If pressed, then the ID is better. For example:
CheckBox all = (CheckBox) solo.GetView(R.id.checkboxid);
solo.clickOnView(all);
I think the best way is to use:
solo.clickOnCheckBox(set_here_cb_index);
So use:
solo.clickOnCheckBox(0); //to check or uncheck the first checkbox
solo.clickOnCheckBox(1); //for the second
Try to call
solo.clickOnText("your CheckBox name");

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.

Categories

Resources