Laur RyiD
Laur RyiD

Reputation: 31

How can I add a Header in Drawer

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:choiceMode="singleChoice"
android:divider="@android:color/transparent" 
android:dividerHeight="0dp"
android:background="#cccc" 
tools:context=".NavigationDrawerFragment" />

I have that as fragment_navigation_drawer.xml , but i want to have a header, just a simple ImageView or that complex thing with Name, Email, CircleView..

How can i transform this ?


This file: fagment_main.xml

 <RelativeLayout       
          xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools" 
   android:layout_width="match_parent"
   android:layout_height="match_parent" 
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
   tools:context=".MainActivity$PlaceholderFragment">

<TextView android:id="@+id/section_label" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>





  This file: fragment_navigation_drawer.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NavigationDrawerFragment">

<ImageView
    android:id="@+id/my_header_image_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/simple_image_ok" />

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#cccc" />
</LinearLayout>


        Thisfile: activity_main.xml

   <android.support.v4.widget.DrawerLayout         
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"       
   android:id="@+id/drawer_layout"
   android:layout_width="match_parent" android:layout_height="match_parent"
   tools:context=".MainActivity">

   <FrameLayout android:id="@+id/container"  
     android:layout_width="match_parent"
     android:layout_height="match_parent" />



    <fragment android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width"        
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="t9200.laurentiu.com.dti200t9.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

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

      Those are my xml files.. i hope you can help me :D

03-26 18:34:02.674 5431-5431/t9200.laurentiu.com.dti200t9 E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: t9200.laurentiu.com.dti200t9, PID: 5431 java.lang.RuntimeException: Unable to start activity ComponentInfo{t9200.laurentiu.com.dti200t9/t9200.laurentiu.com.dti200t9.MainActivity}: android.view.InflateException: Binary XML file line #19: Error inflating class fragment at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2395) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2453) at android.app.ActivityThread.access$900(ActivityThread.java:173)

and it goes on..

Upvotes: 0

Views: 124

Answers (2)

creativedrewy
creativedrewy

Reputation: 101

You don't even have to use the header functionality of the ListView. You can have any complex layout in a navigation drawer, so I would even recommend just having a LinearLayout as your top element. Then you can put in your ImageView and have the ListView underneath it. Like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NavigationDrawerFragment">

    <ImageView
        android:id="@+id/my_header_image_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="{Your Drawable Here}" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#cccc" />
</LinearLayout>

You can also easily replace the ImageView with any complex layout & sub elements you want.

Upvotes: 0

ksarmalkar
ksarmalkar

Reputation: 1894

Android list view allow you to add a global header to ListView.

Refer Using ListView : How to add a header view?

LayoutInflater inflater = getLayoutInflater(); ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, myListView, false); myListView.addHeaderView(header, null, false);

Upvotes: 1

Related Questions