[Solved] button doesn’t click in android studio


Your Button clicked properly but the main thing is you did not set fact value to TextView.

#. As you have declared Button and TextView outside onCreate(), no need to declare it again inside onCreate().

Use:

    mfactbutton = (Button) findViewById(R.id.button);
    mfacttext = (TextView) findViewById(R.id.textView2);

Instead of:

    Button mfactbutton = (Button) findViewById(R.id.button);
    TextView mfacttext = (TextView) findViewById(R.id.textView2);

#. In onClick() method show fact value on TextView or show Toast message:

    // TextView
    mfacttext.setText(fact);

    // Toast
    Toast.makeText(getApplicationContext(), "Fact: " + fact, Toast.LENGTH_SHORT).show();

Here is the working code:

public class MainActivity extends AppCompatActivity {

    private Button mfactbutton;
    private TextView mfacttext;


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

        mfactbutton = (Button) findViewById(R.id.button);
        mfacttext = (TextView) findViewById(R.id.textView2);

        // now we need to make out button to click
        View.OnClickListener Listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String[] facts = {
                        "Ants stretch when they wake up in the morning.",
                        "Ostriches can run faster than horses.",
                        "Olympic gold medals are actually made mostly of silver.",
                        "You are born with 300 bones; by the time you are an adult you will have 206.",
                        "It takes about 8 minutes for light from the Sun to reach Earth.",
                        "Some bamboo plants can grow almost a meter in just one day.",
                        "The state of Florida is bigger than England.",
                        "Some penguins can leap 2-3 meters out of the water.",
                        "On average, it takes 66 days to form a new habit.",
                        "Mammoths still walked the earth when the Great Pyramid was being built." };

                String fact = "";

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

                fact = facts[randomNumber] + "";

                mfacttext.setText(fact);
                Toast.makeText(getApplicationContext(), "Fact: " + fact, Toast.LENGTH_SHORT).show();
            }
        };

        mfactbutton.setOnClickListener(Listener);
    }
}

OUTPUT:

enter image description here

solved button doesn’t click in android studio