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 9023825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:51:21+00:00 2026-06-16T05:51:21+00:00

My ListView items need to be centered when they are selected, but the top

  • 0

My ListView items need to be centered when they are selected, but the top and bottom items stop at the top or bottom of the list. How do I center the top and bottom items?

Here’s my test layout:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/parent"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:clipChildren="false"
       android:clipToPadding="false"
       android:gravity="top" >

       <ListView
           android:id="@+id/listview"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_centerVertical="true"
          android:clipChildren="false"
          android:clipToPadding="false" >
       </ListView>

    </RelativeLayout>
  • 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-16T05:51:22+00:00Added an answer on June 16, 2026 at 5:51 am

    I couldn’t find a solution to the clipping issue so I went with adding a header and footer to the ListView. I changed my ListView height back to match_parent and then, using the post method on my ListView’s parent view, I was able to calculate the height of my header and footer like so:

    // this call should be included in the onCreate method
    // the post method is called after parentView is ready to be added
    // so the parentView's height and width can be used
    parentView.post(addSpace(parentView, myListView)); 
    
    private Runnable addSpace(final RelativeLayout parent, final ListView list) {
        return new Runnable(){
            public void run() {
                // create list adapter here or in the onCreate method
                // R.layout.list_item refers to my custom xml layout file
                adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_item, values);
                // calculate size and add header and footer BEFORE applying adapter
                // listItemHeight was calculated previously
                spacerSize = (parent.getHeight()/2) - (listItemHeight/2);
                list.addHeaderView(getSpacer(), null, false);
                list.addFooterView(getSpacer(), null, false);
                list.setAdapter(adapter);
            }
        }
    }
    
    // I used a function to create my spacers   
    private LinearLayout getSpacer(){
        // I used padding instead of LayoutParams thinking it would be easier to
        // change in the future. It wasn't, but still made resizing my spacer easy
        LinearLayout    spacer = new LinearLayout(MainActivity.this);
                        spacer.setOrientation(LinearLayout.HORIZONTAL);
                        spacer.setPadding(parentView.getWidth(), spacerSize, 0, 0);
        return spacer;
    }
    

    Using this method I was able to add the proper spacing to the top and bottom of my ListView so when I scrolled all the way to the top the top item appeared in the center of my view.

    PROBLEM: My app allows the user to hide the bottom interface element which should make my parent much taller. I am working out a way to make my header and footer taller dynamically but I may need to simply remove the ListView and recreate it with a larger header and footer.

    I hope this helps someone from hours of frustration 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of items in ListView and I need something like AfterSelectionChanged
i have a Listview with items that has almost 100 chars, but i need
I've got a ListView with a custom BaseAdapter. The list items contain CheckBoxes that
I have a ListView of items that need to be checkable/uncheckable. I have set
I have another problem with ListView :( Now I need to move items in
How do I set the border between listview items. I need to have specific
I need to access a listview's items in a DoWork event handler. For this
I need to show ListView header always, even when ListView has no items. It
I was following this tutorial, as it's exactly what I need: http://www.marvinlabs.com/2010/10/custom-listview-ability-check-items/ Unfortunately, their
I need to refresh the ListView items every second, and it works fine using

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.