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 Initializing a Button

Prashanth Kethavarapu
Prashanth Kethavarapu
461 Points

Ive not understood this concept Initialising the BUTTON

anyone explain me this concept

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class FunFactsActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {

        // Declare our View variables and assign them the Views from the layout file
        TextView showFactButton = (TextView) findViewById(R.id.showFactButton);
        Button showFactButton=(Button) findViewById(R.id.showFactButton);


1 Answer

Robin Damsgaard Larsen
Robin Damsgaard Larsen
13,929 Points

The initializing part is is when you assign a value (right side of the =) to a variable (left side of the =).

Since the TextView and Button are defined in XML, Android needs to access it in some way. This way is through the findViewById method, which is a method of the Activity class. It searches through the views defined in an XML file, which in this case is R.layout.activity_fun_facts, that you set in the setContentView() method.

The ID it searches for is the one given as argument to the method, which is R.id.showFactButton in your case. If the ID is specified in the XML, Android finds the view and returns it for you to manipulate in Java code.

Now, looking at your code, you as asking Android to go fetch both a TextView and a Button. However, you:

  • gave the variables the same names showFactButton; they can't have the same name if you want to manipulate both views. What happens is that first, the TextView is named showFactButton and initialized as a TextView. Immediately after, the same variable gets declared as a Button and initialized as a Button.
  • are searching for the same ID via the findViewById() method, but initialize both variables as different kinds of views, i.e. TextView and Button. Android will only find 1 view in the XML with the ID, but you're asking it to both be a TextView and a Button. This won't work.

What you need to do is to give the two variables different names (e.g. showFactButton for the Button, and factTextView for the TextView). Then go to the XML file and give both views unique IDs (e.g. the Button ID could be android:id=@+id/showFactButton and the TextView ID could be android:id=@+id/factTextView). Lastly, make sure to ask Android to fetch the proper kind of view for the variable you're initializing (e.g. Button showFactButton is initialized by finding the Button with the Button ID in the XML).

Hope this helps.

[Mod edit - promoted to Answer - sk]