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:fi.tuukka.weather.utils.Utils.java

public static int getScaledFont(Activity activity) {
    DisplayMetrics displayMetrics = activity.getResources().getDisplayMetrics();
    return displayMetrics.widthPixels / 27;
}

From source file:fi.tuukka.weather.utils.Utils.java

public static int dpToPx(int dp, Activity activity) {
    DisplayMetrics displayMetrics = activity.getResources().getDisplayMetrics();
    int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    return px;//w  w w  . ja v  a 2s. c  om
}

From source file:Main.java

public static ProgressDialog createProgressDialog(final Activity activity, int progressDialogTitleId,
        int progressDialogMsgId) {
    ProgressDialog progressDialog = new ProgressDialog(activity);
    if (progressDialogTitleId > 0) {
        progressDialog.setTitle(progressDialogTitleId);
    } else {/* w ww. j  av  a 2  s .  c  om*/
        progressDialog.setTitle(activity.getResources().getIdentifier(PROGRESS_DIALOG_TITLE_RESOURCE, "string",
                activity.getPackageName()));
    }
    if (progressDialogMsgId > 0) {
        progressDialog.setMessage(activity.getString(progressDialogMsgId));
    } else {
        progressDialogMsgId = activity.getResources().getIdentifier(PROGRESS_DIALOG_MESSAGE_RESOURCE, "string",
                activity.getPackageName());
        progressDialog.setMessage(activity.getString(progressDialogMsgId));
    }
    progressDialog.setIndeterminate(true);
    progressDialog.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            activity.onKeyDown(keyCode, event);
            return false;
        }
    });
    // progressDialog.setInverseBackgroundForced(true);
    return progressDialog;
}

From source file:fi.tuukka.weather.utils.Utils.java

public static int[] getEnnusteWidths(Activity activity) {
    DisplayMetrics displayMetrics = activity.getResources().getDisplayMetrics();
    int width = displayMetrics.widthPixels;
    float[] fraqs = new float[] { .10f, .18f };
    int[] widths = new int[fraqs.length];
    for (int i = 0; i < widths.length; i++) {
        widths[i] = Math.round(width * fraqs[i]);
    }//from   w w  w. j  a v a2 s.  co m
    return widths;
}

From source file:Main.java

private static int getStatusBarHeight(Activity activity) {
    Class<?> c = null;/*from ww  w  . ja va2  s . com*/
    Object obj = null;
    Field field = null;
    int x = 0, sbar = 0;
    try {
        c = Class.forName("com.android.internal.R$dimen");
        obj = c.newInstance();
        field = c.getField("status_bar_height");
        x = Integer.parseInt(field.get(obj).toString());
        sbar = activity.getResources().getDimensionPixelSize(x);
        return sbar;
    } catch (Exception e1) {
        //       loge("get status bar height fail");
        e1.printStackTrace();
        return 0;
    }
}

From source file:Main.java

/**
 * @see "http://blog.peterkuterna.net/2011/09/simple-crossfade-on-imageview.html"
 * with modifications by Thomas Suarez./*from  w  w  w .  ja  va  2s  .  c  om*/
 */
public static void setImageDrawableWithFade(final Activity context, final ImageView imageView,
        final String drawableName, final int durationMillis) {
    int resId = getDrawableId(context, drawableName); // get new drawable resource
    Drawable drawable;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        drawable = context.getResources().getDrawable(resId, context.getTheme());
    } else {
        drawable = context.getResources().getDrawable(resId); // this is deprecated starting in Android 5.0 Lollipop
    }

    final Drawable currentDrawable = imageView.getDrawable();
    final Drawable newDrawable = drawable;

    Runnable r = new Runnable() {
        @Override
        public void run() {
            if (currentDrawable != null) {
                Drawable[] arrayDrawable = new Drawable[2];
                arrayDrawable[0] = currentDrawable;
                arrayDrawable[1] = newDrawable;
                TransitionDrawable transitionDrawable = new TransitionDrawable(arrayDrawable);
                transitionDrawable.setCrossFadeEnabled(true);
                imageView.setImageDrawable(transitionDrawable);
                transitionDrawable.startTransition(durationMillis);
            } else {
                imageView.setImageDrawable(newDrawable);
            }
        }
    };
    context.runOnUiThread(r);
}

From source file:Main.java

public static void modifyByte(Activity activity, String srcFile, String destFile, int byteNum,
        byte byteContents) {
    //        throw new UnsupportedOperationException("Not yet implemented");
    //        UIUtils.log("Modifying byte: " + byteNum + " in Asset file: " + srcFile);
    try {/*from   w ww.ja  v a  2  s  .  c om*/
        AssetManager am = activity.getResources().getAssets(); // get the local asset manager
        InputStream is = am.open(srcFile); // open the input stream for reading
        OutputStream os = new FileOutputStream(destFile);
        byte[] buf = new byte[8092];
        int n;
        int totalBytes = 0;
        while ((n = is.read(buf)) > 0) {
            totalBytes += n;
            //                UIUtils.log("Total Bytes read: " + totalBytes);
            if (byteNum <= totalBytes) {
                //                    UIUtils.log("Replacing byte: " + (totalBytes - n + byteNum ) + " contents with: " + byteContents);
                buf[totalBytes - n + byteNum] = byteContents;
            }
            os.write(buf, 0, n);
        }
        //            UIUtils.log(ByteUtils.ByteArrayToString(buf));
        os.close();
        is.close();
    } catch (Exception ex) {
        Log.e("Installer", "failed to modify file: " + ex);
    }
}

From source file:com.koushikdutta.superuser.helper.Theme.java

public static void handleTheme(Activity context, String theme, int textToolbarDefault, DrawerLayout drawer,
        AppBarLayout appBar, Toolbar toolbar, TabLayout tabLayout) {

    if (!theme.equals(PREF_BLACK_THEME)) {
        ATH.setStatusbarColor(context, context.getResources().getInteger(R.integer.statusbar_tint));

        if (drawer != null)
            drawer.setStatusBarBackgroundColor(ThemeStore.statusBarColor(context));
        else/*ww w .j a v a2s.  c o m*/
            ((CoordinatorLayout) context.findViewById(R.id.coordinator))
                    .setStatusBarBackgroundColor(ThemeStore.statusBarColor(context));

        if (appBar != null)
            appBar.setBackgroundColor(ThemeStore.primaryColor(context));
        else
            toolbar.setBackgroundColor(ThemeStore.primaryColor(context));

        ToolbarContentTintHelper.setToolbarContentColor(context, toolbar, null, textToolbarDefault,
                textToolbarDefault, textToolbarDefault, 0xff424242);

        int tabTextColor = PreferenceManager.getDefaultSharedPreferences(context).getInt("tab_text_selected",
                textToolbarDefault);

        if (tabLayout != null)
            tabLayout.setTabTextColors(
                    //ColorUtil.isColorLight(tabTextColor) ? ColorUtil.shiftColor(tabTextColor, 0.9f) : ColorUtil.shiftColor(tabTextColor, 1.5f),
                    Color.argb(180, Color.red(tabTextColor), Color.green(tabTextColor),
                            Color.blue(tabTextColor)),
                    tabTextColor);

    } else {
        ToolbarContentTintHelper.setToolbarContentColor(context, toolbar, null, 0xffeaeaea, 0xffeaeaea,
                0xffeaeaea, 0xff424242);

        if (tabLayout != null)
            tabLayout.setTabTextColors(ColorUtil.shiftColor(0xffeaeaea, 1.5f), 0xffeaeaea);
    }
}

From source file:net.vexelon.myglob.utils.Utils.java

/**
 * Display an alert dialog using resource IDs
 * @param context//from ww  w.j a va2  s .co m
 * @param messageResId
 * @param titleResId
 */
public static void showAlertDialog(Activity activity, int messageResId, int titleResId) {
    showAlertDialog(activity, activity.getResources().getString(messageResId),
            activity.getResources().getString(titleResId));
}

From source file:com.zphinx.sortattributes.SortDialogManager.java

/**
 * Sets the image to be shown by the drawable associated with the
 * StateTextview//from  w ww  .  j a  v a 2 s  .  c o m
 *
 * @param text
 *            The state textview object whose image is to be set
 * @param draws
 *            The array of drawables used by the textview
 * @param activity
 *            - The activity which uses this dialog
 * @throws NotFoundException
 *             If an error occurs
 */

private static void setCurrentImage(final StateTextView text, Drawable[] draws, Activity activity)
        throws NotFoundException {
    Resources res = activity.getResources();
    Integer curState = text.getSortDirection();
    final Integer newState = (curState == StateTextView.SORT_UNINITIALIZED) ? StateTextView.SORT_DESC
            : (curState == StateTextView.SORT_DESC) ? StateTextView.SORT_ASC : StateTextView.SORT_DESC;

    switch (newState) {
    case StateTextView.SORT_DESC:
        draws[2] = ResourcesCompat.getDrawable(res, R.drawable.icon_bottom_arrow, null);
        break;
    case StateTextView.SORT_ASC:
        draws[2] = ResourcesCompat.getDrawable(res, R.drawable.icon_top_arrow, null);
        break;
    default:
        draws[2] = null;
        break;
    }

    Log.d(TAG, "The drawable to draw is: ........................." + draws[2] + " The oldState is: " + curState
            + " The new state is: " + newState);
    Rect bounds = new Rect(0, 0, draws[2].getIntrinsicWidth(), draws[2].getIntrinsicHeight());
    draws[2].setBounds(bounds);
    text.setCompoundDrawables(null, null, draws[2], null);
    text.setSortDirection(newState);

}