How add listView header as a custom layout

10,911

We Can add header to ListView as below :

LayoutInflater myinflater = getLayoutInflater();
ViewGroup myHeader = (ViewGroup)myinflater.inflate(R.layout.headerlayout, myListView, false);
myListView.addHeaderView(myHeader, null, false);

But, As per your error you might have taken Relative Layout in your xml and in your java file, You are using LinearLayout.

You also have to initialize your ListView as :

lv=(ListView)findViewById(R.id.yourlistview);`
Share:
10,911
Sohail Zahid
Author by

Sohail Zahid

Android Software Engineer having experience in developing user-friendly, aesthetically pleasing, and fully functional applications. Dedicated to continuously developing, implementing, and adopting new technologies to maximize development efficiency and produce innovative applications. During my recent positions, I've had the opportunity to strengthen my leadership skills. I had served in leadership positions for the Android Development department as a Team Lead. I have experience with Social Media, eCommerce, Team Management, and executing projects from inception to launch and am able to provide a structured framework to analyze complex situations into simple strategic imperatives. Areas of Expertise • Leadership and innovationn • Strategic thinking • Team management • Documentations and Management tools

Updated on June 04, 2022

Comments

  • Sohail Zahid
    Sohail Zahid almost 2 years

    I want to add header in listView i try like this what i am missing here ids are fine. I am using this in fragment.

    ListView lv;
    
    LinearLayout header = (LinearLayout) rootView.findViewById(R.id.header_layout);
    
    lv.addHeaderView(header);
    // APP is crashing here?
    

    Logcat

    java.lang.ClassCastException: android.widget.AbsListView$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
                at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1732)
                at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1677)
                at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:381)
                at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:389)
                at android.view.View.measure(View.java:17547)
                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
                at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
                at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
                at android.view.View.measure(View.java:17547)
                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                at android.view.View.measure(View.java:17547)
    
  • user5716019
    user5716019 over 8 years
    ohh bro, wc, complete your task and go ahead, Good Luck.