Reputation: 2434
How can I get the Width of a ListView
in Adapter's
GetView
method ?
ParentLayout of the View
inflated is a RelativeLayout
with width as match_parent
Following is what I have tried
Display display = ((Activity)getContext()).getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
maxWidth = size.x;
int specWidth = View.MeasureSpec.makeMeasureSpec(maxWidth , View.MeasureSpec.AT_MOST);
convertView.findViewById(R.id.TagMasterLayout).measure(specWidth , specWidth);
int totalWidth = convertView.findViewById(R.id.TagMasterLayout).getMeasuredWidth();
In this example, I always get the Width of the screen of the device. As I have set the MeasureSpec
to accept a maximum value of Width of the device's screen.
Instead if I do,
int specWidth = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
And rest all portion as same, I get a huge value greater than 1000 (which is greater than device screen width, because variable maxWidth
had a value of only 720).
So How can I exactly measure the width of a ListView
with Width as fill_parent
in getView
method of Adapter class ?
Upvotes: 1
Views: 1687
Reputation: 567
Try to get width of parent view. It is a view to which your view will be attached to.
@Override
public View getView(int position, View convertView, final ViewGroup parent)
{
...
parent.getWidth();
...
return v;
}
parent.getWidth()
will return you the width of ListView
.
Upvotes: 1