1

I have a LinearLayout which I add images when i click a button, I was trying to use LayoutParamsto change the width and height because doesn't fit as at should, and I always got a null reference, the problem is I can't make any reference because is not a static ImageView.

This is how I add images inside of LinearLayout :

public void AddNewImages(Context context,Bitmap bitmap){
    ImageView img = new ImageView(context);
    img.setScaleType(ImageView.ScaleType.FIT_XY);
    img.setImageBitmap(bitmap);
    linearImages.addView(img);
    bitmapArray.add(bitmap);
    indexTags++;
}

XML :

<LinearLayout
    android:orientation="vertical"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <HorizontalScrollView
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <LinearLayout
            android:orientation="horizontal"
            android:id="@+id/linearImages"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <Button
                android:layout_gravity="center"
                android:id="@+id/add_btn"
                android:text="Add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </HorizontalScrollView>

3 Answers3

1

First you have to find the Imageview which you added in linearlayout and set the size i.e

for(int i=0;i<linearImages.getChildCount();i++)
{
    ImageView image=(ImageView)linearImages.getChildAt(i);
    image.getLayoutParams().height=100;
    image.getLayoutParams().width=100;
}
Dani M
  • 1,173
  • 1
  • 15
  • 43
Basavannevva
  • 304
  • 1
  • 6
  • where should I put this for? right after `indexTags++;` ? I tried and my app crash without give any error –  Jan 31 '17 at 15:46
0
ImageView imgae = (ImageView) linearImages.getChildAt(0); // For first image

and then set LayoutParam.

D.J
  • 1,439
  • 1
  • 12
  • 23
0

You are creating ImageView dynamically so you have to first set layoutParameters for it and then you can set height and width of imageview. Refer this NullPointerException while setting LayoutParams question

Community
  • 1
  • 1