Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8797537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:52:40+00:00 2026-06-13T23:52:40+00:00

Is there an XML tag which I can use in a layout file that

  • 0

Is there an XML tag which I can use in a layout file that is equivalent to ListView.addHeaderView()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:52:41+00:00Added an answer on June 13, 2026 at 11:52 pm

    I wrote a simple ListView like your requirement.

    1. Declare custom attribute in attrs.xml in value folder:

      <resources>
          <declare-styleable name="HeaderListViewFromXML"> 
              <attr name="headerView" format="reference"/>
          </declare-styleable>
      </resources>
      
    2. Create HeaderListViewFromXML class extended ListView

      public class HeaderListViewFromXML extends ListView {
          private int headerId;
      
          public HeaderListViewFromXML(Context context) {
              this(context, null);
          }
      
          public HeaderListViewFromXML(Context context, AttributeSet attrs) {
              this(context, attrs, 0);
          }
      
          public HeaderListViewFromXML(Context context, AttributeSet attrs, int defStyle) {
              super(context, attrs, defStyle);
              TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.HeaderListViewFromXML, defStyle, defStyle);
      
              try {
                  headerId = a.getResourceId(R.styleable.HeaderListViewFromXML_headerView, View.NO_ID);
                  if (headerId != View.NO_ID) {
                      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                      View header = inflater.inflate(headerId, null);
                      addHeaderView(header);
                  }
              } finally {
                  a.recycle();
              }
          }
      }
      
    3. Declare custom HeaderListViewFromXML in layout.xml

      <RelativeLayout
              xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
          <com.example.rewidget.HeaderListViewFromXML
                  android:id="@+id/listWithHeader"
                  android:layout_width="fill_parent"
                  android:layout_height="150dp"
                  android:layout_marginTop="60dp"
                  android:background="#00FF00"
                  // custom attribute. Point to layout in header1.xml
                  app:headerView="@layout/header1" />
      </RelativeLayout>
      
    4. In Activity, use like normal ListView

      public class MainActivity extends Activity {
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              ListView list = (ListView) findViewById(R.id.listWithHeader);
      
              String[] values = new String[] { "Android", "iPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" };
              ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
      
              list.setAdapter(adapter);
          }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

there is a xml document that i'm parsing to array.. how can i access
say there is an xml file, which not created by me, with a known
There's precious little documentation about the declare-styleable tag by which we can declare custom
I can use XDocument to build the following file which works fine : XDocument
There are so many html and xml libraries built into python , that it's
There are a lot of tutorials that teach on how to validate XML against
I write an XML file by hand, (i.e. not with LINQ to XML), which
I want to use simplified select one tag, which would generate select items list
I need to write a swing based editor that can open specified xml files,
I have been told that there is a way to use aapt's --version-code option

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.