0

i am setting a drawrlayout background using Picasso, and looked at this question and followed the steps, it works but the weird thing that is happening is that it doesn't update the background when the activity is first opened (meaning I have to close it then open it again and the background appears)

this is my layout

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary" />
<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_store"
    app:menu="@menu/activity_store_drawer" />

</android.support.v4.widget.DrawerLayout>

EDIT: after the suggestion and this is my activity class :

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

public class Store extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    final DrawerLayout layout = (DrawerLayout) Store.this.findViewById(R.id.drawer_layout);
    Target t = new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            final Bitmap p = bitmap;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    layout.setBackgroundDrawable(new BitmapDrawable(Store.this.getResources(), p));
                }
            });
            Log.d("TAG", "Loaded");
        }

        @Override
        public void onBitmapFailed(final Drawable errorDrawable) {
            Log.d("TAG", "FAILED");
        }

        @Override
        public void onPrepareLoad(final Drawable placeHolderDrawable) {
            Log.d("TAG", "Prepare Load");
        }
    };

    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {

        return true;
    }
}
Community
  • 1
  • 1
user1
  • 254
  • 1
  • 15

2 Answers2

0
runOnUiThread(new Runnable() {
    @Override
    public void run() {
        layout.setBackgroundDrawable(new BitmapDrawable(Store.this.getResources(), bitmap));
    }
});
wogsland
  • 9,106
  • 19
  • 57
  • 93
Nikhil Borad
  • 2,065
  • 16
  • 20
0

Picasso holds a weak reference to Target objects, hence it is likely to be garbage collected. So create a separate class of Target and set the data. Also you are using wrong layout for background change.

public class Store extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

private DrawerLayout layout;
private NavigationView navigationView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.toolbar_layout);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setHomeButtonEnabled(false);
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, R.string.app_name, R.string.app_name);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    layout = (DrawerLayout) Store.this.findViewById(R.id.drawer_layout);

    Picasso.with(this).load("http://a2.mzstatic.com/us/r30/Purple5/v4/58/2e/b9/582eb968-988b-ad42-dee6-92d396cbde5c/icon256.png").into(mTarget);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {

    return true;
}

final Target mTarget = new Target() {
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
        Log.d("DEBUG", "onBitmapLoaded");
        BitmapDrawable mBitmapDrawable = new BitmapDrawable(getResources(), bitmap);
        navigationView.setBackground(mBitmapDrawable);
    }

    @Override
    public void onBitmapFailed(Drawable drawable) {
        Log.d("DEBUG", "onBitmapFailed");
    }


    @Override
    public void onPrepareLoad(Drawable drawable) {
        Log.d("DEBUG", "onPrepareLoad");
    }
};
}