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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:07:46+00:00 2026-05-27T18:07:46+00:00

I have a custom ListView class (subclassed from ListView) and I need it to

  • 0

I have a custom ListView class (subclassed from ListView) and I need it to add a small padding to the bottom of the final view element so that it is not overlapped by a small bar that I have across the bottom of the screen. I only want to do this when the child views grow past the visible region of the listview. I am trying to use this code to achieve this:

@Override
protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
     super.onLayout(changed, left, top, right, bottom);

     // If there are hidden listview items we need to add a small padding to the last one so that it
     // partially hidden by the bottom sliding drawer handle
     if (this.getLastVisiblePosition() - this.getFirstVisiblePosition() + 1 > this.getCount()) {
         LinearLayout v2 = (LinearLayout) this.getChildAt(this.getCount() - 1);
         v2.setPadding(v2.getPaddingLeft(), v2.getPaddingTop(), v2.getPaddingRight(), 
                 v2.getPaddingBottom() + 5);
     }
 }

However, the values returned by getLastVisiblePostion(), getFirstVisiblePostion() and getCount() do not reflect what the adapter holds yet. I am assuming this is because the Adapter has not yet notified the ListView of the data, but I cannot figure out where the ListView would actually know about the data and thus have the correct values. This code is being run when the Activity is being loaded.

At what point in the rendering process will I have access to this data? I should also say that I use an AsyncTask to load the data from a database and then create the Adapter in there and add it to the list view. Is there an event I can use within the ListView that will fire when the adapter adds data/causes the listview to render the new items?

  • 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-05-27T18:07:47+00:00Added an answer on May 27, 2026 at 6:07 pm

    I’m not sure if it’s the best solution, but I know that in one of the branches of a pull-to-refresh implementation for Android, the height of the list is compared against the total height of all the list items. From what you’re saying, it sounds like that’s the same information you’re looking for in order to determine whether to apply extra padding or not.

    The relevant parts of the implementation are in the following three methods:

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mHeight == -1) {  // do it only once
            mHeight = getHeight(); // getHeight only returns useful data after first onDraw()
            adaptFooterHeight();
        }
    }
    
    /**
     * Adapts the height of the footer view.
     */
    private void adaptFooterHeight() {
        int itemHeight = getTotalItemHeight();
        int footerAndHeaderSize = mFooterView.getHeight()
            + (mRefreshViewHeight - mRefreshOriginalTopPadding);
        int actualItemsSize = itemHeight - footerAndHeaderSize;
        if (mHeight < actualItemsSize) {
            mFooterView.setHeight(0);
        } else {
            int h = mHeight - actualItemsSize;
            mFooterView.setHeight(h);
            setSelection(1);
        }
    }
    
    /**
     * Calculates the combined height of all items in the adapter.
     * 
     * Modified from http://iserveandroid.blogspot.com/2011/06/how-to-calculate-lsitviews-total.html
     * 
     * @return 
     */
    private int getTotalItemHeight() {
        ListAdapter adapter = getAdapter();
        int listviewElementsheight = 0;
        for(int i =0; i < adapter.getCount(); i++) {
            View mView  = adapter.getView(i, null, this);
            mView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            listviewElementsheight+= mView.getMeasuredHeight();
        }
        return listviewElementsheight;
    }
    

    The full source code can be found here on GitHub.

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

Sidebar

Related Questions

I have a Custom ListView created from XML Layout. I want to bind that
I have created a custom listview that have one webview per element. My problem
I have a custom adapter for ListView and I need to return LinearLayout from
I have a ListView with a custom Adapter that extends ArrayAdapter. It's a ArrayAdapter
I have a listview with 200 items. I use a custom view for each
I have a custom CursorAdapter that is taking items from a database and displaying
I have a listview with a custom arrayadapter that handles about 15 strings. The
I have a listview that's populated by rows that get their data from a
I have a Custom ListView that contains a vertical LinearLayout of horizontal LinearLayouts for
I have a listview with a custom array adapter: public class SmsMessageAdapter extends ArrayAdapter<ContactMessage>

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.