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 Build a Simple Android App Basic Android Programming Adding the OnClick Method

Roland Joseph
Roland Joseph
1,210 Points

I can't get my button to work. I typed in the java code just like the video, but nothing happens when I click my button

All I see is the original text about ants stretching. It doesn't switch to the text about Ostriches after clicking the button

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey Rolang,

can you paste the code plz?

Roland Joseph
Roland Joseph
1,210 Points

Here is my code...

package com.rojo.funfacts;

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView;

public class FunFactsActivity extends AppCompatActivity { // Declare our view variables private TextView mFactTextView; private Button mShowFactButton;

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

    // Assign the views from the layout file in 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) {
            // The button was clicked, so update the fact TextView with a new fact
            String fact = "Ostriches can run faster than horses.";
            mFactTextView.setText(fact);
        }
    };
    mShowFactButton.setOnClickListener(listener);
}

}

4 Answers

Roland Joseph
Roland Joseph
1,210 Points

I just had to relaunch the emulator and it worked.

Roland Joseph
Roland Joseph
1,210 Points

Thank your Grigorij!!

I relaunched the emulator and it worked.

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Great !

Always the easy things that makes us crazy :)

And I am sure that your code you have posted in the beginning would also work fine.

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Roland,

here is my code suggestion:

package com.example.nexuskiller.testproject;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView mTextView;
    private Button mButton;

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

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

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String fact = "Ostriches can run faster than horses.";
                mTextView.setText(fact);
            }
        });

    }
}

I have implemented the onClick() and setText() logic inside the setOnClickListener().

Let me know if it was helpful.

Grigorij

Roland Joseph
Roland Joseph
1,210 Points

Thank you for you help. I used your suggestion but it still isn't working. Here is my code...

package com.rojo.funfacts;

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView;

public class FunFactsActivity extends AppCompatActivity { // Declare our view variables private TextView mFactTextView; private Button mShowFactButton;

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

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

    mShowFactButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // The button was clicked, so update the fact TextView with a new fact
            String fact = "Ostriches can run faster than horses.";
            mFactTextView.setText(fact);
        }
    });
}

}

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

To be honest, I have no clue why the code isnΒ΄t working. Have you checked the xml file? IdΒ΄s are set properly? Reloaded the project? Tried to run the app on the physical phone?

Let me know if you have found a trick

Grigorij

omprakash krishna
omprakash krishna
7,068 Points

Roland Joseph....your code work fine!!!..thnx a lot...