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:com.aosijia.dragonbutler.ui.widget.ActionSheet.java

public static int getNavBarHeight(Activity activity) {
    Resources resources = activity.getResources();
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
    //?NavigationBar
    int height = resources.getDimensionPixelSize(resourceId);
    return height;
}

From source file:com.nextgis.maplibui.util.ControlHelper.java

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static void lockScreenOrientation(Activity activity) {
    WindowManager windowManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
    Configuration configuration = activity.getResources().getConfiguration();
    int rotation = windowManager.getDefaultDisplay().getRotation();

    // Search for the natural position of the device
    if (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
            && (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180)
            || configuration.orientation == Configuration.ORIENTATION_PORTRAIT
                    && (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270)) {
        // Natural position is Landscape
        switch (rotation) {
        case Surface.ROTATION_0:
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            break;
        case Surface.ROTATION_90:
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
            break;
        case Surface.ROTATION_180:
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
            break;
        case Surface.ROTATION_270:
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            break;
        }//ww  w . java  2  s  . c  om
    } else {
        // Natural position is Portrait
        switch (rotation) {
        case Surface.ROTATION_0:
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            break;
        case Surface.ROTATION_90:
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            break;
        case Surface.ROTATION_180:
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
            break;
        case Surface.ROTATION_270:
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
            break;
        }
    }
}

From source file:nl.mpcjanssen.simpletask.util.Util.java

public static AlertDialog createDeferDialog(final Activity act, int dateType, final boolean showNone,
        final InputDialogListener listener) {
    String[] keys = act.getResources().getStringArray(R.array.deferOptions);
    String today = "0d";
    String tomorrow = "1d";
    String oneWeek = "1w";
    String twoWeeks = "2w";
    String oneMonth = "1m";
    final String[] values = { "", today, tomorrow, oneWeek, twoWeeks, oneMonth, "pick" };
    if (!showNone) {
        keys = Arrays.copyOfRange(keys, 1, keys.length);
    }/*  w  w w .j  a  v  a2  s. com*/
    int titleId;
    if (dateType == Task.DUE_DATE) {
        titleId = R.string.defer_due;
    } else {
        titleId = R.string.defer_threshold;
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(act);
    builder.setTitle(titleId);
    builder.setItems(keys, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int whichButton) {
            if (!showNone) {
                whichButton++;
            }
            String selected = values[whichButton];
            listener.onClick(selected);
        }
    });
    return builder.create();
}

From source file:Main.java

public static int getStatusBarHeight(Activity context) {
    int statusHeight = 0;
    Rect frame = new Rect();
    context.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    statusHeight = frame.top;//from   ww w . j  av a 2 s .  c  o m
    if (0 == statusHeight) {
        Class<?> localClass;
        try {
            localClass = Class.forName("com.android.internal.R$dimen");
            Object localObject = localClass.newInstance();
            int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());
            statusHeight = context.getResources().getDimensionPixelSize(i5);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return statusHeight;
}

From source file:de.wikilab.android.friendica01.Max.java

/**
* Calls the api method <code>verify_credentials</code> with the login data
 * saved in the <code>SharedPreferences</code>. Calls ctx.OnLogin on success,
 * showLoginForm(ctx, "error") on error//from  www  .  j a  v  a  2 s . c  o m
* @param ctx     MUST IMPLEMENT LoginListener !!!
*/
public static void tryLogin(final Activity ctx) {

    final ProgressDialog pd = new ProgressDialog(ctx);
    pd.setMessage(ctx.getResources().getString(R.string.logging_in));
    pd.show();

    String server = Max.getServer(ctx);
    Log.i(TAG, "tryLogin on server " + server);

    final TwAjax t = new TwAjax(ctx, true, true);
    t.getUrlContent(server + "/api/account/verify_credentials", new Runnable() {
        @Override
        public void run() {
            pd.dismiss();
            try {
                if (t.isSuccess()) {
                    Log.i(TAG, "... tryLogin - http status: " + t.getHttpCode());
                    if (t.getHttpCode() == 200) {
                        JSONObject r = (JSONObject) t.getJsonResult();
                        String name = r.getString("name");
                        ((TextView) ctx.findViewById(R.id.selected_clipboard)).setText(name);
                        ((LoginListener) ctx).OnLogin();

                        final TwAjax profileImgDl = new TwAjax();
                        final String targetFs = IMG_CACHE_DIR + "/my_profile_pic_" + r.getString("id") + ".jpg";
                        if (new File(targetFs).isFile()) {
                            ((ImageView) ctx.findViewById(R.id.profile_image))
                                    .setImageURI(Uri.parse("file://" + targetFs));
                        } else {
                            profileImgDl.urlDownloadToFile(r.getString("profile_image_url"), targetFs,
                                    new Runnable() {
                                        @Override
                                        public void run() {
                                            ((ImageView) ctx.findViewById(R.id.profile_image))
                                                    .setImageURI(Uri.parse("file://" + targetFs));
                                        }
                                    });
                        }

                    } else {
                        showLoginForm(ctx, "Error: " + t.getResult());
                    }
                } else {
                    Log.w(TAG, "... tryLogin - request failed");
                    showLoginForm(ctx, "ERR: " + t.getError().toString());
                }

            } catch (Exception ex) {
                Log.w(TAG, "... tryLogin - exception:");
                ex.printStackTrace();
                showLoginForm(ctx, "ERR2: " + t.getResult() + ex.toString());

            }
        }
    });
}

From source file:org.kontalk.util.SystemUtils.java

/**
 * Returns the correct screen orientation based on the supposedly preferred
 * position of the device.//from   w  ww  . ja v a2 s  .  co  m
 * http://stackoverflow.com/a/16585072/1045199
 */
public static int getScreenOrientation(Activity activity) {
    WindowManager windowManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
    Configuration configuration = activity.getResources().getConfiguration();
    int rotation = windowManager.getDefaultDisplay().getRotation();

    // Search for the natural position of the device
    if (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
            && (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180)
            || configuration.orientation == Configuration.ORIENTATION_PORTRAIT
                    && (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270)) {
        // Natural position is Landscape
        switch (rotation) {
        case Surface.ROTATION_0:
            return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        case Surface.ROTATION_90:
            return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
        case Surface.ROTATION_180:
            return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
        case Surface.ROTATION_270:
            return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        }
    } else {
        // Natural position is Portrait
        switch (rotation) {
        case Surface.ROTATION_0:
            return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        case Surface.ROTATION_90:
            return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        case Surface.ROTATION_180:
            return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
        case Surface.ROTATION_270:
            return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
        }
    }

    return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
}

From source file:Main.java

/**
 * @param activity//w  ww  .  j a  v a  2s .  c  o m
 * @return > 0 success; <= 0 fail
 */
public static int getStatusHeight(Activity activity) {
    int statusHeight = 0;
    Rect localRect = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
    statusHeight = localRect.top;
    if (0 == statusHeight) {
        Class<?> localClass;
        try {
            localClass = Class.forName("com.android.internal.R$dimen");
            Object localObject = localClass.newInstance();
            int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());
            statusHeight = activity.getResources().getDimensionPixelSize(i5);
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException
                | IllegalArgumentException | SecurityException | NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
    return statusHeight;
}

From source file:net.idlesoft.android.apps.github.activities.Commit.java

/**
 * Get the Gravatars of all users in the commit log
 *//*from  w  w  w .j a  va 2  s  .c om*/
public static Bitmap loadGravatarByLoginName(final Activity pActivity, final String pLogin) {
    if (pLogin != null && !pLogin.equals("")) {
        return GravatarCache.getDipGravatar(GravatarCache.getGravatarID(pLogin), 30.0f,
                pActivity.getResources().getDisplayMetrics().density);
    } else {
        return null;
    }
}

From source file:Main.java

public static int getStatusHeight(Activity activity) {
    int statusHeight = 0;
    Rect localRect = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
    statusHeight = localRect.top;//from   w  ww.j  a  va2 s .c o  m
    if (0 == statusHeight) {
        Class<?> localClass;
        try {
            localClass = Class.forName("com.android.internal.R$dimen");
            Object localObject = localClass.newInstance();
            int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());
            statusHeight = activity.getResources().getDimensionPixelSize(i5);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
    return statusHeight;
}

From source file:jahirfiquitiva.iconshowcase.utilities.utils.Utils.java

public static int getNavigationBarHeight(Activity activity) {
    // getRealMetrics is only available with API 17 and +
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int usableHeight = activity.getResources().getBoolean(R.bool.isTablet) ? metrics.heightPixels
                : activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
                        ? metrics.widthPixels
                        : metrics.heightPixels;
        activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
        int realHeight = activity.getResources().getBoolean(R.bool.isTablet) ? metrics.heightPixels
                : activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
                        ? metrics.widthPixels
                        : metrics.heightPixels;
        if (realHeight > usableHeight)
            return realHeight - usableHeight;
        else/*from w  ww . j  av a2  s. c  o  m*/
            return 0;
    }
    return 0;
}