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)
We Can add header to ListView as below :
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 :
You need to initialise list view before you set header view.
something like this
then you can add the header view after inflating header view.
And after adding HeaderView to ListView, Try to set Adapter of ListView.
Hopefully it will help you ! Let me know if you get success !