Example usage for android.app Activity getResources

List of usage examples for android.app Activity getResources

Introduction

In this page you can find the example usage for android.app Activity getResources.

Prototype

@Override
    public Resources getResources() 

Source Link

Usage

From source file:Main.java

public static Drawable getDrawableFromResource(Activity activity, int id) {
    if (id > 0) {
        return activity.getResources().getDrawable(id);
    }/*from  w w w .  ja va  2  s. c o m*/
    return null;
}

From source file:Main.java

public static String loadAssetsText(Activity activity, String fileName, String charset) {
    return loadAssetsText(activity.getResources().getAssets(), fileName, charset);
}

From source file:Main.java

/**
 * Locks specified activity's orientation until it is unlocked or recreated.
 *
 * @param activity activity//from  w w  w . ja  va 2 s.  c  o  m
 */
public static void lockOrientation(Activity activity) {
    Configuration config = activity.getResources().getConfiguration();
    final int deviceOrientation = config.orientation;
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();

    int orientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
    if (deviceOrientation == Configuration.ORIENTATION_PORTRAIT) {
        orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_180)
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
    } else if (deviceOrientation == Configuration.ORIENTATION_LANDSCAPE) {
        orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270)
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
    }

    activity.setRequestedOrientation(orientation);
}

From source file:Main.java

public static int getScreenW(Activity activity) {
    DisplayMetrics dm = new DisplayMetrics();
    dm = activity.getResources().getDisplayMetrics();
    int w = dm.widthPixels;
    // int w = aty.getWindowManager().getDefaultDisplay().getWidth();
    return w;//from   w  w  w . jav  a 2  s  . co  m
}

From source file:Main.java

public static int getDialogW(Activity activity) {
    DisplayMetrics dm = new DisplayMetrics();
    dm = activity.getResources().getDisplayMetrics();
    int w = dm.widthPixels - 100;
    // int w = aty.getWindowManager().getDefaultDisplay().getWidth() - 100;
    return w;/*from   w  w  w.ja v a2  s . c  o  m*/
}

From source file:Main.java

public static int getScreenH(Activity activity) {
    DisplayMetrics dm = new DisplayMetrics();
    dm = activity.getResources().getDisplayMetrics();
    int h = dm.heightPixels;
    // int h = aty.getWindowManager().getDefaultDisplay().getHeight();
    return h;//from   w w w. java  2  s.c  o  m
}

From source file:Main.java

public static void OrientationFix(Activity context, boolean fixOrient) {
    if (fixOrient) {
        int ori = context.getResources().getConfiguration().orientation;
        if (ori == Configuration.ORIENTATION_LANDSCAPE) {
            context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {//  ww  w .  j a  v a  2 s.  com
            context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    } else {
        context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
    }
}

From source file:Main.java

/**
 * Rotates the given activity.//from   w w w .  j  a v  a  2  s  .com
 * 
 * @param activity a given activity
 */
private static void rotateActivity(Activity activity) {
    activity.setRequestedOrientation(
            activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT
                    ? ORIENTATION_LANDSCAPE
                    : ORIENTATION_PORTRAIT);
}

From source file:Main.java

/**
 * Calculates the default orientation of the device.<br>
 * The default orientation of most phones is portrait, and the default orientation of most tablets is landscape.
 * //from  ww w . jav  a  2s .  co  m
 * @param activity Activity.
 * @return {@link Configuration#ORIENTATION_PORTRAIT} or {@link Configuration#ORIENTATION_LANDSCAPE}
 */
public static int getDefaultOrientationOfDevice(Activity activity) {
    Configuration configuration = activity.getResources().getConfiguration();
    boolean orientationLandscape = (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE);

    Display display = activity.getWindowManager().getDefaultDisplay();
    int rotation = display.getRotation();
    boolean parallelToDefaultVerticalAxis = (rotation == Surface.ROTATION_0)
            || (rotation == Surface.ROTATION_180);

    boolean defaultOrientationLandscape = (parallelToDefaultVerticalAxis && orientationLandscape)
            || (!parallelToDefaultVerticalAxis && !orientationLandscape);

    if (defaultOrientationLandscape) {
        return Configuration.ORIENTATION_LANDSCAPE;
    }
    return Configuration.ORIENTATION_PORTRAIT;
}

From source file:Main.java

public static int getDrawableId(final Activity context, final String drawableName) {
    final int resId = context.getResources().getIdentifier(drawableName, "drawable", context.getPackageName());
    return resId;
}