Reputation: 5
I have a simple quiz game which is working well but the one problem I am having is that the answer and options to the questions are displayed on buttons but the answer button appears in the same place everytime.
Is there a way to randomise the positioning through the XML interface or would it be better to handle this on the coding side?
Below is the code I am using for the XML and the code I use to bind the data value from SQLite to the buttons. Any advice would be much appreciated.
private void showQuestions(Cursor cursor) {
// Collect String Values from Query
StringBuilder questiontxt = new StringBuilder("");
StringBuilder answertxt1 = new StringBuilder("");
StringBuilder answertxt2 = new StringBuilder("");
StringBuilder answertxt3 = new StringBuilder("");
StringBuilder answertxt4 = new StringBuilder("");
while (cursor.moveToNext()) {
String question = cursor.getString(2);
String answer = cursor.getString(3);
String option1 = cursor.getString(4);
String option2 = cursor.getString(5);
String option3 = cursor.getString(6);
questiontxt.append(question).append("");
answertxt1.append(answer).append("");
answertxt2.append(option1).append("");
answertxt3.append(option2).append("");
answertxt4.append(option3).append("");
}
// Display answer button
TextView answer = (TextView) findViewById(R.id.option1_button);
answer.setText(answertxt1);
//Display question
TextView text = (TextView) findViewById(R.id.text);
text.setText(questiontxt);
TextView optionbutton1 = (TextView) findViewById(R.id.option2_button);
optionbutton1.setText(answertxt2);
TextView optionbutton2 = (TextView) findViewById(R.id.option3_button);
optionbutton2.setText(answertxt3);
TextView optionbutton3 = (TextView) findViewById(R.id.option4_button);
optionbutton3.setText(answertxt4);
}
And the XML
<Button
android:id="@+id/option1_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/option3_button"/>
<Button
android:id="@+id/option2_button"
android:text="@string/option2_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_above="@+id/option4_button"/>
<Button
android:id="@+id/option3_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/option3_label"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"/>
<Button
android:id="@+id/option4_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/option4_label"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>
Upvotes: 0
Views: 176
Reputation: 5244
Hi I would suggest handle that on the coding side..I doubt I you would be able to chieve this on the xml side!!
int randomInt = Math.random() * 1000;
TextView answer = (TextView) findViewById(R.id.option1_button);
answer.setText(answertxt1);
//Display question
TextView text = (TextView) findViewById(R.id.text);
text.setText(questiontxt);
if(randomInt%3==0){
TextView optionbutton1 = (TextView) findViewById(R.id.option2_button);
optionbutton1.setText(answertxt2);
TextView optionbutton2 = (TextView) findViewById(R.id.option3_button);
optionbutton2.setText(answertxt3);
TextView optionbutton3 = (TextView) findViewById(R.id.option4_button);
optionbutton3.setText(answertxt4);}
else {TextView optionbutton1 = (TextView) findViewById(R.id.option3_button);
optionbutton1.setText(answertxt3);
TextView optionbutton2 = (TextView) findViewById(R.id.option4_button);
optionbutton2.setText(answertxt4);
TextView optionbutton3 = (TextView) findViewById(R.id.option2_button);
optionbutton3.setText(answertxt2);}
you can try various random methods to display in various way!! I hope this helps
Upvotes: 1