Search code examples
androidgridviewandroid-custom-viewbaseadapter

Adjust the height and width of custom grid item according to different screen sizes


I have a GridView and as items for this GridView i use custom layout and an adapter to inflate this custom view at the grid items. but am having a problemwith different screen sizes because the gridview items is not expanding but rather still showing small because the sizes of the custom layout is statically declared. What can i make so that grid view items will take certian % of the screen heigth and width.

Notice:

This Code wont Work

View someView = (View).findViewById(someId):
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)
someView .getLayoutParams();
params.height = 130;
someView .setLayoutParams(params);

Solution

  • I Found the solution if anybody is interested.

    private int ScreenHeight;
    private int ScreenWidth;
    
    DisplayMetrics displaymetrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    ScreenHeight = displaymetrics.heightPixels;
    ScreenWidth = displaymetrics.widthPixels;
    
    holder.BookItemWraper.setLayoutParams(new android.widget.AbsListView.LayoutParams((int) Math.round(ScreenWidth / 2.1), (int) Math.round(ScreenHeight / 2.5)));