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.
Related
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 5 years ago.
Improve this question
I created Custom Button which extends Compound Button but the textview is behind the drawable of the button. I want text of Button should come up and drawable should go behind without creating other view in the layout.
Is it possible to achieve this?
Any Link or sample code helps a lot.
CompoundButton is an abstract class which is mainly used to create other views from SDK like Checkbox or Switch and it has it's own limitations and specific behaviour.
There are different ways to implement it much easier and without CompoundButton dependency.
1. Create your own ViewGroup and use children as layers. There're methods to change z-order: view.bringToFront and group.bringChildToFront. This class rotates three layers every second.
static class LayeredButton extends RelativeLayout {
public LayeredButton(Context context) {
super(context);
Button buttonA = new Button(context);
Button buttonB = new Button(context);
Button buttonC = new Button(context);
buttonA.setText("a");
buttonB.setText("b");
buttonC.setText("c");
addView(buttonC);
addView(buttonB);
addView(buttonA);
rotateChildren();
}
void rotateChildren(){
postDelayed(new Runnable() {
#Override
public void run() {
getChildAt(0).bringToFront();
rotateChildren();
}
}, 1000);
}
}
2. Another option is just to switch visibility of views: view.setVisibility(View.VISIBLE) also inside ViewGroup
3. And last one is draw it "manually" using Canvas. This is probably the most flexible way to do it. Also has better performance and gives you opportunity to add custom animation.
public void onDraw(Canvas canvas){
canvas.drawRect(rect);//bottom layer
canvas.drawText(text);//mid layer
canvas.drawLine(line);//top layer
}
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.
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.
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++;
}
}
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?