I'm trying to properly display some user information in a NavigationDrawer
header, but for some reason some of the properties are getting displayed twice in the header ("some email or other information" and the image on top). Could you tell me what I'm doing wrong and need to fix. Thanks!
Here is a screenshot of the NavigationDrawer
header:
Here is the XML for the header:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="16dp"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/userImage"
android:layout_width="96dp"
android:layout_height="96dp"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="Some user"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some email or other information" />
And here is the part of the java code that initializes the NaviationDrawer
, loads the image, etc...:
main method:
drawer = (DrawerLayout) findViewById(R.id.drawer_layout); //gets drawer button
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( //intializes toggle for navigationdrawer
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle); //sets toggle for navigationdrawer
navigationView = (NavigationView) findViewById(R.id.nav_view); //initializes list of items in navigationdrawer
navigationView.setNavigationItemSelectedListener(this); //sets listener for items in navigationdrawer
navigationView.getMenu().getItem(0).setChecked(true); //sets "Map" as checked
loadNavigationDrawerInfo(navigationView.inflateHeaderView(R.layout.nav_header_main));
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1 * 1000); // 1 second, in milliseconds
drawer.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
Log.d("TAG", "hasFocus" + hasFocus);
}
});
public void loadNavigationDrawerInfo(View headerlayout) {
Map config = new HashMap();
config.put("cloud_name", "some cloud name");
config.put("api_key", "some api key");
config.put("api_secret", "some api secret api secret");
Cloudinary cloudinary = new Cloudinary(config);
new DownloadImageTask((CircleImageView) headerlayout.findViewById(R.id.userImage)).execute("some valid URL with a jpg");
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
CircleImageView bmImage;
public DownloadImageTask(CircleImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
Log.d("TAG", "Done loading image");
}
}
This becomes when you both using :
navigationView.inflateHeaderView(R.layout.nav_header_main);
and app:headerLayout="@layout/drawer_header"
in your xml. Try to disable one of them.
If you want to programmatically change email, photo use Java
code, if you have stable header view just use Xml
code.