Reputation: 667
I have an application with questions and want to show a progress bar to show how many questions that are left/answered. I would like to have the colour of the progress bar green if the question was correct answered and red if the answer was wrong.
Let say there are 5 questions. After e.g. 3 questions, the progress bar should be
green|red|red|grey|grey
if question 1 was correct and 2 and 3 were wrong...
Upvotes: 0
Views: 1040
Reputation: 667
I found a working solution.. Probably not the best, so comments are appriciated!
in my xml-file
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/info"
/>
<LinearLayout
android:id="@+id/take_test_progress_bar_linear_layout"
android:layout_width="match_parent"
android:layout_height="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_below="@+id/info"
android:orientation="horizontal" >
</LinearLayout>
<TextView
android:id="@+id/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/take_test_progress_bar_linear_layout"
android:text="@string/stt1"
/>
And in my activity:
oncreate(){
....
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
for(int k=0; k<mTotalQuestionNum; k++)
{
View v = new View(this);
v.setBackgroundColor(Color.GRAY);
params.weight = (float)1.0/( (float) mTotalQuestionNum);
v.setLayoutParams(params);
mProgressLayout.addView(v,k);
}
...
}
Then, when handling answers...
if(correct)
mProgressLayout.getChildAt(mQuestionNum-1).setBackgroundColor(Color.GREEN);
else
mProgressLayout.getChildAt(mQuestionNum-1).setBackgroundColor(Color.RED);
Upvotes: 1
Reputation: 22592
As njzk2 said in the comments, a ProgressBar
isn't the right component for the job.
I'd suggest using a LinearLayout
.
You can programatically add as many Views
inside it as you need, using weights for equal horizontal distribution, and you can change the background colour of these Views
as the user progresses through your questions.
Upvotes: 0