Question

I'm having problem when adding the Imageview in my relativelayout if a layoutparams is added to the view, the imageview will not add but if the imageview has no layoutparams, it will show but no image. Imageview is created through code.

TableLayout imageCon = (TableLayout)root.findViewById(R.id.home_screen_table_imagecon);
TableRow imageTableRow = new TableRow(root.getContext());
    TableRow.LayoutParams rowSpanLayout = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

imageTableRow.addView(getLove());
imageTableRow.addView(getHappy());
imageCon.addView(imageTableRow,rowSpanLayout);
imageCon.setShrinkAllColumns(true);

private ImageView getLove(){
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    lp.setMargins(10, 10, 10,10);
    imgLove.setLayoutParams(lp);
    imgLove.setImageResource(R.drawable.img_emo_love);

    return imgLove;
}
private ImageView getHappy(){
    imgHappy.setImageResource(R.drawable.img_emo_happy);
    return imgHappy;
}

I'm looking forward for your help guys.

Thanks Chkm8

Was it helpful?

Solution

You need to use the TableRow.LayoutParams , since your adding the ImageView into a TableRow

Change the LayoutParams as

TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,  TableRow.LayoutParams.MATCH_PARENT);
lp.setMargins(10, 10, 10,10);
imgLove.setLayoutParams(lp);

If ImageView is added to a RelativeLayout then use RelativeLayout.LayoutParams

If ImageView is added to a LinearLayout then use LinearLayout.LayoutParams

OTHER TIPS

put below line in xml ImageView tag

android:adjustViewBounds="true"

enjoy...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top