Save instance when rotating

Joined
Sep 27, 2023
Messages
2
Reaction score
0
Hello all

I have made a "math game" for my class in University. I have a problem that one text field always changes its text when the phone rotates. The text in the text field is taken from an array with 15 equations. Does anyone know how to solve this. Here is some code:

the array:
<string-array name="regnestykker">
<item>1 + 1</item>
<item>2 + 3</item>
<item>4 + 2</item>
<item>5 + 3</item>
<item>8 + 2</item>
<item>6 + 5</item>
<item>5 + 9</item>
<item>7 + 8</item>
<item>6 + 6</item>
<item>8 + 4</item>
<item>1 + 5</item>
<item>9 + 4</item>
<item>9 + 9</item>
<item>2 + 6</item>
<item>9 + 6</item>
</string-array>


Some of the XML file (it is the top one that changes):
<TextView
android:id="@+id/textRegnestykket"
android:layout_width="190dp"
android:layout_height="49dp"
android:layout_marginStart="5dp"
android:layout_marginTop="93dp"
android:layout_marginEnd="85dp"
android:layout_marginBottom="420dp"
android:text="@string/regnestykket"
android:textSize="40sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textBrukerSvar"
android:layout_width="158dp"
android:layout_height="48dp"
android:layout_marginTop="93dp"
android:layout_marginStart="210dp"
android:layout_marginEnd="50dp"
android:layout_marginBottom="420dp"
android:textSize="40sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.constraintlayout.widget.Group
android:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</androidx.constraintlayout.widget.ConstraintLayout>


the equation selected in the class:
// Metode for å generere tilfeldige regnestykker
public void randomGenerator() {
if (teller == antallStykker) { // Avslutter spillet dersom antall stykker er nådd
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setMessage(getResources().getString(R.string.nyttSpill))
.setCancelable(false)
.setPositiveButton(getResources().getString(R.string.ja), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
StartSpill.this.finish();
Intent intent = new Intent(StartSpill.this, StartSpill.this.getClass());
StartSpill.this.startActivity(intent);
}
})
.setNegativeButton(getResources().getString(R.string.nei), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
StartSpill.this.finish();
}
})
.show();
} else { // Spillet fortsetter, nytt regnestykke gis ut
teller += 1;
int øvreGrense = 15;
int forrigeIndeks = indeks;
indeks = random.nextInt(øvreGrense); // Får ut tilfeldig tall fra 0 til 24 som er indeks i arrayet med regnestykker

while (indeks == forrigeIndeks) {
indeks = random.nextInt(øvreGrense);
}
textRegnestykket.setText(oppgArray.get(indeks) + " = ");
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,057
Messages
2,570,443
Members
47,115
Latest member
DorothyLus

Latest Threads

Top