Karishma Patel
Karishma Patel

Reputation: 499

How to change string color into color resource?

I want to change my header background color and I am using the MaterialDrawer library:

   AccountHeader headerResult = new AccountHeaderBuilder()
            .withActivity(this)                   
  .withHeaderBackground(Color.parseColor(mPrefs.getString("theme_color",getResources().getString(R.string.default_color)))
  .withSelectionListEnabledForSingleProfile(false)
            ... and so on 

But with header background, I got the error message "Expected a color resource id (R.color.) but received an RGB integer" and not able to set background color read from preferences. it give Error on log : "android.content.res.Resources$NotFoundException: Resource ID #0x2e60e8"

Upvotes: 4

Views: 3282

Answers (6)

karan
karan

Reputation: 8853

Creat ColorDrawable from your string and you can set it to your header

int col = Color.parseColor(mPrefs.getString("theme_color",getResources().getString(R.string.default_color)));
ColorDrawable cd = new ColorDrawable();
cd.setColor(col);

headerResult = new AccountHeaderBuilder()
            .withActivity(this)
            .withCompactStyle(false)
            .withHeaderBackground(cd)
            .withSavedInstance(savedInstanceState)
            .build();

Upvotes: 1

Rohit Singh
Rohit Singh

Reputation: 18202

You can do this in Two steps

Lets say you have a color #8080000

1. Convert your Hex to int first

int yourColor = Color.parseColor("#808000");

2. Set Background

.setBackgroundColor(context.getColor(yourColor));

Upvotes: -1

forpas
forpas

Reputation: 164099

The signature of withHeaderBackground() you're using is this:

public AccountHeaderBuilder withHeaderBackground(@DrawableRes int headerBackgroundRes)

but you're passing an int value of some color.
You need a color or drawable resource id.

Upvotes: 0

navylover
navylover

Reputation: 13569

Color.parseColor() parse the color string, and return the corresponding color-int, this int value is different from resource ID, even two values are also int type.

You could do like this:

int colorID =
    getResourceID("your_color_name", "color", getApplicationContext());

No needs for using Color.parseColor() method.

Upvotes: 0

shriakhilc
shriakhilc

Reputation: 3000

Firstly, you should mention in the question that you are using the MaterialDrawer library, since AccountHeader is not a part of the standard Android SDK.

That said, examining the source of AccountHeaderBuilder in the repo shows that there are 3 variants for the withHeaderBackground method, that take a Drawable, a @DrawableRes int and a ImageHolder respectively.

If you are insistent on loading the color from strings.xml, then I think the following method would work:

Create a ColorDrawable using your string, which should be formatted as 0xAARRGGBB

Since the ColorDrawable class extends Drawable, it should be a valid argument for the withHeaderBackground method. Your code would look like this:

ColorDrawable cd = new ColorDrawable(getResources().getString(R.string.default_color));

AccountHeader headerResult = new AccountHeaderBuilder()
            .withActivity(this)                   
  .withHeaderBackground(cd)
  .withSelectionListEnabledForSingleProfile(false)
            ... and so on 

A simpler method would be to simply have a drawable resource and use that as R.drawable.default_drawable instead.

Upvotes: 0

deluxe1
deluxe1

Reputation: 857

Save the color HEX code in colors.xml like this: #AABBEE (use the color's hex code here) Then, set the background:

  .withHeaderBackground(R.color.myColor)
  .withSelectionListEnabledForSingleProfile(false)
  ....

Upvotes: 0

Related Questions