0

Iam Developing an android app in which one of its module is of Quiz.I want to show Tick image on button when someone click the right answer and wrong when wrong ans is seleceted, I want to show these tick and cross images on the right most of my button.Right now Iam displaying the toast for right and wrong answer.Also I want to show the next quiz question on the same activity when user click the next or previous button.I dont want to create another activity for next question rather want to show the next question on the same activity. the sample image like what I want to do is like this : enter image description here

Code of Quiz xml is :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/empty"
    tools:context=".activity.QuizActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_10sdp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/quiz_question"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Quiz"
            android:textStyle="bold"
            android:textSize="@dimen/_30ssp"
            android:textColor="#FFF"
            android:gravity="center_horizontal"/>

        <TextView
            android:layout_below = "@id/quiz_question"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Which was the first non test playing country to beat India in an international match?"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="15dp"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="vertical">

        <Button
            android:id="@+id/afghan"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="      Afghanistan"
            android:textStyle="bold"
            android:textColor="#fff"
            android:gravity="left|center_vertical"
            android:textAllCaps="false"
            android:textSize="15sp"
            android:background="@drawable/button_border"/>

        <Button
            android:layout_below = "@id/afghan"
            android:id="@+id/bang"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="   Bangladesh"
            android:textStyle="bold"
            android:textColor="#fff"
            android:gravity="left|center_vertical"
            android:textAllCaps="false"
            android:textSize="15sp"></Button>

        <Button
            android:id="@+id/srilanka"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="      Srilanka"
            android:textStyle="bold"
            android:textColor="#fff"
            android:gravity="left|center_vertical"
            android:textAllCaps="false"
            android:textSize="15sp"
            android:background="@drawable/button_border"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView

            android:layout_width="@dimen/_50sdp"
            android:layout_height="@dimen/_50sdp"
          android:background="@drawable/left_arrow"
            android:layout_gravity="bottom"
            android:layout_margin="@dimen/_20sdp"/>

        <ImageView
            android:layout_width="@dimen/_50sdp"
            android:layout_height="@dimen/_50sdp"
            android:background="@drawable/right_arrow"
            android:layout_gravity="bottom"
           android:layout_marginBottom="@dimen/_20sdp"
            android:layout_marginLeft="@dimen/_150sdp"/>
    </LinearLayout>
</LinearLayout>

Code for Activity is :

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.vshine.neuron.riseshine.R;

public class QuizActivity extends AppCompatActivity {
    Button btn1, btn2, btn3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
        btn1 = findViewById(R.id.afghan);
        btn2 = findViewById(R.id.bang);
        btn3 = findViewById(R.id.srilanka);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

        Toast.makeText(getApplicationContext(),"Sorry! Wrong Answer",Toast.LENGTH_LONG).show();

            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"Sorry! Wrong Answer",Toast.LENGTH_LONG).show();
            }
        });
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"Congratulation! Right Answer",Toast.LENGTH_LONG).show();
            }
        });

        getSupportActionBar().setTitle("Quiz");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle arrow click here
        if (item.getItemId() == android.R.id.home) {
            finish(); // close this activity and return to preview activity (if there is any)
        }

        return super.onOptionsItemSelected(item);
    }
    }
Umair Iqbal
  • 1,959
  • 1
  • 19
  • 38

1 Answers1

0

I'd suggest you do couple of things.

  1. Instead of hardcodding your buttons' onclick methods, try determining which button was clicked and was it the write answer or not.

  2. You can make a method which will check if the clicked button holds the right answer or not.

  3. For reusing the activity, you can simply update the textview (that holds the question) and the button's texts.

Mohammad Zain Abbas
  • 728
  • 1
  • 11
  • 24
  • Can you please tell me how can I update the textview? – Umair Iqbal Sep 19 '18 at 06:29
  • You can simply update it's text via "myTextView.setText("new text");" – Mohammad Zain Abbas Sep 19 '18 at 06:32
  • Check this https://stackoverflow.com/a/37982061/6390175 if you are still unclear. – Mohammad Zain Abbas Sep 19 '18 at 06:33
  • The method you are telling is for two questions I have multiple ones so how can this method can be applicable on more than 2 questions ? I will get ans set these question from API – Umair Iqbal Sep 19 '18 at 06:58
  • @Umair perhaps you didn't read my 3rd point. You can reuse the same activity for as many times as you want. I am suggesting that you can load your new question in textview and it's options by updating the button's text, whenever the activity reload itself (when you press next) – Mohammad Zain Abbas Sep 19 '18 at 07:22