Irfan Akram
Irfan Akram

Reputation: 29

Changing navigation view header image programmatically is not working

I'm trying to change the Navigation view's header image on clicking drawer menu icon programmatically but now working.
How to solve this problem?

Here's my DrawerActivity class`

public class DrawerActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drawer);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(120, 120);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    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.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout  drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.drawer, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

Here's my AriesActivity , on which I'm using drawer menu icon, on clicking drawer has been opened.

public class AriesActivity extends AppCompatActivity {

Toolbar mtoolbar;
private DrawerLayout drawerLayout;

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

    //setting TOOLBAR on this activity(page)
    mtoolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mtoolbar);
    Objects.requireNonNull(getSupportActionBar()).setTitle(null);


    //setting DRAWER on this activity(page)
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, mtoolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawerLayout.addDrawerListener(toggle);
    toggle.syncState();


}

Here's my navigation_view.xml layout file`

    <android.support.design.widget.NavigationView android:layout_width="wrap_content"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_view"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header_drawer"
app:menu="@menu/activity_drawer_drawer"
xmlns:android="http://schemas.android.com/apk/res/android"
    />

and finally nav_view_header.xml layout file

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/background_nav_header"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:id="@+id/headerView">

    <ImageView
        android:id="@+id/imageViewNav"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_marginBottom="5dp"
        android:background="@drawable/border_ring_shape"
        android:contentDescription="@string/nav_header_desc"
        android:padding="22dp"
        app:srcCompat="@drawable/background_img_nav_0" />
    <TextView
        android:id="@+id/textViewNav"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/sign_name"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textSize="20sp" />

</LinearLayout>

Upvotes: 0

Views: 1202

Answers (1)

ʍѳђઽ૯ท
ʍѳђઽ૯ท

Reputation: 16976

Add prefix of NavigationView before findviewByid or;

View header = mNavigationView.getHeaderView(0);
myImageView = (ImageView) header.findViewById(R.id.imageviewname);
myImageView.setBackground(ContextCompat.getDrawable(this, R.drawable.your_drawable));

And check this answer: https://stackoverflow.com/a/43285072/4409113

Upvotes: 3

Related Questions