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

Christian Rodriguez
PLUS
Christian Rodriguez
Courses Plus Student 338 Points

Relative id not working

After adding android:layout="@+id/relativeLayout to the xml , the mRelativeLayout = (RelativeLayout) findViewById(R.id.relativelayout); the relativeLayout gives this error :Error:(30, 61) error: cannot find symbol variable relativelayout

3 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Based on what you posted there, it looks like you need to use a capital "L" for relativeLayout in both places. It's case sensitive, so using relativelayout in findViewById would throw that error. Case sensitivity is tricky!

Christian Rodriguez
Christian Rodriguez
Courses Plus Student 338 Points

Thanks I'll check it out !!! By the way i also have a problem with the COLOR.RED it works but when I open the app and click the button it crashes , I remove the variable and it works fine

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Can you paste in all your code? We can try to help you troubleshoot the Color.RED issue. "Color" should only have the C capitalized, for instance.

Christian Rodriguez
PLUS
Christian Rodriguez
Courses Plus Student 338 Points

My funfact.java

package unclaimedcorp.funfacts;

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

import java.util.Random;

public class FunFactsActivity extends AppCompatActivity {
    private FactBook mFactBook = new FactBook();

    // declare our view variable

    private TextView mFactTextView;
    private Button mShowFactButton;
    private RelativeLayout mRelativeLayout;

    @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

        mFactTextView = (TextView) findViewById(R.id.FactTextView);
        mShowFactButton = (Button) findViewById(R.id.ShowFactButton);
        mRelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String fact = mFactBook.getFact();

                // Update the screen with our dynamic fact

                mFactTextView.setText(fact);
                mRelativeLayout.setBackgroundColor(Color.BLUE);
            }
        };

        mShowFactButton.setOnClickListener(listener);


}
}

and the xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:layout="@+id/relativeLayout"
    tools:context="unclaimedcorp.funfacts.FunFactsActivity"
    android:background="#51b46d">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Did you know!?"
        android:textColorHighlight="@android:color/white"
        android:textColorHint="@android:color/transparent"
        android:textColorLink="@android:color/transparent"
        android:textSize="24sp"
        tools:textColorHighlight="?attr/colorBackgroundFloating"
        android:textAllCaps="false"
        android:textColor="@color/colorAccent" />

    <TextView
        android:text="Ants stretch when they wake in the morning "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="214dp"
        android:id="@+id/FactTextView"
        android:textSize="24sp"
        android:textColor="@android:color/white" />

    <Button
        android:text="Show another Fun Fact"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ShowFactButton"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@android:color/white" />
</RelativeLayout>

Thanks for replying ! appreciated , i'm really hooked to this course!

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Line 11 in your XML layout should start with android:id, not android:layout:

What you have:

android:layout="@+id/relativeLayout"

What it should be:

android:id="@+id/relativeLayout"

This should allow you to use Color.BLUE, etc, like you have in the Activity's code.