N
N
ngapp2020-02-08 19:47:15
Android
ngapp, 2020-02-08 19:47:15

How to get user's screen size in BaseAdapter?

Hello everyone, I have an ImageAdapter and need to get the user's screen size in order to adjust the GridView

imageView.setLayoutParams(new GridView.LayoutParams(320,320));

How can this be done?
package com.ngapp.cngbelarus.ui.history;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;

import com.ngapp.cngbelarus.R;

public class ImageAdapter extends BaseAdapter {
    private Context context;
    public Integer[] images = {
            R.drawable.avatar_1, R.drawable.avatar_2,
            R.drawable.avatar_3, R.drawable.avatar_4,
            R.drawable.avatar_5, R.drawable.avatar_6,
            R.drawable.avatar_7, R.drawable.avatar_8,
            R.drawable.avatar_9, R.drawable.avatar_10,
            R.drawable.avatar_11, R.drawable.avatar_12,
            R.drawable.avatar_13, R.drawable.avatar_14,
            R.drawable.avatar_15, R.drawable.avatar_16,
            R.drawable.avatar_17, R.drawable.avatar_18,
            R.drawable.avatar_19, R.drawable.avatar_20,
            R.drawable.avatar_21, R.drawable.avatar_22,
            R.drawable.avatar_23, R.drawable.avatar_24
    };



    public ImageAdapter(Context c) {
        context = c;
    }

    @Override
    public int getCount() {
        return images.length;
    }

    @Override
    public Object getItem(int position) {
        return images[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imageView = new ImageView(context);
        imageView.setImageResource(images[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(320,320));
        return imageView;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
one pavel, 2020-02-08
@onepavel

Maybe this will help
Display d = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = d.getWidth();
int height = d.getHeight();
copied from here
https://stackoverflow.com/questions/2902640/androi...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question