Reputation: 11
I want to change the Imageview Height and width based on the drawable image size programmatically? i have two types of images one is horizontal and another one is vertical. By default i have write my xml code for vertical images but i want to change Image view height and width for horizontal images by java code. how to do this ?
my xml code is
<RelativeLayout
android:paddingTop="47dp"
android:paddingBottom="48dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:id="@+id/flipcardWrapper">
<ImageView
android:orientation="horizontal"
android:layout_width="365dp"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:id="@+id/imgtrans"
android:background="@android:color/transparent" />
and my java code to change height and width of image view is
imgview = (ImageView) findViewById(R.id.imgtrans);
Drawable d = getResources().getDrawable(R.drawable.image1);
int h = d.getIntrinsicHeight();
int w = d.getIntrinsicWidth();
if(d.getIntrinsicHeight()==797 && d.getIntrinsicWidth()==1218)
{
imgtrans.getLayoutParams().width=300;
imgtrans.requestLayout();
}
Upvotes: 1
Views: 858
Reputation: 397
Use ViewGroup.LayoutParams.MATCH_PARENT in java code.
imgview = (ImageView) findViewById(R.id.imgtrans);
Drawable d = getResources().getDrawable(R.drawable.image1);
int h = d.getIntrinsicHeight();
int w = d.getIntrinsicWidth();
if(d.getIntrinsicHeight()==797 && d.getIntrinsicWidth()==1218)
{
imgtrans.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
imgtrans.getLayoutParams().height = someValue;
imgtrans.requestLayout();
}
Upvotes: 0
Reputation: 13348
Set two images for horizontal and another one is vertical in drawable .then find screen orientation based on it set image
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
Drawable d = getResources().getDrawable(R.drawable.imagehorizontal);
//set your other properties
}
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
Drawable d = getResources().getDrawable(R.drawable.imagevertical);
//set your other properties
}
Upvotes: 0