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 (2014) Basic Android Programming Initializing a Button

Initialize the showFactButton variable using the findViewById() method like the TextView above it. The ID for the button

what does this mean

3 Answers

Daniel Hartin
Daniel Hartin
18,106 Points

Initializing a variable simply put means making the variable name equal something. So you can declare a variable like: String aVariableName;

You would initialize it by putting the variable name equal to something:

String aVariableName = "My variable value";

In this particular code challenge it is asking to you to set the showFactButton variable's value, but because this is a reference to an object on your XML file we use a method call (findViewById) to set the variable correctly.

Button showFactButton = (Button) findViewById(R.id.showFactButton);

Hope this helps Daniel

When i type "(R.id.showFactButton); the showFactbutton part is not recognised and comes up as a red error on the side bar?

Daniel Hartin
Daniel Hartin
18,106 Points

Can you post your entire code please. The code above worked for me so i'm thinking something else is a miss.

Thanks so much for helping me btw :))

public class FunFactsActivity extends Activity {

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

    // Declare our code variable and asign them the views from the layout file
    final TextView factlabel = (TextView) findViewById(R.id.factTextView);
    Button showFactButton = (Button) findViewById(R.id.showFactButton);
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //the button was clicked so update the fact label with a new fact
            String fact = "Ostriches can run faster than horses";
            factlabel.setText(fact);
        }
    };
    showFactButton. setOnClickListener(listener);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.fun_facts, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

Daniel Hartin
Daniel Hartin
18,106 Points

OK, I thought this question related specifically to the code challenge which is why it might not work in your code but that's no problem we can work it out!.

I'm not sure if it is the font but it looks like you've got a space after your period (full stop) in the line showFactButton.setOnClickListener(listener); you should remove this space if you have.

The layout is set as activity_fun_facts so go into your XML with this name and make sure you have a Button element in this layout file and it has the line:

android:id="@+id/showFactButton"

It must be spelt exactly the same as in your java file. If you are having trouble paste your XML code as well and I can direct you from there. I highly suspect the id of your button is set as something different if it underlined in the Java code.

Hope this helps Daniel

OMG, thank you so much for helping me your fixed it with "android:id="@+id/showFactButton"" because it only said "@+id/button". Thanks so much!!

Daniel Hartin
Daniel Hartin
18,106 Points

No problem, glad it worked for you!