I want to set the height of my Gridview programmatically in my application.
Is there any way to implement that?
I just want to change the gr
Use this, as you want to retain the current layoutParams
:
ViewGroup.LayoutParams layoutParams = yourGridview.getLayoutParams();
layoutParams.height = 50; //this is in pixels
yourGridview.setLayoutParams(layoutParams);
Edit: If you want to input the height as dp
instead of pixels, translate the dp
amount to pixels:
public static int convertDpToPixels(float dp, Context context){
Resources resources = context.getResources();
return (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dp,
resources.getDisplayMetrics()
);
}
I think this should work:
GridView gridView = (GridView)findViewById(...);
ViewGroup.LayoutParams lp=new ViewGroup.LayoutParams(width,height);
gridView.setLayoutParams(lp);