ahmed afzaal
ahmed afzaal

Reputation: 21

Howdo display random number generated on te scree in textView

i m trying to show the random number generated on the screen but for some reason its not coming up the screen looks like

 _______
 1  2  3
 4  5  6
 7  8  9
del 0 # -

random number are to be shown where"_" is

main.xml

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

question.Java

package org.example.question;

import android.app.Activity;
import android.os.Bundle;
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. */
    int fnum, snum;


Button one,two,three,four,five,six,seven,eight,nine,zero,minus,hash;
    TextView display;






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


        display = (TextView)findViewById(R.id.Title);




        //Buttons

        one= (Button) findViewById(R.id.keypad_1);
        two= (Button) findViewById(R.id.keypad_2);
        three= (Button) findViewById(R.id.keypad_3);
        four= (Button) findViewById(R.id.keypad_4);
        five = (Button) findViewById(R.id.keypad_5);
        six= (Button) findViewById(R.id.keypad_6);
        seven = (Button) findViewById(R.id.keypad_7);
        eight = (Button) findViewById(R.id.keypad_8);
        nine = (Button) findViewById(R.id.keypad_9);
        minus = (Button) findViewById(R.id.keypad_subtract);
        hash = (Button) findViewById(R.id.keypad_hash);

        one.setOnClickListener(this);     two.setOnClickListener(this);     three.setOnClickListener(this); 
        three.setOnClickListener(this);   four.setOnClickListener(this);     five.setOnClickListener(this);
        six.setOnClickListener(this);     seven.setOnClickListener(this);     eight.setOnClickListener(this);
        nine.setOnClickListener(this);     minus.setOnClickListener(this);   hash.setOnClickListener(this);

}




    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        display.setText("2+2");
    }




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

    }
}

help would be greatly apprecited

Upvotes: 0

Views: 787

Answers (1)

Akhil
Akhil

Reputation: 14058

You could use java.util.Random class to generate random numbers. For example :

 Random r  = new Random();
    int randomInt = r.nextInt(10);

This will generate a random number in the range 0 to 9. You can put this code in onClick() of a button and set it to a TextView.

Upvotes: 1

Related Questions