0

In mainActivity i take hexString then start new activity and send hexString to it and change background color with this hex string. i need to know if there is background color method can take argument hexString or long .

code:

String colorValue = getIntent().getStringExtra("colorHex"); findViewById(R.id.layout1).setBackgroundColor(Color.parseColor(colorValue));//want change in the argument or if there's another method. 

2 Answers 2

1

Google Documentation

Try with the function public static int parseColor (String colorString) That take a String and return the int color!

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 or one of the following names: 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray', 'grey', 'lightgrey', 'darkgrey', 'aqua', 'fuchsia', 'lime', 'maroon', 'navy', 'olive', 'purple', 'silver', 'teal'.

Try something like this

//find your layout LinearLayout ll = (LinearLayout) findViewById(R.id.yourLinearLayout); //Get the color from an EditText EditText newcolor = (EditText) findViewById(R.id.yourEditText); String stringColor = newcolor.getText().toString(): //Assume that you have the #RRGGBB //the function take only #RRGGBB with 6 values read documentation for more information int intColor = Color.parseColor(stringColor); //Set the color to the LinearLayout ll.setBackground(intColor); 
Sign up to request clarification or add additional context in comments.

6 Comments

not work it make new activity stop if i insert hex ex 0xfff.
try with #, like this public static int parseColor("#0xfff") Use a try and catch because If the string cannot be parsed, throws an IllegalArgumentException exception
Still get down when it start new activity.
You say that the app crash, when it crash you can find the error in the logcat, copy and paste the error when the app crash.
And post more code not only the 2 row so me and other user can give more help.
|
1

assume color yellow.

private static int yellow = Color.parseColor("#f9ce1e"); view.setBackgroundColor(yellow); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.