1

I am trying to change the drawer icon of the toggle button which is used to open and close the navigation drawer. But all my efforts have failed and I am unsuccessful. Kindly help me on this.

val d = BitmapDrawable(this.getResources(), path) as Drawable

            toggle!!.setHomeAsUpIndicator(d)

            if (path != null && path != "") {
                Picasso.with(this)?.load(path.toString())?.placeholder(resources?.getDrawable(R.drawable.ic_add_a_photo_black_24dp))?.error(
                    resources.getDrawable(
                        android.R.drawable.ic_input_add
                    )
                )!!.into(userPhotoImv)
            }
md gouse
  • 527
  • 1
  • 6
  • 22
  • mToolbar.setNavigationIcon(R.drawable.navIcon); use this. This is for java you need to change it for kotlin – pratik vekariya Jan 23 '20 at 06:28
  • i am changing it using imagepath from server but its blank – md gouse Jan 23 '20 at 06:52
  • @mdgouse please chek updated answer may this helps you. – InsaneCat Jan 23 '20 at 07:47
  • If my answer helps you then please mark as a right my answer from tick mark otherwise i'm gonna remove this answer brother : https://www.google.com/search?q=right+mark+as+an+answer+stackoverflow&sxsrf=ACYBGNQVsfXj9VeT9WRKWerxeSRoAMe2uQ:1579936139192&source=lnms&tbm=isch&sa=X&ved=2ahUKEwiQk-DimJ7nAhWBc30KHdm9CGAQ_AUoAXoECAwQAw&biw=1517&bih=730#imgrc=9_JN49MpzDykBM: – InsaneCat Jan 25 '20 at 07:44

3 Answers3

0

Posting my answer in java it might help.

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
toggle.setDrawerIndicatorEnabled(false);
toggle.setHomeAsUpIndicator(R.drawable.ic_custom_drawer_icon);

Set Liteners for toggle.

toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                drawer.openDrawer(GravityCompat.START);
            }
        }
    });
Muhammad Zahab
  • 1,049
  • 10
  • 21
0

Try this way

First creating bitmap image from url

try {
    URL url = new URL("http://....");
    Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch(IOException e) {
    System.out.println(e);
}

Now initialize navigation drawer like

 ActionBarDrawerToggle  toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawerLayout.addDrawerListener(toggle);
    toggle.syncState();

    navView.setNavigationItemSelectedListener(this);
    //change navigation icon
    toggle.setDrawerIndicatorEnabled(false);

    Drawable drawable = new BitmapDrawable(getResources(), bitmap);

    toggle.setHomeAsUpIndicator(drawable);

Note: this is java working code, you need to change in kotlin(i think you can direct convert it to kotlin when you copy this in your class)

hope this work :)

pratik vekariya
  • 1,105
  • 1
  • 8
  • 14
0

Normally you can set like below code snippet:

        Toolbar chatbox_toolbar = findViewById(R.id.toolbar);
        chatbox_toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        setSupportActionBar(chatbox_toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.mipmap.ic_back_arrow_gray);

And if you want to add a dunamic image from URL then pls wait i'll update here in a while

Using Glide Library:

        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayUseLogoEnabled(true);

        Glide.with(this).asDrawable().load(YourImageUrl).into(new CustomTarget<Drawable>() {
        @Override
        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
            getSupportActionBar().setLogo(resource);
        }
       @Override
       public void onLoadCleared(@Nullable Drawable placeholder) {
      }
});

Using Piccasso Library:

final ActionBar MyActionBar = getSupportActionBar();
Picasso.with(this)
       .load(YourImageUrl)
       .into(new Target()
       {
           @Override
           public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from)
           {
               Drawable drawble = new BitmapDrawable(getResources(), bitmap);
               MyActionBar .setIcon(drawble);
               MyActionBar .setDisplayShowHomeEnabled(true);
               MyActionBar .setDisplayHomeAsUpEnabled(true);
           }

           @Override
           public void onBitmapFailed(Drawable errorDrawable)
           {
           }

           @Override
           public void onPrepareLoad(Drawable placeHolderDrawable)
           {
           }
       });   

If not get your result then hide your actionbar home button and take a Imageview in Custom Header Toolbar and then set Image using Glide or piccaso from your Image URL. if you need help into this then let me know further first try above code snippets.

Try out this may helps you

InsaneCat
  • 2,115
  • 5
  • 21
  • 40