Skip to content Skip to sidebar Skip to footer

Creating A Button

I am trying to create a button which would start the randomGenerator. Random randomGenerator = new Random(); int randomInt = randomGenerator.nextInt(9); String wordList[] =

Solution 1:

Try something like this in the onCreate method:

ButtonbtnStart= (Button) findViewById(R.id.button1);
TextViewtv= (TextView) findViewById(R.id.textview1);

btnStart.setOnClickListener(newOnClickListener() {
    @OverridepublicvoidonClick(final View v) {
        tv.setText(wordToDisplay);
        }
    });

And don't forget to set an id in the TextView. i.e.

<TextView
    android:id="textview1"
...

Solution 2:

Button mButton; 
TextView mTextView;
String wordList[] = newString[9];


publicvoidonCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main); 

mButton = (Button)findViewById(R.id.button1); 
mTextView = (TextView) findViewById(R.id.textview1);
mButton.setOnClickListener(onClick); 


    {
        wordList[0] = "Mexican";
        wordList[1] = "American";
        wordList[2] = "Barbeque";
        wordList[3] = "Chinese";
        wordList[4] = "Indian";
        wordList[5] = "Italian";
        wordList[6] = "Thai";
        wordList[7] = "Viatnamese";
        wordList[8] = "Middle Eastern";

    }

    }



    privateView.OnClickListener onClick= newView.OnClickListener() {
            // @OverridepublicvoidonClick(View v) {

            Random randomGenerator = newRandom();
         int randomInt = randomGenerator.nextInt(9);
         String wordToDisplay = wordList[randomInt];
        mTextView.setText(wordToDisplay);
        }

    };

NOTE : In your code, your TextView is missing the id attribute. Make sure you do add it.

<TextView
    android:id="textview1"
...

Solution 3:

If I got you right, You should add this to your onCreate():

finalButtonbutton1= (Button) findViewById(R.id.button1);
     button1.setOnClickListener(newView.OnClickListener() {
         publicvoidonClick(View v) {
             // Perform action on clickRandomrandomGenerator=newRandom();
             intrandomInt= randomGenerator.nextInt(9);
             StringwordToDisplay= wordList[randomInt];

             // display in TextViewTextViewtv= (TextView) findViewById(R.id.textview1);
             tv.setText(wordToDisplay);
         }
     });

And add android:id="textview1" to your TextView xml.

Solution 4:

You can do something like this:

Add android:onClick="randomGenerator" to your button attribute. Then in your activity. make a function that will be called when your button is clicked. (I assumed that you wanted to do your random generation code when the button is clicked.)

publicvoidrandomGenerator(View view){

    Random randomGenerator = newRandom();
    int randomInt = randomGenerator.nextInt(9);
    String wordList[] = newString[9];
    {
        wordList[0] = "Mexican";
        wordList[1] = "American";
        wordList[2] = "Barbeque";
        wordList[3] = "Chinese";
        wordList[4] = "Indian";
        wordList[5] = "Italian";
        wordList[6] = "Thai";
        wordList[7] = "Viatnamese";
        wordList[8] = "Middle Eastern";

    }

    String wordToDisplay = wordList[randomInt];

}

Hope that helps.

Post a Comment for "Creating A Button"