I created a game with a question and answer, and I need to keep score without creating any database. I created an individual activity and .xml for each question, and the user needs to enter the answer in the text box, and if the answer is correct, he automatically proceeds to the next action (i.e. the next question). Now I need to keep the score so that after the correct answer from the user, the score should be displayed in the upper right corner of the screen. How can I do it? please help. Here's the java activity and xml file for my second level one question:
package com.golo.user.gaunkhanekatha;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Handler;
public class TwoActivity extends Activity {
public Button check;
public EditText typeh;
private Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
toast = Toast.makeText(TwoActivity.this, "", Toast.LENGTH_SHORT);
check = (Button)findViewById(R.id.check1);
typeh = (EditText)findViewById(R.id.type1);
check.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String Answer = (String) typeh.getText().toString();
if(Answer.equals("piano") || (Answer.equals("keyboard")))
{
toast.setText("Correct! Now, next question...");
toast.setGravity(Gravity.TOP | Gravity.LEFT, 500, 300);
toast.show();
Intent i = new Intent(TwoActivity.this, ThreeActivity.class);
startActivity(i);
finish();
}
else{
toast.setText("Wrong! Try Again!");
toast.setGravity(Gravity.TOP | Gravity.LEFT, 500, 300);
toast.show();
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if(toast!= null) {
toast.cancel();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_aboutus, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:weightSum="1"
android:textAlignment="center"
android:id="@+id/level1">
<TextView
android:layout_width="300dp"
android:layout_height="200dp"
android:text="What has 88 keys but cannot open a single door?"
android:id="@+id/que1"
android:width="255dp"
android:textSize="30dp"
android:layout_margin="50dp"
android:textStyle="italic"
android:gravity="center" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/type1"
android:layout_gravity="center_horizontal"
android:hint="Type here..." />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check answer..."
android:id="@+id/check1"
android:layout_gravity="center_horizontal" />
</LinearLayout>
source
share