Load Icon from firebase and set it as Icon for BottomNavigationBar












1















I am trying to load an Icon dinammically into my Bottom Navigation Bar.
Each time, the user clicks a new variable (in my case a new city) the icon for the variable shall be downloaded from the firebase server and be set to the bottom navigation bar.
My code till now looks like this:



// first I load the relevant Icon from firebase
iconRef = mFirebaseDatabase.getReference().child(city);
iconRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
cityIcon = (String) dataSnapshot.getValue();
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});

// this is for initiating bottom navigation bar
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
Menu menu = bottomNavigationView.getMenu();

// the following shall load the Id of the part in the bottom navigation bar, where the icon shall be passed to
ImageView iconView = findViewById(R.id.CityIcon);
Picasso.get().load(cityIcon).into(iconView);
menuItemCity.setTitle(cCity);
// the problem is, when I start the activity it crashes


Alternatively I thought this could help me:



// but the following gives me an error as I am only allowed to enter an Integer and I am entering a String 
MenuItem menuItemCity = menu.getItem(0);
menuItemCity.setIcon(cityIcon);


I was also thinking about first saving all the icons into the drawable folder. But as I want to be able to change the Icons after launch of the App it wouldn't serve my requirements. Though there might be a possibility to save the Icons into the drawable folder after downloading them from the server - maybe some of you people have any ideas how to solve this problem.










share|improve this question





























    1















    I am trying to load an Icon dinammically into my Bottom Navigation Bar.
    Each time, the user clicks a new variable (in my case a new city) the icon for the variable shall be downloaded from the firebase server and be set to the bottom navigation bar.
    My code till now looks like this:



    // first I load the relevant Icon from firebase
    iconRef = mFirebaseDatabase.getReference().child(city);
    iconRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    cityIcon = (String) dataSnapshot.getValue();
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
    });

    // this is for initiating bottom navigation bar
    bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
    Menu menu = bottomNavigationView.getMenu();

    // the following shall load the Id of the part in the bottom navigation bar, where the icon shall be passed to
    ImageView iconView = findViewById(R.id.CityIcon);
    Picasso.get().load(cityIcon).into(iconView);
    menuItemCity.setTitle(cCity);
    // the problem is, when I start the activity it crashes


    Alternatively I thought this could help me:



    // but the following gives me an error as I am only allowed to enter an Integer and I am entering a String 
    MenuItem menuItemCity = menu.getItem(0);
    menuItemCity.setIcon(cityIcon);


    I was also thinking about first saving all the icons into the drawable folder. But as I want to be able to change the Icons after launch of the App it wouldn't serve my requirements. Though there might be a possibility to save the Icons into the drawable folder after downloading them from the server - maybe some of you people have any ideas how to solve this problem.










    share|improve this question



























      1












      1








      1








      I am trying to load an Icon dinammically into my Bottom Navigation Bar.
      Each time, the user clicks a new variable (in my case a new city) the icon for the variable shall be downloaded from the firebase server and be set to the bottom navigation bar.
      My code till now looks like this:



      // first I load the relevant Icon from firebase
      iconRef = mFirebaseDatabase.getReference().child(city);
      iconRef.addListenerForSingleValueEvent(new ValueEventListener() {
      @Override
      public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
      cityIcon = (String) dataSnapshot.getValue();
      }

      @Override
      public void onCancelled(@NonNull DatabaseError databaseError) {

      }
      });

      // this is for initiating bottom navigation bar
      bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
      Menu menu = bottomNavigationView.getMenu();

      // the following shall load the Id of the part in the bottom navigation bar, where the icon shall be passed to
      ImageView iconView = findViewById(R.id.CityIcon);
      Picasso.get().load(cityIcon).into(iconView);
      menuItemCity.setTitle(cCity);
      // the problem is, when I start the activity it crashes


      Alternatively I thought this could help me:



      // but the following gives me an error as I am only allowed to enter an Integer and I am entering a String 
      MenuItem menuItemCity = menu.getItem(0);
      menuItemCity.setIcon(cityIcon);


      I was also thinking about first saving all the icons into the drawable folder. But as I want to be able to change the Icons after launch of the App it wouldn't serve my requirements. Though there might be a possibility to save the Icons into the drawable folder after downloading them from the server - maybe some of you people have any ideas how to solve this problem.










      share|improve this question
















      I am trying to load an Icon dinammically into my Bottom Navigation Bar.
      Each time, the user clicks a new variable (in my case a new city) the icon for the variable shall be downloaded from the firebase server and be set to the bottom navigation bar.
      My code till now looks like this:



      // first I load the relevant Icon from firebase
      iconRef = mFirebaseDatabase.getReference().child(city);
      iconRef.addListenerForSingleValueEvent(new ValueEventListener() {
      @Override
      public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
      cityIcon = (String) dataSnapshot.getValue();
      }

      @Override
      public void onCancelled(@NonNull DatabaseError databaseError) {

      }
      });

      // this is for initiating bottom navigation bar
      bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
      Menu menu = bottomNavigationView.getMenu();

      // the following shall load the Id of the part in the bottom navigation bar, where the icon shall be passed to
      ImageView iconView = findViewById(R.id.CityIcon);
      Picasso.get().load(cityIcon).into(iconView);
      menuItemCity.setTitle(cCity);
      // the problem is, when I start the activity it crashes


      Alternatively I thought this could help me:



      // but the following gives me an error as I am only allowed to enter an Integer and I am entering a String 
      MenuItem menuItemCity = menu.getItem(0);
      menuItemCity.setIcon(cityIcon);


      I was also thinking about first saving all the icons into the drawable folder. But as I want to be able to change the Icons after launch of the App it wouldn't serve my requirements. Though there might be a possibility to save the Icons into the drawable folder after downloading them from the server - maybe some of you people have any ideas how to solve this problem.







      android android-studio icons bottomnavigationview






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 16:54







      dunkiero

















      asked Nov 20 '18 at 16:16









      dunkierodunkiero

      345




      345
























          0






          active

          oldest

          votes











          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%2f53397179%2fload-icon-from-firebase-and-set-it-as-icon-for-bottomnavigationbar%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397179%2fload-icon-from-firebase-and-set-it-as-icon-for-bottomnavigationbar%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

          MongoDB - Not Authorized To Execute Command

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

          How to fix TextFormField cause rebuild widget in Flutter