Tuesday, November 17, 2015

How to add an image header in navigation drawer layout in android

Add this code in your Mainactivity on create after add drawer item in drawerlist

View header = getLayoutInflater().inflate(R.layout.abc, null);
ImageView pro = (ImageView) header.findViewById(R.id.profile_image);
pro.setOnClickListener(new View.OnClickListener() {
    @Override 
   public void onClick(View arg0) {
        // TODO Auto-generated method stub        
Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();}});
mDrawerList.addHeaderView(header);

Create abc.xml for header layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"  
 android:layout_width="match_parent"   
 android:layout_height="190dp"   
 android:background="@drawable/bgh"   
 android:orientation="vertical">

  <de.hdodenhof.circleimageview.CircleImageView
  xmlns:app="http://schemas.android.com/apk/res-auto"   
  android:id="@+id/profile_image"      
  android:layout_width="76dp"       
  android:layout_height="76dp"      
  android:layout_alignParentLeft="true"    
  android:layout_alignParentStart="true"
  android:layout_marginLeft="24dp"     
  android:layout_marginStart="24dp"     
  android:src="@drawable/p"       
  app:border_color="#FF000000" />

    <TextView    
   android:id="@+id/username"   
   android:layout_width="wrap_content"       
   android:layout_height="wrap_content"      
   android:layout_above="@+id/email"      
   android:layout_alignLeft="@+id/profile_image"   
   android:layout_alignStart="@+id/profile_image"    
   android:gravity="left"     
   android:paddingBottom="4dp"     
   android:text="Akash Bangad"      
   android:textColor="#FFF"     
   android:textSize="14sp"    
   android:textStyle="bold" />

   <TextView     
   android:id="@+id/email"    
   android:layout_width="wrap_content"    
   android:layout_height="wrap_content"     
   android:layout_alignLeft="@+id/username"     
   android:layout_alignParentBottom="true"      
   android:layout_alignStart="@+id/username"   
   android:layout_marginBottom="8dp"     
   android:gravity="left"    
   android:text="Akash.bangad93@gmail.com"    
   android:textColor="#fff"     
   android:textSize="14sp" />

</RelativeLayout>

Add this code in build.gradle
dependencies {

  compile fileTree(dir: 'libs', include: ['*.jar'])
  compile 'com.android.support:appcompat-v7:22.2.0'  
  compile 'com.android.support:design:22.2.0'  
  compile 'de.hdodenhof:circleimageview:1.3.0'
}

Finally run and see your output

enter image description here 

No comments:

Post a Comment