How to programatically set BottomNavigationView background colour at run time
I have a BottomNavigationView*
tab bar, and I want to set the background colour, however I don't have the colour until runtime. It appears as though setItemBackground
requires a resource id.
Is there some way of creating a resource out of a GradientDrawable
or something similar, then passing that new resource id?
Alternatively is there some other way of doing this without using XML that I've missed and would allow me to just pass a colour or Drawable
?
It seems weird that this is so difficult...
*actually using BottomNavigationViewEx
which is an extension of it from here, but it doesn't make much difference to my question: https://github.com/ittianyu/BottomNavigationViewEx
android android-layout android-resources bottomnavigationview
add a comment |
I have a BottomNavigationView*
tab bar, and I want to set the background colour, however I don't have the colour until runtime. It appears as though setItemBackground
requires a resource id.
Is there some way of creating a resource out of a GradientDrawable
or something similar, then passing that new resource id?
Alternatively is there some other way of doing this without using XML that I've missed and would allow me to just pass a colour or Drawable
?
It seems weird that this is so difficult...
*actually using BottomNavigationViewEx
which is an extension of it from here, but it doesn't make much difference to my question: https://github.com/ittianyu/BottomNavigationViewEx
android android-layout android-resources bottomnavigationview
add a comment |
I have a BottomNavigationView*
tab bar, and I want to set the background colour, however I don't have the colour until runtime. It appears as though setItemBackground
requires a resource id.
Is there some way of creating a resource out of a GradientDrawable
or something similar, then passing that new resource id?
Alternatively is there some other way of doing this without using XML that I've missed and would allow me to just pass a colour or Drawable
?
It seems weird that this is so difficult...
*actually using BottomNavigationViewEx
which is an extension of it from here, but it doesn't make much difference to my question: https://github.com/ittianyu/BottomNavigationViewEx
android android-layout android-resources bottomnavigationview
I have a BottomNavigationView*
tab bar, and I want to set the background colour, however I don't have the colour until runtime. It appears as though setItemBackground
requires a resource id.
Is there some way of creating a resource out of a GradientDrawable
or something similar, then passing that new resource id?
Alternatively is there some other way of doing this without using XML that I've missed and would allow me to just pass a colour or Drawable
?
It seems weird that this is so difficult...
*actually using BottomNavigationViewEx
which is an extension of it from here, but it doesn't make much difference to my question: https://github.com/ittianyu/BottomNavigationViewEx
android android-layout android-resources bottomnavigationview
android android-layout android-resources bottomnavigationview
edited Sep 19 '17 at 16:41
Maxim
29.8k2174121
29.8k2174121
asked Sep 19 '17 at 16:23
Tom H.
36019
36019
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
BottomNavigationViewEx
inherits from View
, which has a setBackground(Drawable drawable)
method. Use that if you're trying to set the background. If you want to change the background of the items, you will have to either subclass or create a custom implementation of BottomNavigationViewEx
as it doesn't look like it supports dynamically generated resources. But I don't see anything prohibiting such a thing.
Of course! I don't know how I missed that. Thanks
– Tom H.
Sep 20 '17 at 7:51
add a comment |
I don't know how it is done in BottomNavigationView
or even if it is possible(at least I didn't find anything in the docs that point out for an easy solution) but personally, I have also tried AHBottomNavigation
and it seems more complete. It also gives you the possibility of achieving what you want with:
bottomNavigation.setDefaultBackgroundColor(Color.parseColor("#FEFEFE"));
Link to git project: HERE
The other solution is more straightforward, but I will bear this alternative navigation bar in mind in case I need to change in future, thanks.
– Tom H.
Sep 20 '17 at 7:52
add a comment |
In your activity you can directly use such as:
bottomNavigationView.setItemBackgroundResource((R.color.colorxyz));
Hope this works for you.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46305411%2fhow-to-programatically-set-bottomnavigationview-background-colour-at-run-time%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
BottomNavigationViewEx
inherits from View
, which has a setBackground(Drawable drawable)
method. Use that if you're trying to set the background. If you want to change the background of the items, you will have to either subclass or create a custom implementation of BottomNavigationViewEx
as it doesn't look like it supports dynamically generated resources. But I don't see anything prohibiting such a thing.
Of course! I don't know how I missed that. Thanks
– Tom H.
Sep 20 '17 at 7:51
add a comment |
BottomNavigationViewEx
inherits from View
, which has a setBackground(Drawable drawable)
method. Use that if you're trying to set the background. If you want to change the background of the items, you will have to either subclass or create a custom implementation of BottomNavigationViewEx
as it doesn't look like it supports dynamically generated resources. But I don't see anything prohibiting such a thing.
Of course! I don't know how I missed that. Thanks
– Tom H.
Sep 20 '17 at 7:51
add a comment |
BottomNavigationViewEx
inherits from View
, which has a setBackground(Drawable drawable)
method. Use that if you're trying to set the background. If you want to change the background of the items, you will have to either subclass or create a custom implementation of BottomNavigationViewEx
as it doesn't look like it supports dynamically generated resources. But I don't see anything prohibiting such a thing.
BottomNavigationViewEx
inherits from View
, which has a setBackground(Drawable drawable)
method. Use that if you're trying to set the background. If you want to change the background of the items, you will have to either subclass or create a custom implementation of BottomNavigationViewEx
as it doesn't look like it supports dynamically generated resources. But I don't see anything prohibiting such a thing.
answered Sep 19 '17 at 16:45
JitterbugChew
30618
30618
Of course! I don't know how I missed that. Thanks
– Tom H.
Sep 20 '17 at 7:51
add a comment |
Of course! I don't know how I missed that. Thanks
– Tom H.
Sep 20 '17 at 7:51
Of course! I don't know how I missed that. Thanks
– Tom H.
Sep 20 '17 at 7:51
Of course! I don't know how I missed that. Thanks
– Tom H.
Sep 20 '17 at 7:51
add a comment |
I don't know how it is done in BottomNavigationView
or even if it is possible(at least I didn't find anything in the docs that point out for an easy solution) but personally, I have also tried AHBottomNavigation
and it seems more complete. It also gives you the possibility of achieving what you want with:
bottomNavigation.setDefaultBackgroundColor(Color.parseColor("#FEFEFE"));
Link to git project: HERE
The other solution is more straightforward, but I will bear this alternative navigation bar in mind in case I need to change in future, thanks.
– Tom H.
Sep 20 '17 at 7:52
add a comment |
I don't know how it is done in BottomNavigationView
or even if it is possible(at least I didn't find anything in the docs that point out for an easy solution) but personally, I have also tried AHBottomNavigation
and it seems more complete. It also gives you the possibility of achieving what you want with:
bottomNavigation.setDefaultBackgroundColor(Color.parseColor("#FEFEFE"));
Link to git project: HERE
The other solution is more straightforward, but I will bear this alternative navigation bar in mind in case I need to change in future, thanks.
– Tom H.
Sep 20 '17 at 7:52
add a comment |
I don't know how it is done in BottomNavigationView
or even if it is possible(at least I didn't find anything in the docs that point out for an easy solution) but personally, I have also tried AHBottomNavigation
and it seems more complete. It also gives you the possibility of achieving what you want with:
bottomNavigation.setDefaultBackgroundColor(Color.parseColor("#FEFEFE"));
Link to git project: HERE
I don't know how it is done in BottomNavigationView
or even if it is possible(at least I didn't find anything in the docs that point out for an easy solution) but personally, I have also tried AHBottomNavigation
and it seems more complete. It also gives you the possibility of achieving what you want with:
bottomNavigation.setDefaultBackgroundColor(Color.parseColor("#FEFEFE"));
Link to git project: HERE
answered Sep 19 '17 at 16:38
Ricardo
3,70011322
3,70011322
The other solution is more straightforward, but I will bear this alternative navigation bar in mind in case I need to change in future, thanks.
– Tom H.
Sep 20 '17 at 7:52
add a comment |
The other solution is more straightforward, but I will bear this alternative navigation bar in mind in case I need to change in future, thanks.
– Tom H.
Sep 20 '17 at 7:52
The other solution is more straightforward, but I will bear this alternative navigation bar in mind in case I need to change in future, thanks.
– Tom H.
Sep 20 '17 at 7:52
The other solution is more straightforward, but I will bear this alternative navigation bar in mind in case I need to change in future, thanks.
– Tom H.
Sep 20 '17 at 7:52
add a comment |
In your activity you can directly use such as:
bottomNavigationView.setItemBackgroundResource((R.color.colorxyz));
Hope this works for you.
add a comment |
In your activity you can directly use such as:
bottomNavigationView.setItemBackgroundResource((R.color.colorxyz));
Hope this works for you.
add a comment |
In your activity you can directly use such as:
bottomNavigationView.setItemBackgroundResource((R.color.colorxyz));
Hope this works for you.
In your activity you can directly use such as:
bottomNavigationView.setItemBackgroundResource((R.color.colorxyz));
Hope this works for you.
answered Nov 11 at 9:49
mears
4119
4119
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46305411%2fhow-to-programatically-set-bottomnavigationview-background-colour-at-run-time%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown