Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Sean Walker
Sean Walker
1,792 Points

Button does Nothing!!!!

hey everyone,hope ya'll doing good....heeeeeeeeeeeeelp! i got huge problem with this button and i'm so confused .... pls help!

public class MainActivity extends AppCompatActivity {

private Button mfactButton;
private TextView mfactTextView;

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

    mfactButton = (Button) findViewById(R.id.button);
    mfactTextView = (TextView) findViewById(R.id.textView2);

    String [] facts = {};

    String fact = "";

    Random randomGenerator = new Random();
    int randomNumber = randomGenerator.nextInt(facts.length);

    fact = facts[randomNumber] + "";

    mfactTextView.setText(fact);

    View.OnClickListener Listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    };
    mfactButton.setOnClickListener(Listener);
Stephen Wall
Stephen Wall
Courses Plus Student 27,294 Points

Hey, Whatever you would like your button to do needs to go inside the onClick method inside of your OnClickListener. OnClickListeners handle click events, and can be applied to many different UI objects.

View.OnClickListener Listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             // Put Whatever you want your button to do in here, 
             // if this is empty the button won't do anything. 
        }
    };

1 Answer

Sean Walker
Sean Walker
1,792 Points

public class FactFun extends AppCompatActivity {

private Button mButton;
private TextView mTextView;


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

    mTextView = (TextView) findViewById(R.id.textView2);
    mButton = (Button) findViewById(R.id.button);


    View.OnClickListener Listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String[] facts = { "facts are crazy,i just care about codes!",
                               "i'm not myself",
                                "my coffee tests like shit!",
                                  "oh god,wtf am i doing?!",
                                    "may God keep my code bugless!",
                                     "the code is the life when you fall in love with it!"};

            String fact = "";


            Random randomGenerator = new Random();
            int randomNumber = randomGenerator.nextInt(facts.length);
            fact = facts[randomNumber] + "";

        }
    };
    mButton.setOnClickListener(Listener);

this is my code now , pls tell me why my button clicls, don't change the contect of the textview2....i'm stucked !!! help!

Sean Walker
Sean Walker
1,792 Points

omg , i've just forgot the mTextView.setText(fact);

:]