Denis Windover Denis Windover - 26 days ago 16
Android Question

Android Navigation Drawer set up image from URL

I'm trying to build app with navigation drawer. What do I need is to be able of changing image dynamically on the head of navigation drawer from code by using glide. After putting this code I get the navigation header twice like on the picture. How to fix it?

enter image description here

nav_header_main.xml

<?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/side_nav_bar"
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">


<ImageView
android:id="@+id/imageView"
android:layout_width="70dp"
android:layout_height="70dp"
android:paddingTop="@dimen/nav_header_vertical_spacing"
app:srcCompat="@android:drawable/sym_def_app_icon" />


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android.studio@android.com" />

</LinearLayout>

MainActivity.java

NavigationView navigationView = null;
ImageView imgProfile = null;
String URL = "http://something.com/image.jpg";

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

navigationView = (NavigationView) findViewById(R.id.nav_view);
View hView = navigationView.inflateHeaderView(R.layout.nav_header_main);
imgProfile = (ImageView)hView.findViewById(R.id.imageView);
Glide.with(getApplicationContext()).load(URL).into(imgProfile);
navigationView.setNavigationItemSelectedListener(this);
}

Answer

remove this

app:headerLayout="@layout/nav_header_main" 

from your Navigationview in Xml