android multiple choice using images - android

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++;
}
}

Related

Getting next and previous from ArrayList after data parsed to new activity

Ok, I was complaining about this a few days ago, now I have some code in from following a few tutorials/lessons.
I have a RecyclerView that contains an ArrayList with cards, those have an image, title and description.
I have OnItemClick for those cards, using Parcelable when a card is clicked a second activity opens and shows the full details from the cards - that image, title and description, with a new layout.
I've added a previous and a next button bellow that.
Edit to clarify Struggling with:
I need to have a button in the second activity, the one that opens when clicking on a specific card, and clicking on that button should bring the details from the next card from the first activity. Currently a user would have to go back from the second activity to the recyclerview and click on the next card to see the full details from it
I tried implementing a couple of things I found in older questions here but since they're mostly just code based on somebody else's specific situation I can't seem to edit them in order to get them to work.
I'm adding the link to the repository, it's a bit messy since I'm testing a bunch of things in it, but I'm leaving it for reference: https://github.com/simplydikka/traffic.git
The activities I'm working with currently are those:
Details from list gist
The only walk-around I could find based on my level was to set the click of each card to lead with an if statement to a specific tab from a tabbed activity, but that would mean I will have a very very long list of fragment layouts with different information in them by hand, and that just can't be the answer for something like that. I got that to work but it makes no sense to put that much content by hand when I've already got it to show based on position
EDIT: I got to here:
Intent intent = getIntent();
final ExampleItem exampleItem = intent.getParcelableExtra("Example Item");
position = intent.getExtras().getInt("Position");
final int imageRes = exampleItem.getImageResource();
final String line1 = exampleItem.getTextTitle();
final String line2 = exampleItem.getmTextDescription();
final ImageView imageView = findViewById(R.id.image_activity2);
imageView.setImageResource(imageRes);
final TextView textView1 = findViewById(R.id.text1_activity2);
textView1.setText(line1);
final TextView textView2 = findViewById(R.id.text2_activity2);
textView2.setText(line2);
Button next = (Button) findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
position = position +1;
imageView.setImageResource(imageRes);
textView1.setText(line1);
textView2.setText(line2);
}
});
This is in my second activity class, the one that is showing the details and buttons. I got the button to work. I just don't get how to attach a position to the resources. I did the dumbest thing just to see if it would work at all, and when I wrote (imgRes+1) on click the image becomes a black square, so it does affect it finally. I just need to find a way to actually bring the next position. I'm still looking, testing and searching, definitely not sitting around and waiting for somebody to solve it for me, but it would be reeeally cool if anybody passing by could hind me :D

Calculate how much time the user looks at recyclerview item [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a screen with a list of products implemented using a recyclerview.
I am looking for a way to check how much time the user spends looking at a certain item in the list. In this way I can know in which items is he interested so I can build him an "maybe interested in" list. More exactly..if a user stays on that item more than x seconds I retain that item id and do the rest... Any kind of ideea or help helps me alot. Thanks
One option is to do something similar to Android: how to check if a View inside of ScrollView is visible? to determine which view is actually within the visible screen. It can be modified slightly to detect if the entire view is visible rather than only a portion.
You're probably also going to have to track the time started/stopped viewing in the recycle view's adapter.
I don't like idea to display user something like "maybe interested in" based on your logic. Because user has seen it already. It can be super annoying if I buy computer mouse and then I see it on every Activity I visit... I'd rather see some items from same category as most seen items but not the most seen items themself. That's why I highly suggest you to count time inside item details Activities (onStart/onStop methods)
But Answer can be something like this:
See following methods:
RecyclerView.Adapter.onBindViewHolder (View created because it is most likely going to be visible to user)
RecyclerView.Adapter.onViewRecycled (View will be recycled - 100% not visible on screen)
You can create some Map or another database column (don't know your project structure) to store item id's and timestamps from
private class MyHolder extends RecyclerView.ViewHolder {
long itemId;
long start;
public MyHolder(View itemView) {
super(itemView);
}
}
#Override
public void onBindViewHolder(BindingHolder holder, int position) {
holder.itemId = ...;
holder.start = System.currentTimeMillis();
}
#Override
public void onViewRecycled(BindingHolder holder) {
super.onViewRecycled(holder);
long currentTime = System.currentTimeMillis();
long difference = currentTime - holder.start;
// store result
map.put(holder.itemId, difference);
// to better match your needs update map item and do not overwrite it
}
This is not 100% solution, but is the simplest one.
To implement it inside Activities use same System.currentTimeMillis() method inside onStart and onStop methods (or onResume/onPause). See Acticity lifecycle so that you know where you need it.

Android: multiple views in one activity

I am new to android, and I am working on a Quiz app with a set of questions, each question has a question(text) and 4 images(imageButton), when user select the correct image, then they go to next question. So from question to question, should I:
call setContentView() multiple times in one activity? (layout is same for all questions, only the question text and image changes)
or add multiple layout in xml, and at runtime, set visibility of each layout? (but by doing so, I received warnings of too many views in one xml file)
any other better suggestions?
Thanks for your reply!
My suggestion populate the question dynamically...
From your Perspective do like this
1.Take xml with Question text and 4 images.
2. Get the Ids of question text(TextView), and 4 imageview's.
3.When ever the event trigger(i.e next question) change the question text and Answer images..
Try this simple logic
int question=0;
ImageView answer1,answer2,answe3,answer4;
int answerids[]={R.id.answer1,R.id.answer1,R.id.answer1,R.id.answer1};
ImageView[] answerimages={answer1,answer2,answer3,answer4};
String questions[]={"Question1","Question2","...."};
int images[][]={{R.drawable.answer1,R.drawable.answer2,R.drawable.answer3,R.drawable.answer4},{second question drawables},{Third question drawables}...};
In onCreate do like this
TextView question=findViewById(R.id.question);
for(int i=0;i<answerids.length;i++)
{
answerimages[i]=(ImageView)findViewById(answerids[i]);
answerimages[i].setImageResource(images[question][i]);
answerimages[i].setOnclickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
question+=1;
nextqueston();
}
});
To populate next question text and images..
public void nextqueston()
{
question.setText(questions[queston]);
for(int i=0;i<answerids.length;i++)
{
answerimages[i].setImageResource(images[question][i]);
}
}
Just keep the same Activity open, when one question is done, set the question text on the TextView programmatically, same for the ImageViews. All in the same layout.

Flipping different data in the same activity when button is clicked

This is my first question to stackoverflow, so please do not see my acceptance rate.
What I am trying to create is a quiz in android. Here I am having the same layout where I have a question and 3 answers. I have a next button at the bottom. When I click on that the same layout should get loaded with new question and options for it.
I am thinking of how to do it. I saw something called view flipper, but can anyone tell me the exact solution for this?
Here is my code
public class TriviaQuiz extends Activity {
Trivia trivias = TourDescription.currentTour.getTriviaArray().get(0);
Here trvias is an object which contains all the data.
}
// send the trivias to the activity where I set the question and options
public void playquiz(View v) {
Intent quizIntent = new Intent(TriviaQuiz.this, Quiz.class);
Bundle bundle = new Bundle();
bundle.putSerializable("trivia", trivias);
quizIntent.putExtras(bundle);
startActivity(quizIntent);
}
public class Quiz extends Activity
]
In this last activity I have the layout and the next button. Hope it is clear now.
Note:Here am trying to update the array value at get(0)
Please help me a way for this.
You can just do it like this, call a method with the array index and then inside the method set the values as needed. Next when tap on the button,update the value of array index in the required manner.
You should update values on the button click for the next question. Do you see any problem with this approach?

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