0

I want to set the height of the image view programatically in gridview. I used below code-

imageview.getLayoutParams().height = height;

This is working fine in android version 4.3 and higher but not working in android version 4.2 and below. I can't find the issue. Any help please.

ImageView in xml-

<ImageView
 android:id="@+id/item_image"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:scaleType="centerCrop"
 android:background="@drawable/top_radius" />
Jagveer Singh
  • 584
  • 7
  • 28
  • possible duplicate of [Set ImageView width and height programmatically on a widget](http://stackoverflow.com/questions/15171654/set-imageview-width-and-height-programmatically-on-a-widget) – Confuse Oct 06 '14 at 09:57
  • @Neutrino that solution is not working in 4.2 or below thats why i ask this question. – Jagveer Singh Oct 06 '14 at 10:03

3 Answers3

2
LayoutParams LP = imageview.getLayoutParams();
LP.height = height;
imageview.setLayoutParams(LP)
Carnal
  • 21,744
  • 6
  • 60
  • 75
1

This is a simple way to set dimensions of imageview

ImageView iv = (ImageView) findViewById(R.id.imageview);
int width = 60;
int height = 60;
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);
iv.setLayoutParams(parms);
Akshit Rewari
  • 941
  • 2
  • 13
  • 26
  • @JagveerSinghRajput I can't find any reason why this isn't working below android 4.2 – Akshit Rewari Oct 06 '14 at 09:48
  • @JagveerSinghRajput It is included in android API level 1 which means it should work on all versions of android. http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html Share some more details about your imageview code. There could be an issue in some other part of the code – Akshit Rewari Oct 06 '14 at 09:50
  • My ImageView is contains in StaggeredGridView. Is this the problem? – Jagveer Singh Oct 06 '14 at 11:27
  • @JagveerSinghRajput: I do not have much experience on working with a staggeredgridview, but I can tell you that the problem is in that part. I have verified that the above code will work without any error for a simple imageview. – Akshit Rewari Oct 06 '14 at 12:35
  • @JagveerSinghRajput I recommend you ask a new question now with complete details of your staggeredgridview code and include this keyword in the question's title and question's tags – Akshit Rewari Oct 06 '14 at 12:37
0

Wrap your ImageView in a LinearLayout and set the height of this LinearLayout like you're doing.

Groco
  • 1,301
  • 15
  • 19