Example usage for android.view WindowManager getDefaultDisplay

List of usage examples for android.view WindowManager getDefaultDisplay

Introduction

In this page you can find the example usage for android.view WindowManager getDefaultDisplay.

Prototype

public Display getDefaultDisplay();

Source Link

Document

Returns the Display upon which this WindowManager instance will create new windows.

Usage

From source file:com.ferdi2005.secondgram.AndroidUtilities.java

public static void lockOrientation(Activity activity) {
    if (activity == null || prevOrientation != -10) {
        return;/* www  .j ava  2  s. c o m*/
    }
    try {
        prevOrientation = activity.getRequestedOrientation();
        WindowManager manager = (WindowManager) activity.getSystemService(Activity.WINDOW_SERVICE);
        if (manager != null && manager.getDefaultDisplay() != null) {
            int rotation = manager.getDefaultDisplay().getRotation();
            int orientation = activity.getResources().getConfiguration().orientation;

            if (rotation == Surface.ROTATION_270) {
                if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                } else {
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                }
            } else if (rotation == Surface.ROTATION_90) {
                if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
                } else {
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }
            } else if (rotation == Surface.ROTATION_0) {
                if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                } else {
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                }
            } else {
                if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                } else {
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
                }
            }
        }
    } catch (Exception e) {
        FileLog.e(e);
    }
}

From source file:com.ferdi2005.secondgram.AndroidUtilities.java

public static void checkDisplaySize(Context context, Configuration newConfiguration) {
    try {/*  w  w w  .  j  a v  a 2 s  .c om*/
        density = context.getResources().getDisplayMetrics().density;
        Configuration configuration = newConfiguration;
        if (configuration == null) {
            configuration = context.getResources().getConfiguration();
        }
        usingHardwareInput = configuration.keyboard != Configuration.KEYBOARD_NOKEYS
                && configuration.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO;
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (manager != null) {
            Display display = manager.getDefaultDisplay();
            if (display != null) {
                display.getMetrics(displayMetrics);
                display.getSize(displaySize);
            }
        }
        if (configuration.screenWidthDp != Configuration.SCREEN_WIDTH_DP_UNDEFINED) {
            int newSize = (int) Math.ceil(configuration.screenWidthDp * density);
            if (Math.abs(displaySize.x - newSize) > 3) {
                displaySize.x = newSize;
            }
        }
        if (configuration.screenHeightDp != Configuration.SCREEN_HEIGHT_DP_UNDEFINED) {
            int newSize = (int) Math.ceil(configuration.screenHeightDp * density);
            if (Math.abs(displaySize.y - newSize) > 3) {
                displaySize.y = newSize;
            }
        }
        FileLog.e("display size = " + displaySize.x + " " + displaySize.y + " " + displayMetrics.xdpi + "x"
                + displayMetrics.ydpi);
    } catch (Exception e) {
        FileLog.e(e);
    }
}

From source file:com.shopify.buy.ui.ProductDetailsFragmentView.java

/**
 * Get the display size.// w  w  w  .  j  a  v  a 2  s  . c  om
 * @return display size in {@link Point}.
 */
private Point getDisplaySize() {
    Point pt = new Point();

    if (DeviceUtils.isTablet(getResources())) {
        // For tablets we have explicitly set a dialog window size, so lets return that here
        pt.x = fragment.getActivity().getWindow().getAttributes().width;
        pt.y = fragment.getActivity().getWindow().getAttributes().height;
    } else {
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        display.getSize(pt);
    }

    return pt;
}

From source file:com.luanthanhthai.android.liteworkouttimer.TimerFragment.java

/**
 * Get screen center height/*from w w  w. j ava  2s .c o m*/
 */
public int getScreenHeight() {
    DisplayMetrics metrics = new DisplayMetrics();
    WindowManager windowManager = getActivity().getWindowManager();
    windowManager.getDefaultDisplay().getMetrics(metrics);

    return metrics.heightPixels;
}

From source file:com.shopify.buy.ui.ProductDetailsFragmentView.java

/**
 * Sets up the image area.//from   ww w  . j a v a 2  s . c  o  m
 * If the product has images, the height of the image area will be set equal to the width of the screen.
 * If the product does not have an image, the height of the image area will be equal to the action bar height.
 */
private void initializeImageArea() {
    imageAreaWrapper = (ViewGroup) findViewById(R.id.product_details_image_wrapper);

    WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);

    int screenWidth = metrics.widthPixels;
    int screenHeight = metrics.heightPixels;

    if (DeviceUtils.isTablet(getResources())) {
        screenHeight = fragment.getActivity().getWindow().getAttributes().height;
        screenWidth = fragment.getActivity().getWindow().getAttributes().width;
    }

    // Make sure the description area below the image is at least 40% of the screen height
    int minDetailsHeightInPx = Math.round(screenHeight * 0.4f);

    boolean hasImage = product != null && product.hasImage();
    int maxHeightInPx = hasImage ? screenHeight - minDetailsHeightInPx : getActionBarHeightPixels();
    imageAreaHeight = Math.min(screenWidth, maxHeightInPx);

    // Create the image pager, and set the listener
    imagePager = (ViewPager) findViewById(R.id.image_pager);
    imagePager.setAdapter(new ProductImagePagerAdapter(null, screenWidth, imageAreaHeight, this, 0));
    imagePager.setOnPageChangeListener(this);

    // Set the touch handler that registers flinging down/up for resizing image
    imageAreaTouchHandler = new ProductDetailsImageAreaTouchHandler(getContext(), this);
    imagePager.setOnTouchListener(imageAreaTouchHandler);

    pagingIndicator = (TabLayout) findViewById(R.id.indicator);

    ViewGroup.LayoutParams params = imageAreaWrapper.getLayoutParams();
    params.height = imageAreaHeight;
}

From source file:com.zhongsou.souyue.circle.view.PagerSlidingTabStrip.java

public PagerSlidingTabStrip(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    setFillViewport(true);/*from   ww w  .  j av  a  2  s.c  o  m*/
    setWillNotDraw(false);
    tabsContainer = new LinearLayout(context);
    tabsContainer.setOrientation(LinearLayout.HORIZONTAL);
    tabsContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    addView(tabsContainer);

    DisplayMetrics dm = getResources().getDisplayMetrics();

    scrollOffset = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, scrollOffset, dm);
    indicatorHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, indicatorHeight, dm);
    underlineHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, underlineHeight, dm);
    dividerPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dividerPadding, dm);
    tabPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, tabPadding, dm);
    dividerWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dividerWidth, dm);
    tabTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, tabTextSize, dm);

    // get system attrs (android:textSize and android:textColor)

    TypedArray a = context.obtainStyledAttributes(attrs, ATTRS);

    tabTextSize = a.getDimensionPixelSize(0, tabTextSize);
    tabTextColor = a.getColor(1, tabTextColor);
    tabTextSelectedColor = getResources().getColor(R.color.pstrip__yaowen_text_selected_color);
    //        othertabTextSelectedColor = getResources().getColor(R.color.ball_circle_text_color);
    /* tabTextSelectedColor = getResources().getColor(R.color.pstrip_text_selected_color_red);
     othertabTextSelectedColor = getResources().getColor(R.color.pstrip_text_selected_color_red);*/

    a.recycle();

    // get custom attrs

    a = context.obtainStyledAttributes(attrs, R.styleable.PagerSlidingTabStrip);

    indicatorColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsIndicatorColor, indicatorColor);
    underlineColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsUnderlineColor, underlineColor);
    dividerColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsDividerColor, dividerColor);
    indicatorHeight = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsIndicatorHeight,
            indicatorHeight);
    underlineHeight = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsUnderlineHeight,
            underlineHeight);
    dividerPadding = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsDividerPadding,
            dividerPadding);
    tabPadding = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsTabPaddingLeftRight, tabPadding);
    tabBackgroundResId = a.getResourceId(R.styleable.PagerSlidingTabStrip_pstsTabBackground,
            tabBackgroundResId);
    shouldExpand = a.getBoolean(R.styleable.PagerSlidingTabStrip_pstsShouldExpand, shouldExpand);
    scrollOffset = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsScrollOffset, scrollOffset);
    textAllCaps = a.getBoolean(R.styleable.PagerSlidingTabStrip_pstsTextAllCaps, textAllCaps);
    final String xmlns = "http://schemas.android.com/apk/res/com.zhongsou.souyue";
    //        isYaowen=attrs.getAttributeBooleanValue(xmlns, "isyaowen", false);
    //remove yaowen flag
    a.recycle();

    WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics metric = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(metric);
    displayWidth = metric.widthPixels; // ??

    rectPaint = new Paint();
    rectPaint.setAntiAlias(true);
    rectPaint.setStyle(Style.FILL);

    dividerPaint = new Paint();
    dividerPaint.setAntiAlias(true);
    dividerPaint.setStrokeWidth(dividerWidth);

    defaultTabLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.MATCH_PARENT);
    expandedTabLayoutParams = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f);

    if (locale == null) {
        locale = getResources().getConfiguration().locale;
    }
}

From source file:com.luanthanhthai.android.liteworkouttimer.TimerFragment.java

/**
 * Get orientation of device//ww w.  j  a  v  a  2  s  .  c  om
 */
public int getOrientation() {
    DisplayMetrics metrics = new DisplayMetrics();
    WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    windowManager.getDefaultDisplay().getMetrics(metrics);

    if (metrics.widthPixels < metrics.heightPixels) {
        return Configuration.ORIENTATION_PORTRAIT;
    } else {
        return Configuration.ORIENTATION_LANDSCAPE;
    }
}

From source file:com.ferdi2005.secondgram.AndroidUtilities.java

public static Point getRealScreenSize() {
    Point size = new Point();
    try {//from  w  ww.j a v a  2s. co  m
        WindowManager windowManager = (WindowManager) ApplicationLoader.applicationContext
                .getSystemService(Context.WINDOW_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            windowManager.getDefaultDisplay().getRealSize(size);
        } else {
            try {
                Method mGetRawW = Display.class.getMethod("getRawWidth");
                Method mGetRawH = Display.class.getMethod("getRawHeight");
                size.set((Integer) mGetRawW.invoke(windowManager.getDefaultDisplay()),
                        (Integer) mGetRawH.invoke(windowManager.getDefaultDisplay()));
            } catch (Exception e) {
                size.set(windowManager.getDefaultDisplay().getWidth(),
                        windowManager.getDefaultDisplay().getHeight());
                FileLog.e(e);
            }
        }
    } catch (Exception e) {
        FileLog.e(e);
    }
    return size;
}

From source file:com.uzmap.pkg.uzmodules.UISearchBar.SearchBarActivity.java

@SuppressWarnings("deprecation")
private void initData() {

    DisplayMetrics metric = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metric);
    mPref = getSharedPreferences("text", Context.MODE_PRIVATE);
    String text = mPref.getString("text", "");
    if (!TextUtils.isEmpty(text)) {
        mEditText.setText(text);//  w w  w.j av  a 2 s.  c  o m
        mEditText.setSelection(text.length());
    }

    mNavigationLayout.setBackgroundColor(config.navBgColor);

    if (TextUtils.isEmpty(config.searchBoxBgImg)) {
        BitmapDrawable bitmapDrawable = new BitmapDrawable(BitmapFactory.decodeResource(getResources(),
                UZResourcesIDFinder.getResDrawableID("mo_searchbar_bg")));
        mRelativeLayout.setBackgroundDrawable(bitmapDrawable);
    } else {
        BitmapDrawable bitmapDrawable = new BitmapDrawable(generateBitmap(config.searchBoxBgImg));
        mRelativeLayout.setBackgroundDrawable(bitmapDrawable);
    }

    if (config.cancel_bg_bitmap != null) {
        mTextView.setBackgroundDrawable(new BitmapDrawable(config.cancel_bg_bitmap));
    } else {
        mTextView.setBackgroundColor(config.cancel_bg_color);
    }

    mTextView.setTextSize(config.cancel_size);
    mTextView.setTextColor(config.cancal_color);

    LayoutParams params = new LayoutParams(UZUtility.dipToPix(config.searchBoxWidth),
            UZUtility.dipToPix(config.searchBoxHeight));
    params.setMargins(UZUtility.dipToPix(10), 0, 0, 0);

    params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
    mEditText.setLayoutParams(params);

    WindowManager wm = this.getWindowManager();
    int width = wm.getDefaultDisplay().getWidth();

    double realWidth = width * 0.80;
    LayoutParams layoutParams = new LayoutParams((int) realWidth, UZUtility.dipToPix(config.searchBoxHeight));

    layoutParams.setMargins(UZUtility.dipToPix(5), 0, 0, 0);
    layoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);

    layoutParams.topMargin = UZUtility.dipToPix(8);
    layoutParams.bottomMargin = UZUtility.dipToPix(8);

    mRelativeLayout.setLayoutParams(layoutParams);

    double cancelRealWidth = width * 0.15;

    int space = (width - (int) realWidth - (int) cancelRealWidth - UZUtility.dipToPix(5)) / 2;

    LayoutParams cancalTxtParam = new LayoutParams((int) cancelRealWidth,
            UZUtility.dipToPix(config.searchBoxHeight));

    cancalTxtParam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    cancalTxtParam.addRule(RelativeLayout.CENTER_VERTICAL);

    cancalTxtParam.rightMargin = space;
    cancalTxtParam.leftMargin = space;

    mTextView.setLayoutParams(cancalTxtParam);

    mListView.setBackgroundColor(config.list_bg_color);

    listSize = config.list_size;
    mCleanTextColor = config.clear_font_color;
    mCleanTextSize = config.clear_font_size;

    recordCount = config.historyCount;

    /**
     * add clean list item
     */
    int relativeLayoutCleanId = UZResourcesIDFinder.getResLayoutID("mo_searchbar_clean_item");
    relativeLayoutClean = (LinearLayout) View.inflate(getApplicationContext(), relativeLayoutCleanId, null);
    int tv_cleanId = UZResourcesIDFinder.getResIdID("tv_clean");
    mCleanTV = (TextView) relativeLayoutClean.findViewById(tv_cleanId);
    mCleanTV.setTextSize(mCleanTextSize);
    mCleanTV.setTextColor(mCleanTextColor);

    mCleanTV.setText(config.clearText);

    relativeLayoutClean
            .setBackgroundDrawable(addStateDrawable(config.clear_bg_color, config.clear_active_bg_color));
    list.clear();

    list.add(relativeLayoutClean);
    mPref = getSharedPreferences("history" + config.database, Context.MODE_PRIVATE);

    editor = mPref.edit();

    trimHistroyList(config.historyCount);

    @SuppressWarnings("unchecked")
    Map<String, String> map = (Map<String, String>) mPref.getAll();
    if (map != null) {
        if (map.size() != 0) {
            for (int i = 1; i <= map.size(); i++) {

                int listview_item = UZResourcesIDFinder.getResLayoutID("mo_searchbar_listview_item");
                LinearLayout linearLayout = (LinearLayout) View.inflate(SearchBarActivity.this, listview_item,
                        null);
                int tv_listId = UZResourcesIDFinder.getResIdID("tv_listview");
                TextView tv = (TextView) linearLayout.findViewById(tv_listId);
                tv.setTextSize(listSize);
                tv.setTextColor(config.list_color);

                linearLayout.setBackgroundDrawable(
                        addStateDrawable(config.list_bg_color, config.list_item_active_bg_color));

                int itemBorderLineId = UZResourcesIDFinder.getResIdID("item_border_line");
                linearLayout.findViewById(itemBorderLineId).setBackgroundColor(config.list_border_color);

                for (Entry<String, String> iterable_element : map.entrySet()) {
                    String key = iterable_element.getKey();
                    if ((i + "").equals(key)) {
                        tv.setText(iterable_element.getValue());
                    }
                }
                list.addFirst(linearLayout);
            }
            id = map.size();
        }
    }

    adapter = new MyAdapter();
    mListView.setAdapter(adapter);
}

From source file:com.facebook.android.friendsmash.GameFragment.java

@SuppressWarnings({ "deprecation" })
@TargetApi(13)/*from  w  w w  .ja  v  a  2 s .  c  om*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_game, parent, false);

    gameFrame = (FrameLayout) v.findViewById(R.id.gameFrame);
    progressContainer = (FrameLayout) v.findViewById(R.id.progressContainer);
    smashPlayerNameTextView = (TextView) v.findViewById(R.id.smashPlayerNameTextView);
    scoreTextView = (TextView) v.findViewById(R.id.scoreTextView);
    livesContainer = (LinearLayout) v.findViewById(R.id.livesContainer);

    // Set the progressContainer as invisible by default
    progressContainer.setVisibility(View.INVISIBLE);

    // Set the icon width (for the images to be smashed)
    setIconWidth(getResources().getDimensionPixelSize(R.dimen.icon_width));

    // Set the screen dimensions
    WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    if (Build.VERSION.SDK_INT >= 13) {
        Point size = new Point();
        display.getSize(size);
        setScreenWidth(size.x);
        setScreenHeight(size.y);
    } else {
        setScreenWidth(display.getWidth());
        setScreenHeight(display.getHeight());
    }

    // Always keep the Action Bar hidden
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActivity().getActionBar().hide();
    }

    // Instantiate the fireImageTask for future fired images
    fireImageTask = new Runnable() {
        public void run() {
            spawnImage(false);
        }
    };

    // Refresh the score board
    setScore(getScore());

    // Refresh the lives
    setLives(getLives());

    // Note: Images will start firing in the onResume method below

    return v;
}