kick
kick

Reputation: 43

Android studio get screen height in class

Is it possible to get my screen height in this function? i want to make my listview 80% of my screen size but am unable to figure this out.

private void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        int len = listAdapter.getCount();
        for (int i = 0; i < len; i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }



        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = 2000; //+ (listView.getDividerHeight() * (listAdapter.getCount() - 1);
        listView.setLayoutParams(params);
    }```

Upvotes: 1

Views: 46

Answers (2)

Albert Lipaev
Albert Lipaev

Reputation: 76

My answer might not be right, but when working with screen resolution, I've used the following code:

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

Upvotes: 0

kimchibooty
kimchibooty

Reputation: 339

You can find the dimensions of your device screen with the following:

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;

Upvotes: 3

Related Questions