0
<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..

ChrisF
  • 134,786
  • 31
  • 255
  • 325
Laur RyiD
  • 31
  • 6

2 Answers2

1

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);

Community
  • 1
  • 1
ksarmalkar
  • 1,884
  • 1
  • 16
  • 21
  • You can define your own layout file for header as header.xml and then inflate it and above code will add it as header to you list view – ksarmalkar Mar 25 '15 at 20:18
0

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.

  • Can you give more information about what is not working? – creativedrewy Mar 26 '15 at 16:23
  • It compiles, but the app crashes. – Laur RyiD Mar 26 '15 at 16:33
  • Is the top level element of your activity layout a android.support.v4.widget.DrawerLayout? The activity that has the nav drawer has to have that as the root node. Then you have a normal layout section, and the LinearLayout I am referencing above becomes the nav drawer portion. – creativedrewy Mar 26 '15 at 16:46
  • In your above stack trace, it looks like you might be setting a layout xml that has a fragment as a top node as an activity layout. Your activity layout needs a top level layout like RelativeLayout. – creativedrewy Mar 26 '15 at 19:55