Thanh Duy Ngo
Thanh Duy Ngo

Reputation: 1611

Get width layout when layout add view dynamically

I have a problem when I getWidth of layout... It doesn't return a real width after adding new view?... how can I solve it... thanks... This is my sample code.

llDayHeaderTable = (LinearLayout) viewGroup
            .findViewById(R.id.llDayHeaderTable);
    LayoutInflater vi = (LayoutInflater) parent
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for (View v : vTmp) {
        llDayHeaderTable.removeView(v);
    }
    vTmp.removeAllElements();

    for (int i = 0; i < dto.arrProductTitleList.size(); i++) {
        String productName = dto.arrProductTitleList.get(i).productInfoName;
        vItemHeader = vi.inflate(R.layout.layout_kpi_sku_group_accum_item,
                null);
        TextView tv = (TextView) vItemHeader
                .findViewById(R.id.tvHeaderProduct);
        tv.setText("" + productName);
        llDayHeaderTable.addView(vItemHeader);
        vTmp.add(vItemHeader);
    }
int width = llDayHeaderTable.getWidth();

Upvotes: 0

Views: 2055

Answers (1)

Syamantak Basu
Syamantak Basu

Reputation: 935

Try using getMeasuredHeight() and getMeasuredWidth() instead of getWidth() and getHeight()

llDayHeaderTable.measure(0, 0);
final int w = llDayHeaderTable.getMeasuredWidth();
final int h = llDayHeaderTable.getMeasuredHeight();

Upvotes: 7

Related Questions