Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.app.Activity;

import android.view.Display;
import android.view.View;

import android.view.Window;

public class Main {
    /**
     * Tries to get the available width from the (previously measured) view of the activity.
     * If if fails then calls getDisplayWidth().
     */
    private static int getWindowWidth(Activity activity) {
        int width = 0;

        //Doesn't work on create method, should call it from another place?
        View content = activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT);
        if (content != null)
            width = content.getWidth();

        if (width == 0)
            width = getDisplayWidth(activity);

        return width;
    }

    /**
     * Gets the available width using only the DisplayMetrics class and removing known decorations.
     */
    @SuppressWarnings("deprecation")
    public static int getDisplayWidth(Activity activity) {
        Display display = activity.getWindowManager().getDefaultDisplay();
        return display.getWidth();
    }
}