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 with Java Improving Our Code Dynamically Changing the Background Color

Noah Price
Noah Price
365 Points

Can't find relativeLayout id with findViewById

I am having an issue when I try to access the relativeLayout view object by id, it does not show up in auto-complete, and typing in the id manually results in an error.. I have ensured I am referring to it exactly the same as it is labeled inside of the activity_fun_facts.xml file, but it still appears red. I have also gone ahead and downloaded the course file for this particular episode and compared my source code with that provided using a separate program, but aside from the use of a cast which isn't necessary in my version, I can't see any difference in my code.

Related Source code: FunFactsActivity.java

public class FunFactsActivity extends AppCompatActivity {
    private FactBook factBook = new FactBook();
    // Declare our View Variables
    private TextView factTextView;
    private Button showFactButton;
    private RelativeLayout relativeLayout;

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

        // Assign the Views from the layout file to the corresponding variables
        factTextView = findViewById(R.id.factTextView);
        showFactButton = findViewById(R.id.showFactButton);
        relativeLayout = findViewById(R.id.relativeLayout); // this final part is where it turns red, and I am unable to find the id that is being referenced in my .xml file.

activity_fun_facts.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#51b64d"
    android:clickable="false"
    android:padding="50dp"
    android:id="@+id/relativeLayout" <!-- This is the id I have declared -->
    tools:context="com.rug.funfacts.FunFactsActivity">

3 Answers

Noah Price
Noah Price
365 Points

It seems that Android Studio doesn't like the id of 'relativeLayout'. I changed it to be 'funFactLayout' inside of the activity_fun_facts.xml file:

android:id="@+id/funFactLayout"

and went ahead with the exact same procedure inside of the FunFactsActivity.java

public class FunFactsActivity extends AppCompatActivity {
// ......
    private TextView factTextView;
    private Button showFactButton;
    private RelativeLayout relativeLayout;
// .....
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fun_facts);

        // Assign the Views from the layout file to the corresponding variables
        factTextView = findViewById(R.id.factTextView);
        showFactButton = findViewById(R.id.showFactButton);
        relativeLayout = findViewById(R.id.funFactLayout);
Marcus Hallenberg
Marcus Hallenberg
2,772 Points

i had the problem when i wrote the name Android:id="@+id/relativeLayout" i forgot the + and added it afterward it didnt work but when i wrote the exact same code without missing the + it worked same with the color cause of the auto helping i wrote color.RED instead of Color.RED so i just change the c to a C.

Same here.

Moira Lawrie-Martyn
Moira Lawrie-Martyn
8,073 Points

have you tried casting each of the views to their correct view type? Has that resolved the issue? Because I'd say that you're having a problem because Android doesn't know what it's looking for.

Noah Price
Noah Price
365 Points

In the newer version that I am using (I believe it is Ice Cream Sandwhich) just says that casting is redundant. When I wrote the code for the button and TextView, I did not require a cast either. The issue I had, which I resolved in my own answer above, is that android didn't like me naming the id of the RelativeLayout to RelativeLayout.

Thank you for your response anyway! :)