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

Help on FunFacts relativeLayout

Help, final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout); is not working...relativeLayout is showing red.

package com.example.muhammadmumu.funfacts;

import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.Random; import android.widget.RelativeLayout; import android.widget.Toast;

public class FunFactsActivity extends Activity {

public static final String TAG = FunFactsActivity.class.getSimpleName();

private FactBook mFactBook = new FactBook();
private ColorWheel mColorWheel = new ColorWheel();


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

    // Declare our View variables and assign the view from the layout file
    final TextView factLabel = (TextView) findViewById(R.id.factTextView);
    final Button showFactButton = (Button) findViewById(R.id.showFactButton);
    final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);

    Button showfactButton = (Button) findViewById(R.id.showFactButton);
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View View) {
        String fact = mFactBook.getFact();
        //Update the label with our dynamic fact
          factLabel.setText(fact);

          int color = mColorWheel.getColor();
          relativeLayout.setBackgroundColor(color);
          showFactButton.setTextColor(color);

        }
    };
    showfactButton.setOnClickListener(listener);

    // Toast.makeText(this, "Yay! Our activity was created!.", Toast.LENGTH_LONG).show();
    Log.d(TAG, "We're logging from the onCreate() method!");


}

}

1 Answer

Did you give your RelativeLayout the same id in the xml file?

Aha. Yeah I was forgot to input the id at xml file. Thanks!