Android Build a Simple Android App Basic Android Programming Generating a Random Number

Steve McNutt
Steve McNutt
2,562 Points

[SOLVED] - Can someone help me fix this bug in Generating a Random Number?

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fun_facts);

    // Assign the views from layout file to the corresponding variables
    mFactTextView = (TextView) findViewById(R.id.factTextView);
    mShowFactButton = (Button) findViewById(R.id.showFactButton);

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // update to a new fact after the button has been clicked
            String fact = "";
            // Randomly select a new fact
            Random randomGenerator = new Random();
            int randomNumber = randomGenerator.nextInt(3);
            fact = randomNumber + "";

            // update the screen with our dynamic fact
            mFactTextView.setText(fact);
        }
    };
    mShowFactButton.setOnClickListener(listener);
}

This is the code that I have entered into FunFactsActivity.java. When you run the app on the emulator, it's supposed to make it so that each time you click the button a random number between 0 and 2 appears on the screen. For some reason, when I run the app and click the button, it becomes lateral, moves to the left side of the screen, and loses most of its width. It also does not display any number. I have looked back through other videos and my code matches, and the gradle gives me 0 errors and 0 warnings. I'm not sure if I need to provide more information, but does anyone have any idea why my button keeps re-sizing and relocating?

[SOLVED]

Steve McNutt
Steve McNutt
2,562 Points

Nevermind. I managed to find the error. Back in the XML sheet, I had my button layout:width set to wrap content instead of fill parent.

It turns out that it had nothing to do with the java file.

[SOLVED]

Jennifer Nordell
Jennifer Nordell
Treehouse Moderator

Try marking your question with [SOLVED] at the end. And btw, kudos on finding your fix! :smiley: