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

  • Home
  • SEARCH
  • 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 3484466
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:47:29+00:00 2026-05-18T10:47:29+00:00

I inflated a ListView as the contentView in a PopupWindow. If I don’t set

  • 0

I inflated a ListView as the contentView in a PopupWindow.
If I don’t set the width & height, I can’t see the PopupWindow.
If I set them like this:

setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

The layout is being set like “fill_parent”. Why?

The layout attributes of the ListView and ListView item are all set to “wrap_content”.
Any suggestion? Thanks.

  • 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-18T10:47:30+00:00Added an answer on May 18, 2026 at 10:47 am

    Here’s the SimpleListView class implementation. I know it’s inefficient and I’m pretty sure it contains errors, but it shows how to measure a width of a list view.

    I also recommend to read this article.

        public class SimpleListView extends AdapterView<ListAdapter> {
        private ListAdapter adapter;
        private Drawable divider;
        private int dividerHeight;
        private boolean clipDivider;
    
        public SimpleListView( final Context context )
        {
            this( context, null );
        }
    
        public SimpleListView( final Context context, final AttributeSet attrs )
        {
            this( context, attrs, android.R.attr.listViewStyle );
        }
    
        public SimpleListView( final Context context, final AttributeSet attrs, final int defStyle )
        {
            super( context, attrs, defStyle );
    
            final TypedArray array =
                context.obtainStyledAttributes( attrs, R.styleable.SimpleListView, defStyle, 0 );
    
            final Drawable dividerAttribute =
                array.getDrawable( R.styleable.SimpleListView_android_divider );
            if( dividerAttribute != null ) {
                setDivider( dividerAttribute );
            }
    
            final int dividerHeightAttribute =
                array.getDimensionPixelSize( R.styleable.SimpleListView_android_dividerHeight, 0 );
            if( dividerHeightAttribute != 0 ) {
                setDividerHeight( dividerHeightAttribute );
            }
    
            array.recycle();
        }
    
        public Drawable getDivider()
        {
            return this.divider;
        }
    
        public void setDivider( final Drawable newDivider )
        {
            if( newDivider != null ) {
                this.dividerHeight = newDivider.getIntrinsicHeight();
                this.clipDivider = newDivider instanceof ColorDrawable;
            } else {
                this.dividerHeight = 0;
                this.clipDivider = false;
            }
            this.divider = newDivider;
            requestLayout();
        }
    
        public int getDividerHeight()
        {
            return this.dividerHeight;
        }
    
        public void setDividerHeight( final int newHeight )
        {
            this.dividerHeight = newHeight;
            requestLayout();
        }
    
        @Override
        public ListAdapter getAdapter()
        {
            return this.adapter;
        }
    
        @Override
        public void setAdapter( final ListAdapter adapter )
        {
            this.adapter = adapter;
            removeAllViewsInLayout();
            requestLayout();
        }
    
        @Override
        public View getSelectedView()
        {
            throw new UnsupportedOperationException(
                "SimpleListView.getSelectedView() is not supported" );
        }
    
        @Override
        public void setSelection( final int position )
        {
            throw new UnsupportedOperationException(
                "SimpleListView.setSelection(int) is not supported" );
        }
    
        @Override
        protected void onMeasure( final int widthMeasureSpec, final int heightMeasureSpec )
        {
            super.onMeasure( widthMeasureSpec, heightMeasureSpec );
    
            final int widthMode = MeasureSpec.getMode( widthMeasureSpec );
            int widthSize = MeasureSpec.getSize( widthMeasureSpec );
            final int heightMode = MeasureSpec.getMode( heightMeasureSpec );
            int heightSize = MeasureSpec.getSize( heightMeasureSpec );
    
            int innerWidth = 0;
            int innerHeight = 0;
    
            final int itemCount = this.adapter == null ? 0 : this.adapter.getCount();
            if( itemCount > 0
                && ( widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY ) ) {
                for( int i = 0; i < itemCount; ++i ) {
                    final View convertView = getChildAt( i );
                    final View child = this.adapter.getView( i, convertView, this );
                    if( convertView == null ) {
                        LayoutParams params = child.getLayoutParams();
                        if( params == null ) {
                            params = new LayoutParams( LayoutParams.WRAP_CONTENT,
                                LayoutParams.WRAP_CONTENT );
                            child.setLayoutParams( params );
                        }
                        addViewInLayout( child, i, params );
                    }
    
                    if( child.getLayoutParams() instanceof MarginLayoutParams ) {
                        measureChildWithMargins( child, widthMeasureSpec, 0, heightMeasureSpec, 0 );
                    } else {
                        measureChild( child, widthMeasureSpec, heightMeasureSpec );
                    }
                    innerWidth = Math.max( innerWidth, child.getMeasuredWidth() );
                    innerHeight += child.getMeasuredHeight();
                }
    
                innerHeight += ( itemCount - 1 ) * this.dividerHeight;
            }
    
            if( widthMode != MeasureSpec.EXACTLY ) {
                final int newWidthSize = getPaddingLeft() + getPaddingRight() + innerWidth;
                widthSize =
                    widthMode == MeasureSpec.AT_MOST ? Math.min( widthSize, newWidthSize )
                        : newWidthSize;
            }
    
            if( heightMode != MeasureSpec.EXACTLY ) {
                final int newHeightSize = getPaddingTop() + getPaddingBottom() + innerHeight;
                heightSize =
                    heightMode == MeasureSpec.AT_MOST ? Math.min( heightSize, newHeightSize )
                        : newHeightSize;
            }
    
            setMeasuredDimension( widthSize, heightSize );
        }
    
        @Override
        protected void onLayout( final boolean changed, final int left, final int top, final int right,
            final int bottom )
        {
            super.onLayout( changed, left, top, right, bottom );
    
            if( this.adapter == null ) {
                return;
            }
    
            positionItems();
            invalidate();
        }
    
        private void positionItems()
        {
            int top = getPaddingTop();
            final int left = getPaddingLeft();
    
            for( int i = 0, count = getChildCount(); i < count; ++i ) {
                final View child = getChildAt( i );
    
                final int width = child.getMeasuredWidth();
                final int height = child.getMeasuredHeight();
    
                child.layout( left, top, left + width, top + height );
                top += height + this.dividerHeight;
            }
        }
    
        @Override
        protected void dispatchDraw( final Canvas canvas )
        {
            final boolean drawDividers = this.dividerHeight > 0 && this.divider != null;
    
            if( drawDividers ) {
                final int left = getPaddingLeft();
                final int right = getWidth() - getPaddingRight();
                final Rect dividerBounds = new Rect( left, 0, right, 0 );
    
                for( int i = 0, count = getChildCount(); i < count - 1; ++i ) {
                    final View child = getChildAt( i );
    
                    dividerBounds.top = dividerBounds.bottom + child.getMeasuredHeight();
                    dividerBounds.bottom = dividerBounds.top + this.dividerHeight;
                    drawDivider( canvas, dividerBounds );
                }
            }
    
            super.dispatchDraw( canvas );
        }
    
        private void drawDivider( final Canvas canvas, final Rect bounds )
        {
            if( !this.clipDivider ) {
                this.divider.setBounds( bounds );
            } else {
                canvas.save();
                canvas.clipRect( bounds );
            }
    
            this.divider.draw( canvas );
    
            if( this.clipDivider ) {
                canvas.restore();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a populating a listview on a fragment from a database: public View
I have a customized BaseAdapter for my listView with this code: public View getView(int
i've got a listView with some data that i inflated to get some nice
I populate a listview from a JSON object. This works fine setListAdapter(new ArrayAdapter<String>(this, R.layout.list_items,
I have a ListView that is being populated with a custom adapter. I'd like
hi I've setup (well inflated) a bunch of buttons, which are built from a
I have a dynamic ListView which uses an ArrayAdapter . When a name is
I have a problem with adding a listview as a header in my listview
I try to create listView dynamically from code with: @Override public View getView(int position,
I have an activity with a ListView which is populated through a custom ArrayAdapter.

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.