How to programatically set BottomNavigationView background colour at run time










0














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










share|improve this question




























    0














    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










    share|improve this question


























      0












      0








      0







      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










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 19 '17 at 16:41









      Maxim

      29.8k2174121




      29.8k2174121










      asked Sep 19 '17 at 16:23









      Tom H.

      36019




      36019






















          3 Answers
          3






          active

          oldest

          votes


















          1














          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.






          share|improve this answer




















          • Of course! I don't know how I missed that. Thanks
            – Tom H.
            Sep 20 '17 at 7:51


















          2














          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






          share|improve this answer




















          • 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


















          0














          In your activity you can directly use such as:




          bottomNavigationView.setItemBackgroundResource((R.color.colorxyz));




          Hope this works for you.






          share|improve this answer




















            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
            );



            );













            draft saved

            draft discarded


















            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









            1














            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.






            share|improve this answer




















            • Of course! I don't know how I missed that. Thanks
              – Tom H.
              Sep 20 '17 at 7:51















            1














            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.






            share|improve this answer




















            • Of course! I don't know how I missed that. Thanks
              – Tom H.
              Sep 20 '17 at 7:51













            1












            1








            1






            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.






            share|improve this answer












            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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
















            • 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













            2














            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






            share|improve this answer




















            • 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















            2














            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






            share|improve this answer




















            • 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













            2












            2








            2






            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






            share|improve this answer












            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







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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
















            • 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











            0














            In your activity you can directly use such as:




            bottomNavigationView.setItemBackgroundResource((R.color.colorxyz));




            Hope this works for you.






            share|improve this answer

























              0














              In your activity you can directly use such as:




              bottomNavigationView.setItemBackgroundResource((R.color.colorxyz));




              Hope this works for you.






              share|improve this answer























                0












                0








                0






                In your activity you can directly use such as:




                bottomNavigationView.setItemBackgroundResource((R.color.colorxyz));




                Hope this works for you.






                share|improve this answer












                In your activity you can directly use such as:




                bottomNavigationView.setItemBackgroundResource((R.color.colorxyz));




                Hope this works for you.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 9:49









                mears

                4119




                4119



























                    draft saved

                    draft discarded
















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Darth Vader #20

                    How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                    Ondo