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

Joseph Henshaw
Joseph Henshaw
461 Points

Dynamically changing the background color of relativeLayout isnt working in android studio.

Changing the background color of a relative layout isnt working in android studio, i've cross-checked my code and i dont see anything wrong.

3 Answers

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

If you post the code snippet, we might be able to see what you're having issues with and potentially offer suggestions :)

Joseph Henshaw
Joseph Henshaw
461 Points
package com.hexagon.funfacts;


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;


public class fun_facts_activity extends AppCompatActivity {
    private UniversalFactBook UniFactBook = new UniversalFactBook();
    private ColorCycle colorcycle = new ColorCycle();

    //  Declare our view variables
    private TextView factTextView;
    private Button showFactButton;
    private RelativeLayout relativeLayoutOne;

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

//        Assign the views from the layout file to the corresponding variables
        factTextView = (TextView) findViewById(R.id.factTextView);
        showFactButton = (Button) findViewById(R.id.showFactButton);
        relativeLayoutOne = (RelativeLayout) findViewById(R.id.relativeLayoutOne);

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String fact = UniFactBook.getFact();
//                update the screen with a new fact
                factTextView.setText(fact);
                int color = colorcycle.getColor();
                relativeLayoutOne.setBackgroundColor(color);
                showFactButton.setTextColor(color);

            }
        };
        showFactButton.setOnClickListener(listener);
    }
}


package com.hexagon.funfacts;

import android.graphics.Color;

import java.util.Random;

/**
 * Created by JoJoe on 11/12/2017.
 */

public class ColorCycle {
    // Fields or Member variables -properties about the object
    String[] colors =  {
            "#39add1", // light blue
            "#3079ab", // dark blue
            "#c25975", // mauve
            "#e15258", // red
            "#f9845b", // orange
            "#838cc7", // lavender
            "#7d669e", // purple
            "#53bbb4", // aqua
            "#51b46d", // green
            "#e0ab18", // mustard
            "#637a91", // dark gray
            "#f092b0", // pink
            "#b7c0c7"  // light gray
    };

    //    Methods -Actions the objects can take
    public int getColor() {
//        Randomly select a fact
        Random randomGenerator = new Random();
        int randomNumber = randomGenerator.nextInt(colors.length);
        int color = Color.parseColor(colors[randomNumber]);
        return color;
    }


}
<?xml version="1.0" encoding="utf-8"?>
<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="?attr/colorPrimaryDark"
    android:backgroundTint="@color/colorPrimaryDark"
    android:padding="50dp"
    android:id="@+id/relativeLayoutOne"
    tools:context="com.hexagon.funfacts.fun_facts_activity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Did you know?"
        android:textColor="#80ffffff"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/factTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:text="Ants stretch when  they wake up in the morning"
        android:textColor="@android:color/white"
        android:textIsSelectable="false"
        android:textSize="24sp" />

    <Button
        android:id="@+id/showFactButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@android:color/white"
        android:text="Show Another Fact"
        android:textColor="@color/colorAccent" />

</RelativeLayout>
Joseph Henshaw
Joseph Henshaw
461 Points

I have no idea why the background color doesnt change when the button is clicked

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

Okay, I had a bit of a tinker with the code.

First off

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);-3

Needs correcting cause of the -3 after the ; otherwise it's going to throw an error.

Secondly, it looks like you're overriding the background color being set here:

android:backgroundTint="@color/colorPrimaryDark"

So only the tint is going to show up. Try fixing the first and removing the second and that should fix it :)