Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7630835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:12:03+00:00 2026-05-31T06:12:03+00:00

I want to display Correct when the keypad_hash is pressed once if the answer

  • 0

I want to display “Correct” when the keypad_hash is pressed once if the answer is right and “Incorrect” when it’s wrong, and when the keypad_hash is pressed AGAIN, it goes to the next random question.

Part of my XML is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/Title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:textSize="25dp" />

    <TextView
        android:id="@+id/Guess"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:text="Guess: "
        android:textSize="25dp" />

    <TextView
        android:id="@+id/Answer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:textSize="25dp" />

    <TextView
        android:id="@+id/Incorrect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

    <TextView
        android:id="@+id/CORRECT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="40dp"/>

The Java code is

package org.example.question;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class QuestionActivity extends Activity implements View.OnClickListener {
    /** Called when the activity is first created. */

    //variable for different questions
    int fnum0, snum0,fnum1, snum1,fnum2, snum2,fnum3, snum3,
    fnum4, snum4,fnum5, snum5,fnum6, snum6,fnum7, snum7,
    fnum8, snum8,fnum9, snum9, answer;

    //Variable and type declaration for buttons and text
    Button keyOne;
    Button keyTwo;
    Button keyThree;
    Button keyFour;
    Button keyFive;
    Button keySix;
    Button keySeven;
    Button keyEight;
    Button keyNine;
    Button keyDel;
    Button keyZero;
    Button keyHash;
    Button keySubtract;
    TextView display;
    TextView display1;
    TextView answer0;
    int q=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Display text on screen

        display1 = (TextView)findViewById(R.id.Guess);
        display = (TextView)findViewById(R.id.Title);
        answer0 = (TextView)findViewById(R.id.Answer);

        //Code for correct and incorrect

        //Assigning names to each keypad
        keyOne= (Button) findViewById(R.id.keypad_1);
        keyTwo= (Button) findViewById(R.id.keypad_2);
        keyThree= (Button) findViewById(R.id.keypad_3);
        keyFour= (Button) findViewById(R.id.keypad_4);
        keyFive = (Button) findViewById(R.id.keypad_5);
        keySix= (Button) findViewById(R.id.keypad_6);
        keySeven = (Button) findViewById(R.id.keypad_7);
        keyEight = (Button) findViewById(R.id.keypad_8);
        keyNine = (Button) findViewById(R.id.keypad_9);
        keyZero = (Button) findViewById(R.id.keypad_0);
        keySubtract = (Button) findViewById(R.id.keypad_subtract);
        keyHash = (Button) findViewById(R.id.keypad_hash);
        keyDel = (Button) findViewById(R.id.delete);

        //setting button to produce an event when each button is pressed
        keyOne.setOnClickListener(this);     keyTwo.setOnClickListener(this);     keyThree.setOnClickListener(this);
        keyFour.setOnClickListener(this);     keyFive.setOnClickListener(this);     keySix.setOnClickListener(this);
        keySeven.setOnClickListener(this);      keyEight.setOnClickListener(this);  keyNine.setOnClickListener(this);
        keySubtract.setOnClickListener(this);    keyHash.setOnClickListener(this);  keyDel.setOnClickListener(this);
    }

    public void onClick(View arg0) {
        //Created a new string object
        String str = new String();

        switch (arg0.getId()) {

            //what happens when hash button is pressed

            case R.id.keypad_hash:
            {
                //Generates random numbers

                fnum0 = (int) ((double) ((Math.random() * 10)));
                snum0 = (int) ((double) ((Math.random() * 10)));

                //Generates a random number between 0 to 9 with random operators for first and sceond number.
                int operation = (int) ((double) ((Math.random() * 10)));

                /* Generate random expression with random operators and display "?" if the answer is one digit
                 * or"??" if the answer is greater than 10.
                 */
                if(operation == 0)
                    str = fnum0+  "+"  +  snum0+  "=" + ((fnum0+snum0<10)? "?" : "??");
                if("Correct".equals(display.getText().toString()))
                {
                    display.setText("Correct");
                }
                else
                {
                    display.setText("Incorrect");
                }

                if(operation == 1)
                    str = fnum0 +  "-"  +  snum0+  "=" + ((fnum0-snum0<10)? "?" : "??");
                if("Correct".equals(display.getText().toString()))
                {
                    display.setText("Correct");
                }
                else
                {
                    display.setText("Incorrect");
                }
                if(operation == 2)
                   str = fnum0 +  "*"  +  snum0+  "=" +  ((fnum0*snum0<10)? "?" : "??");
                if("Correct".equals(display.getText().toString()))
                {
                    display.setText("Correct");
                }
                else
                {
                    display.setText("Incorrect");
                }
                if("Correct".equals(display.getText().toString()))
                {
                    display.setText("Correct");
                }
                else
                {
                    display.setText("Incorrect");
                }
                str = fnum0 +  "/"  +  snum0+  "=" +  ((fnum0/snum0<10)? "?" : "??");
                if("Correct".equals(display.getText().toString()))
                {
                    display.setText("Correct");
                }
                else
                {
                    display.setText("Incorrect");
                }
                display.setText(str);
                break;
            }

            case R.id.keypad_1:
                String str1 = display.getText().toString();
                display.setText(str1.replace("?", "1"));
                break;

            case R.id.keypad_2:
                String str2 = display.getText().toString();
                display.setText(str2.replace("?", "2"));
                break;

            case R.id.keypad_3:
                String str3 = display.getText().toString();
                display.setText(str3.replace("?", "3"));
                break;

            case R.id.keypad_4:
                String str4 = display.getText().toString();
                display.setText(str4.replace("?", "4"));
                break;

            case R.id.keypad_5:
                String str5 = display.getText().toString();
                display.setText(str5.replace("?", "5"));
                break;

            case R.id.keypad_6:
                String str6 = display.getText().toString();
                display.setText(str6.replace("?", "6"));
                break;

            case R.id.keypad_7:
                String str7 = display.getText().toString();
                display.setText(str7.replace("?", "7"));
                break;

            case R.id.keypad_8:
                String str8 = display.getText().toString();
                display.setText(str8.replace("?", "8"));
                break;

            case R.id.keypad_9:
                String str9 = display.getText().toString();
                display.setText(str9.replace("?", "9"));
                break;

            case R.id.delete:
                break;

            case R.id.keypad_0:
                String str0 = display.getText().toString();
                display.setText(str0.replace("?", "0"));
                break;

            case R.id.keypad_subtract:
                display.setText("-");
                break;
        }
    }

    public void requestFocus() {
        // TODO Auto-generated method stub
    }
}

It’s not displaying the “Correct” and “Incorrect”, but when I click keypad_hash ONCE it takes me to the next random question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:12:05+00:00Added an answer on May 31, 2026 at 6:12 am

    Depending on the operation, replace the line of code by:

    str = fnum0+  "+"  +  snum0+  "=" + ((fnum0+snum0<10) ? "?" : "??")
    

    or

    str = fnum0+  "*"  *  snum0+  "=" + ((fnum0*snum0<10) ? "?" : "??")
    

    or
    …

    For your information:

    int a = ( b ? c : d );
    // is shorthand for:
    if ( b )
    {
        a = c;
    }
    else
    {
        a = d;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to display some Arabic text from Right to Left. So I set
HI, I want to display 20 random images at a time over Activity screen
If I want to display, say, 4 decimal points, what is the correct format?
I want display data from database in Listbox...Here is my code, It is not
i want display 1 record from colums zodys , I'm programint in C# I
I have a simple quiz application and I want display a nice timer /
I have db with this table (TableToDo): http://goo.gl/NlTEk I want display all records in
In the header of the django admin, I want display a link. This link
I want to display some kind of animation when my datagrid is updating. Does
I want to display data in two columns as below Entry 1 Entry 2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.