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 Adding More Colors

Dynamically change background color

my background wont change at all. where is problem, can you help me ? here is code

Main.java
package com.example.daqa020.funfacts;

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

public class Main extends AppCompatActivity {
    private FactBook factBook=new FactBook();
    private ColorWheel colorWheel = new ColorWheel();
//deklarisanje View varijabli
    private TextView factTextView;
    private RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);



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

        // dodeljivanje Views iz layout file korespondirajucim varijablama

        factTextView = (TextView) findViewById(R.id.factTextView);
        Button button = (Button) findViewById(R.id.button);

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String fact = factBook.getFact;


                //update screen with our new fact

                factTextView.setText(fact);

                String color = colorWheel.getColor();
                relativeLayout.setBackgroundColor(color);
            }
        };
        button.setOnClickListener(listener);

    }


}

and

ColorWheel.java
package com.example.daqa020.funfacts;

import android.graphics.Color;

import java.util.Random;

/**
 * Created by daqa020 on 11/30/2017.
 */

public class ColorWheel {
    //Fields or Member variables / properties about object
//Methods / Actions the objects can take

    public 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
    };


    /**
     *
     */
    public int getColor{
        //the update button was cliked so update the factTextView with new fact
        //randomly select the fact
        Random randomGenerator = new Random();
        int randomNumber = randomGenerator.nextInt(10);
        int color = Color.parseColor();
        return color;};
}; 

[MOD: edited code blocks - srh]

3 Answers

Hi there,

Your color selection isn't right.

And you've got some extra semi-colons in there. Try something like this - look at the video at 1m13s for this code.

    public int getColor(){
        Random randomGenerator = new Random();
        int randomNumber = randomGenerator.nextInt(colors.length);
        int color = Color.parseColor(colors[randomNumber]);
        return color;
    } // no ;
}     // close the class - no ;

I hope that helps,

Steve.

thank you for fast answer steve. i changed it and still doesn't work. here is code ColorWhele.java

public int getColor(){
        //the update button was cliked so update the factTextView with new fact
        //randomly select the fact
        Random randomGenerator = new Random();
        int randomNumber = randomGenerator.nextInt(colors.length);
        int color = Color.parseColor(colors[randomNumber]);
        return color;}

and

MainActivity.java
View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String fact = factBook.getFact;


                //update screen with our new fact

                factTextView.setText(fact);

                int color = colorWheel.getColor();
                relativeLayout.setBackgroundColor(color);
            }
        };

Strange - that looks fine. What error are you getting? Can you copy the errors from the Logcat at the bottom of the Android Studio window?

You've assigned the relativeLayout instance outside of onCreate. I'm wondering if you can't do that. The onCreate method runs as the activity is created. Maybe you can't assign to the RelativeLayout until the activity has been created?

Try formatting your code the same as in the video. Also, have you definitely called your button in the XML, button? rather that showFactButton?

yes, in my code showFactButton is called just "button". i named it in start button. only error that i have is in FactBook.java

package com.example.daqa020.funfacts;

import java.util.Random;

/**
 * Created by daqa020 on 11/29/2017.
 */

class FactBook{
//Fields or Member variables / properties about object
//Methods / Actions the objects can take
public String[] facts={"Lorem ipsum dolor sit amet, consectetur adipisicing elit. Placeat consectetur minus quaerat cupiditate cumque, laboriosam laborum. Saepe et id animi placeat eius rerum molestiae iste nulla quisquam voluptate, esse similique maxime, asperiores quam autem nam neque at maiores voluptatem! Atque distinctio quo quam illo accusamus sequi, nisi ab ex repellat qui consectetur ut facere! ",
        "Laudantium, accusamus. Iusto deserunt totam recusandae dolore iure dicta quos veniam nam porro neque adipisci, ducimus ipsa molestias, optio quidem at assumenda voluptatem placeat velit.",
        "Exercitationem, odit error sit labore possimus quis laudantium cumque adipisci, repellendus sequi facilis obcaecati pariatur nobis provident accusantium, eum, iste temporibus repudiandae quia! Voluptate accusamus officiis in illo, aliquid voluptates natus mollitia non.",
        "Eos libero ipsa animi rerum neque deleniti quas porro, possimus aperiam pariatur officia fugiat placeat ipsum corrupti fugit, nobis.",
        "Veritatis, eveniet est. Eius cupiditate dicta, nemo omnis blanditiis iure rem perferendis temporibus!",
        "Explicabo facilis doloremque consectetur dolores perspiciatis officia. Qui rerum eos odio aliquam, neque cupiditate quaerat praesentium iste quae eligendi beatae dolorem et error ducimus expedita odit tempora sint? ",
        "Sapiente dolorem libero harum! Adipisci ad in vero consectetur! Itaque perspiciatis est quos necessitatibus earum quaerat voluptas vel, impedit eaque tempore!",
        " Repellat nostrum, non distinctio error libero temporibus unde sed ullam ipsam. Facilis voluptatem, error non voluptates facere odit voluptatibus. ",
        " Corporis dolorum, quae consequatur rerum hic, laborum ipsum inventore ut molestias minima excepturi temporibus numquam magni aliquid eius ipsam non quod voluptate delectus cumque ullam ea eligendi illo itaque?",
        "Odio praesentium, commodi ipsa magni, maxime quia fugiat, laboriosam mollitia suscipit incidunt unde corporis illum obcaecati saepe eos eveniet doloremque officiis molestias optio libero natus alias."};

        /**
         *
         */
        String getFact{
        //the update button was cliked so update the factTextView with new fact
        //randomly select the fact
        Random randomGenerator = new Random();
        int randomNumber = randomGenerator.nextInt(10);
        return facts[randomNumber];}
}

says : String getFact ' ; expected '

other one says : getFact 'return outside method'

i will start from start, i just want to know where have i'am do it wrong. tnx steve

Hiya,

Your method takes no parameters but still needs the brackets:

String getFact(){
  // code
}

Steve.