How To Make A Dynamic Imageview?
I need to make an app with some Images, and one that it's in the center needs to change his size depending on the size of the screen. Here is the code, the image that I need to cha
Solution 1:
Instead of adding ImageView in xml you should do that programmatically. In xml you need just RelativeLayout which will be used as image holder. So what you need to do is:
- Create image holder
`
<RelativeLayout
android:id="@+id/image_view_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@color/blk" >`
- Create and add ImageView in image holder
`
DisplayMetricsmetrics=newDisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
intwidth= metrics.widthPixels;
intheight= metrics.heightPixels;
RelativeLayoutimgHolder= (RelativeLayout) findViewById(R.id.image_view_holder);
ImageViewimg=newImageView(this);
RelativeLayout.LayoutParamsparams=newRelativeLayout.LayoutParams(width, height/2);
img.setLayoutParams(params);
imgHolder.addView(img);`
Now when you know screen size you can set image size depending on screen size.
Solution 2:
Try below code: -> First give your layout a id.
<RelativeLayoutandroid:id="@+id/relative"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:background="@color/blk" ><ImageViewandroid:id="@+id/imageView4"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center"android:background="@color/celeste"android:paddingBottom="50dp"android:paddingTop="50dp"android:src="@drawable/image4" /></RelativeLayout>
Now, in your code, change as per below.
ImageView imageView4;
RelativeLayout relative;
imageView4=(ImageView)findViewById(R.id.imageView4);
relative=(RelativeLayout)findViewById(R.id.relative);
LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
imageView4.setLayoutParams(params);
relative.addView(imageView4, params);
Post a Comment for "How To Make A Dynamic Imageview?"