0

I am having a serious problem in my project and it is affecting several users using android <21. I change the colors of my navigationView however when using android <21 the application throws an exception. Larger versions work normally.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
navigationView.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(context, ThemeUtil.getThemeList().get(mTheme).getPrimaryColor())));
}else {
((TintableBackgroundView)navigationView).setSupportBackgroundTintList(ColorStateList.valueOf(tintColor));
}

Problem is in this line:

((TintableBackgroundView) navigationView) .setSupportBackgroundTintList (ColorStateList.valueOf (tintColor));

How can I use setSupportBackgroundTintList in navigationView?

Will V
  • 142
  • 11
  • Whats the exception?! – em_ Jan 25 '19 at 15:10
  • java.lang.ClassCastException: android.support.design.widget.NavigationView cannot be cast to android.support.v4.view.TintableBackgroundView – Will V Jan 25 '19 at 15:30
  • Why do you think that `NavigationView` is a `TintableBackgroundView`? – Mike M. Jan 25 '19 at 15:38
  • I wanted to take an example and just found this: https://stackoverflow.com/questions/33495164/unexpected-cast-to-appcompatbutton-layout-tag-was-button/33495477 – Will V Jan 25 '19 at 16:04
  • That's for specifically `AppCompatButton`, which does implement `TintableBackgroundView`. `NavigationView` does not, however. For versions prior to Lollipop, you'll have to set the tint list directly on the background `Drawable`. You can do something like is shown in [this answer](https://stackoverflow.com/a/49259711), substituting `DrawableCompat.setTintList()` for `DrawableCompat.setTint()`. – Mike M. Jan 25 '19 at 17:49
  • Mike, there is no "setBackgroundTint" method for android smaller API 21 How to do this? I think I should use "setSupportBackgroundTintList", but I'm not sure how to apply this to my NavigationView – Will V Jan 25 '19 at 18:17
  • I linked to a specific example – https://stackoverflow.com/a/49259711. What problems are you having with it? – Mike M. Jan 25 '19 at 18:19
  • In this example it is using `setBackground` and not `setBackgroundTintList` `setBackground` Changes the color of the menu, `setBackgroundTintList` changes the color of the title – Will V Jan 25 '19 at 18:27
  • mike look -> https://imgur.com/a/t76dDtm – Will V Jan 25 '19 at 18:32
  • "In this example it is using `setBackground` and not `setBackgroundTintList`" – Yes, that's why you apply the tint list to the background `Drawable`. "`setBackground` Changes the color of the menu, `setBackgroundTintListchanges` the color of the title" – I'm not sure what you're saying there. What do you mean by menu, exactly? And what title? I can't really make out that image. – Mike M. Jan 25 '19 at 18:46
  • The color is dark gray, should look blue as the whole menu. Please see the image again. – Will V Jan 25 '19 at 18:50
  • I don't know how you're doing that, exactly, but I just ran a quick test, and both methods work the same for me. https://drive.google.com/file/d/1JwKVx3HiCqelWxi_ot-tGLrNFWC8_4Ra/view?usp=drivesdk – Mike M. Jan 25 '19 at 19:07
  • That way you said it did not work! But I managed to do it successfully like this:: `navigationView.setBackgroundColor(ContextCompat.getColor(context, ThemeUtil.getThemeList().get(mTheme).getPrimaryColor()));` – Will V Jan 25 '19 at 19:16
  • Then you did something wrong, because that example is equivalent to `setBackgroundTintList()`. Furthermore, why didn't you just set the background color to begin with? If it's a solid color – which I can't really tell from that image – tinting it is rather pointless. You don't even need to do a version check. Just call `setBackgroundColor()`. – Mike M. Jan 25 '19 at 19:21
  • 1
    Yes I agree! Is that before I only used setBackgroundTintList, but to know that user with android with API <21 was having problems I put the version check. It stopped giving problem but the menu was still in problem. Important that it worked, thank you very much for your time in trying to work out and sorry for something. – Will V Jan 25 '19 at 19:31

0 Answers0