How to count yes and no answers using radio buttons - android

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.

Related

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);

Create a key pressed function for button

i want create a key pressed (hold key) func for my application.
for example when I press (hold) a button next year the values of textview grow and when I pickup my finger from button, stop working.
anyone can help me i most use which of Listener or function for create it?
this is my setOnClickListener but i want button press(hold) work
nextDay.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (values >= downrange && values <= uprange)
values++;
if (values > uprange)
values = downrange;
if(values <10)
textDayo.setText(PersianReshape.reshape("0"+String.valueOf(values)));
else
textDayo.setText(PersianReshape.reshape(String.valueOf(values)));
}
});
thanks for your help
and sorry for bad english
when I press (hold) a button next year the values of textview grow
Have you looked at the DatePicker? This allows you to set a date very easily:
You can follow this tutorial
If you do not want to use a DatePickker, you can try to use an OnTouchListener and a Handler to do this yourself, but this is hard to do.
I understand that you are trying to hold a button (widget) and while holding it to increase the year.
If this is the case then this link should help you. The idea is that you use a timer to update the ui. Something similar is also described here. If your problem is just setting the date though, a simpler way could be the one suggested by Sam above.

Displaying an array of objects, one at a time through a single dialog... instead of several dialogs

In my application I have a list of questions stored in an ArrayList, and I want to display a dialog that shows one question, and then continues to the next one after the question is answered. The way that I'm currently doing it (iterating through a loop) hasn't been working because it just layers all of the dialogs on top of one another all at once which causes a host of other issues. What I'm looking for is a way to still iterate through the questions, but just change the layout of the dialog each time until it has finished each question in the list. Can anyone give me a good pointer for how to get this going?
You can make a function that takes title and message as parameters and shows a dialog.
showDialog(String title, String message){ // Show dialog code here}
Within that dialog's answer button's listener call another function (showQuestion(currentQuestion)) that iterates the arrayList till it is over
int currentQuestion=0;
ArrayList<QuestionObject> questionList;
showQuestion(int i){
if(i<questionList.size()){
showDialog(questionList.get(i).getTitle,questionList.get(i).getMessage);
currentQuestion++;
}else{
//quiz is over
}
}
I assume you mean that you just want to change 1 single layout(created within XML i.e main.xml). In order to do this, make sure that the class your working on is pointing to that layout. From there (assuming your using an Event listener for when the user submits an answer) you can change do as you want by the following:
TextView txt = (TextView) findViewById(R.id.textView); // references the txt XML element
and in your Event listener, if the answer is correct then change(Have i be a global variable thats initially set to 0).
if(i<arrayList.size()){
txt.setText(arrayList.get(++i));
}else{
txt.setText("You Finished");
}
From there, in the else statement, you can change arrayLists and reset i to 0;
If you are trying to use the positive, neutral, and negative buttons; then you may have problems with multiple dialogs. Try defining a customized layout with your own TextViews, ListViews, and Buttons. You can implement listeners and everything else like a regular layout. Then just pass your customized layout to the dialog through AlertDialog.Builder.setView().
PS If you include code examples of what you are currently doing we can provided answers that are less vague.

android multiple choice using images

I have 10 images in an array which are answers to 10 questions which are also images in another array.
I have 10 image buttons coming up on my relative layout loaded with the 10 answer images. The answer array is answers[] and the questions array is questions[]. They correspond to each other, meaning that answers[1] is the answer to questions[1], and answers[4] is the answer to questions[4], and so on. I have a simple "for" statement which is for(int i=0; i<=9; i++).
I want the 10 questions to come up one at a time, and let the user answer. I need the program to pause and let the user click the answer to the question. With that "for" statement, the first question which is questions[0] will pop up in imageview. Using if statements, I want to say "if the imagebutton with images[0] is clicked then I want an image to display that will say correct and do some other stuff, else I want an image to say incorrect and do some other stuff" and then I want it to pause until the user just clicks anywhere on the screen.
Can anyone help me with the pausing in the "for" statement to let the user answer, and the "if" statement where if the correct answer is picked I can display an image that says correct etc, and then pause again until the user just clicks anywhere on the screen?? Thanks!
By the way, I need those question images to pop up in the same imageview each time obviously, just changing the image.
You can't really control the program flow the way you want to with a for loop. The user interface in Android is event-driven, so the only way to move from question to question is by reacting to the event that is generated when the user clicks a button. You do that by finding the button and attaching an event handler for the click event, called onClick, which is a part of the View.OnClickListener interface. You can define a variable on your activity class to store which question you're on and use that to step your way through as the user answers questions. Your activity might look something like this:
public class QuestionActivity extends Activity implements View.OnClickListener {
public int currentQuestion = 0;
Image[] answers;
Image[] questions;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Load questions and answers
setContentView(R.layout.mylayout);
final ImageButton button = (ImageButton)findViewById(R.id.mybutton);
button.setOnClickListener(this);
}
public void onClick(View view)
{
if (/* the answer is correct */) {
// happiness: show images, do what needs doing
} else {
// sadness: show other images, etc
}
currentQuestion++;
}
}

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