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

@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public static void setBackground(Activity activity, int background) {

    activity.getActionBar()// www . j av  a2  s.  c  om
            .setIcon(new ColorDrawable(activity.getResources().getColor(android.R.color.transparent)));

    Drawable draw;
    if (android.os.Build.VERSION.SDK_INT >= 21) {
        draw = activity.getResources().getDrawable(background, activity.getTheme());
        activity.getActionBar().setBackgroundDrawable(draw);
    } else {
        draw = activity.getResources().getDrawable(background);
        activity.getActionBar().setBackgroundDrawable(draw);
    }
}

From source file:Main.java

public static int findByValue(Activity activity, String value) {
    value = value.replaceAll("\\D", "_");
    value = "day_" + value;

    int id = activity.getResources().getIdentifier(value, "color", activity.getPackageName());

    int idColor = (id == 0) ? R.color.darker_gray : id;

    return activity.getResources().getColor(idColor);
}

From source file:Main.java

public static void alertActivityFinish(final Activity activity, String msg) {
    AlertDialog.Builder ad = new AlertDialog.Builder(activity);
    ad.setIcon(activity.getResources().getDrawable(android.R.drawable.ic_dialog_alert)); // Android Resource
    ad.setTitle("Sky Canvas").setMessage(msg).setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // finish dialog
            dialog.dismiss();/*from   ww  w  .  ja v a2  s. com*/
            activity.finish();
        }
    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

        }
    }).show();
    ad = null;
}

From source file:Main.java

public static void showProgressDialog(Activity context, int stringResId) {
    try {//from   www .java2 s .com
        showProgressDialog(context, null, context.getResources().getString(stringResId));
    } catch (Exception e) {
        Log.e(TAG,
                "showProgressDialog  showProgressDialog(Context context, null, context.getResources().getString(stringResId));");
    }
}

From source file:Main.java

public static int getColor(Activity activity, String attr, int fallbackColor) {
    TypedValue color = new TypedValue();
    try {/*from  w  ww .  j av a 2  s.c o  m*/
        int colorId = activity.getResources().getIdentifier(attr, "attr", activity.getPackageName());
        if (activity.getTheme().resolveAttribute(colorId, color, true)) {
            return color.data;
        }
    } catch (Exception ignored) {
    }

    return activity.getResources().getColor(fallbackColor);
}

From source file:Main.java

/**
 * Same as {@link #toggleOrientation(Activity)} except {@link Instrumentation#waitForIdleSync()}
 * is called after each orientation change.
 *
 * @param activity whose orientation will be changed and restored
 * @param instrumentation use for idle syncing
 *//*from w  w  w  .j  a v a2 s .c  o m*/
public static void toggleOrientationSync(final Activity activity, final Instrumentation instrumentation) {
    final int originalOrientation = activity.getResources().getConfiguration().orientation;
    final int newOrientation = originalOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
            ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
            : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    changeOrientation(activity, instrumentation, newOrientation);
    changeOrientation(activity, instrumentation, originalOrientation);
}

From source file:Main.java

/**
 * Returns the view container for the ActionBar.
 *
 * @return//from www .j a  va  2s  .  c  o  m
 */
public static View getActionBarView(Activity activity) {
    Window window = activity.getWindow();
    View view = window.getDecorView();
    int resId = activity.getResources().getIdentifier("action_bar_container", "id", "android");

    return view.findViewById(resId);
}

From source file:Main.java

public static int getStatusBarHeight(Activity activity) {
    int statusBarHeight = 0;

    if (!hasOnScreenSystemBar(activity)) {
        int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0)
            statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);
    }//  ww  w. j  a v  a 2s  . co  m

    return statusBarHeight;
}

From source file:Main.java

/**
 * Set default language//from  ww w. jav a 2 s  . c  om
 * 
 * @param act
 * @param defLanguage
 */
public static void setDefaultLanguage(Activity act, String defLanguage) {
    if (defLanguage != null) {
        // Change locale settings in the app.
        Resources res = act.getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        android.content.res.Configuration conf = res.getConfiguration();
        conf.locale = new Locale(defLanguage);
        res.updateConfiguration(conf, dm);
    }
}

From source file:Main.java

/**
 * Set action bar background//from w ww .j a va 2s  .c  om
 *
 * @param activity   The activity to set ActionBar background to.
 * @param resColorId The Drawable color to set the ActionBar to.
 */
public static void setActionBarBackground(Activity activity, int resColorId) {
    // change action bar color
    activity.getActionBar()
            .setBackgroundDrawable(new ColorDrawable(activity.getResources().getInteger(resColorId)));
    activity.getActionBar().setDisplayShowTitleEnabled(false);
    activity.getActionBar().setDisplayShowTitleEnabled(true);
}