Android Screen Size Get getScreenWidth(Context context)

Here you can find the source of getScreenWidth(Context context)

Description

get Screen Width

Declaration

public static int getScreenWidth(Context context) 

Method Source Code

//package com.java2s;
import android.content.Context;
import android.content.res.Configuration;

import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

public class Main {
    public static final String TAG = "CLDeviceUtil";

    public static int getScreenWidth(Context context) {
        int screenSizeOne = -1;
        int screenSizeTwo = -1;

        if (screenSizeOne <= 0 || screenSizeTwo <= 0) {
            DisplayMetrics dm = new DisplayMetrics();
            WindowManager wm = (WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE);
            if (wm != null) {
                wm.getDefaultDisplay().getMetrics(dm);
            }/*from   w  ww  .ja  v  a  2 s.c  o  m*/
            screenSizeOne = dm.widthPixels;
            screenSizeTwo = dm.heightPixels;
        }

        Configuration conf = context.getResources().getConfiguration();
        switch (conf.orientation) {
        case Configuration.ORIENTATION_LANDSCAPE:
            return screenSizeOne > screenSizeTwo ? screenSizeOne
                    : screenSizeTwo;
        case Configuration.ORIENTATION_PORTRAIT:
            return screenSizeOne < screenSizeTwo ? screenSizeOne
                    : screenSizeTwo;
        default:
            Log.e(TAG, "can't get screen width!");
        }
        return screenSizeOne;
    }
}

Related

  1. getScreenWidth(Display display)
  2. getScreenSizePixels(Context ctx)
  3. getRealScreenDimensions(Context context)
  4. getScreenHeight(Context context)
  5. getScreenSize(@Nonnull Context context)
  6. getScreenDensity(Context context)
  7. getScreenSize(Activity ctx)
  8. getScreenSize(Context context)
  9. getScreenSize(Context ctx)