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

hello i want to change the layout background color.but by taken the color from the user him self.he enter the color

I want to take the color name from the user.and use this color and change the layout backgroun with it

1 Answer

Cam Richardson
seal-mask
MOD
.a{fill-rule:evenodd;}techdegree
Cam Richardson
Front End Web Development Treehouse Moderator 16,895 Points

Check out the parseColor method from Android's Color class (from the documentation):

Parse the color string, and return the corresponding color-int. If the string cannot be parsed, throws an IllegalArgumentException exception. Supported formats are:

  • #RRGGBB
  • #AARRGGBB

The following names are also accepted: red, blue, green, black, white, gray, cyan, magenta, yellow, lightgray, darkgray, grey, lightgrey, darkgrey, aqua, fuchsia, lime, maroon, navy, olive, purple, silver, and teal.

You can add an EditText or Spinner view (populated with predefined strings from above) and then get the value of the view and use the Color.parseColor method to convert the user's response into a color you can apply to the layout's background.

Let me know if you need any more help with this!

UPDATE: Here is a working example:

package com.example.cam.colorparseexample;

import android.graphics.Color;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText editText;
    private ConstraintLayout layout;
    private Button button;

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

        editText = findViewById(R.id.editText);
        layout = findViewById(R.id.layout);
        button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setBackgroundColor(editText.getText().toString());
            }
        });
    }

    private void setBackgroundColor(String colorToParse) {
        try {
            int color = Color.parseColor(colorToParse);
            layout.setBackgroundColor(color);
        } catch(IllegalArgumentException iae) {
            Toast.makeText(this, "Invalid color entered!", Toast.LENGTH_LONG).show();
        }
    }
}

thanks for the help but I still dosen't understand how can I take the color that the user write on the EditText. to put him on the parseColor to use it like a color to change the background.

hello thanks a lot it work well.but one more issue when I'm enter other colers he dosen't recognize the colore like orange or violet. so I'm asking how to add more colors . and thanks a lot again you for your help.

Cam Richardson
seal-mask
.a{fill-rule:evenodd;}techdegree
Cam Richardson
Front End Web Development Treehouse Moderator 16,895 Points

zaka ettoum Per the android documentation it only accepts named values of red, blue, green, black, white, gray, cyan, magenta, yellow, lightgray, darkgray, grey, lightgrey, darkgrey, aqua, fuchsia, lime, maroon, navy, olive, purple, silver, and teal. Any other colors must be specified in hexadecimal like #RRGGBB.