Example usage for android.content.res Resources getString

List of usage examples for android.content.res Resources getString

Introduction

In this page you can find the example usage for android.content.res Resources getString.

Prototype

@NonNull
public String getString(@StringRes int id, Object... formatArgs) throws NotFoundException 

Source Link

Document

Return the string value associated with a particular resource ID, substituting the format arguments as defined in java.util.Formatter and java.lang.String#format .

Usage

From source file:Main.java

public static String getString(Context context, int res, Object... arg) {
    Resources resource = context.getResources();
    return resource.getString(res, arg).toString();
}

From source file:Main.java

/**
 * Return the string value associated with a particular resource ID,
 * substituting the format arguments as defined in {@link java.util.Formatter}
 * and {@link java.lang.String#format}. It will be stripped of any styled text
 * information.//w  w w. j  a va 2 s. com
 *
 * @param id         The desired resource identifier, as generated by the aapt
 *                   tool. This integer encodes the package, type, and resource
 *                   entry. The value 0 is an invalid identifier.
 * @param formatArgs The format arguments that will be used for substitution.
 * @return String The string data associated with the resource,
 * stripped of styled text information.
 * @throws Resources.NotFoundException Throws NotFoundException if the given ID does not exist.
 */
@NonNull
public static String getString(@NonNull Resources resources, @StringRes int id, Object... formatArgs) {
    try {
        return resources.getString(id, formatArgs);
    } catch (IllegalFormatException e) {
        final String message = "Failed to format the string resource! The user locale is:"
                + Locale.getDefault().toString();
        throw new IllegalArgumentException(message, e);
    }
}

From source file:Main.java

private static void getFormatTime(Context context, Time time, int i, boolean flag, CharBuffer charbuffer) {
    long l = time.toMillis(true);
    boolean flag1;
    if ((i & 1) != 0)
        flag1 = true;/*from w w w . ja  v a2 s .c  om*/
    else
        flag1 = false;
    if (flag)
        charbuffer.append(DateUtils.formatDateRange(context, l, l, i));
    else if (flag1) {
        if ((i ^ 1) != 0) {
            charbuffer.append(DateUtils.formatDateRange(context, l, l, i ^ 1));
            charbuffer.append(" ");
        }
        String s = getDetailedAmPm(time.hour);
        if (time.hour > 12)
            time.hour = -12 + time.hour;
        long l1 = time.toMillis(true);
        String s1 = DateUtils.formatDateRange(context, l1, l1, 129);
        Resources resources = Resources.getSystem();
        Object aobj[] = new Object[2];
        aobj[0] = s1;
        aobj[1] = s;
        charbuffer.append(resources.getString(0x60c01f7, aobj));
    } else {
        charbuffer.append(DateUtils.formatDateRange(context, l, l, i));
    }
}

From source file:com.baasbox.android.samples.aloa.receive.NewMessageNotification.java

/**
 * Shows the notification, or updates a previously shown notification of
 * this type, with the given parameters.
 * <p>//from   w ww  .  j  av  a 2  s.c om
 * TODO: Customize this method's arguments to present relevant content in
 * the notification.
 * <p>
 * TODO: Customize the contents of this method to tweak the behavior and
 * presentation of new message notifications. Make
 * sure to follow the
 * <a href="https://developer.android.com/design/patterns/notifications.html">
 * Notification design guidelines</a> when doing so.
 *
 * @see #cancel(Context)
 */
public static void notify(final Context context, final String exampleString, final int number) {
    final Resources res = context.getResources();

    final String ticker = exampleString;
    final String title = res.getString(R.string.new_message_notification_title_template, exampleString);
    final String text = res.getString(R.string.new_message_notification_placeholder_text_template,
            exampleString);

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context)

            // Set appropriate defaults for the notification light, sound,
            // and vibration.
            .setDefaults(Notification.DEFAULT_ALL)

            // Set required fields, including the small icon, the
            // notification title, and text.
            .setSmallIcon(R.drawable.ic_stat_new_message).setContentTitle(title).setContentText(text)

            // All fields below this line are optional.

            // Use a default priority (recognized on devices running Android
            // 4.1 or later)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)

            // Set ticker text (preview) information for this notification.
            .setTicker(ticker)

            // Show a number. This is useful when stacking notifications of
            // a single type.
            .setNumber(number)

            // Set the pending intent to be initiated when the user touches
            // the notification.
            .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class),
                    PendingIntent.FLAG_UPDATE_CURRENT))

            // Automatically dismiss the notification when it is touched.
            .setAutoCancel(true);

    notify(context, builder.build());
}

From source file:com.google.android.apps.forscience.whistlepunk.metadata.TriggerHelper.java

public static void populateAutoTextViews(TextView autoTextView, String autoText, int drawableId,
        Resources res) {
    autoTextView.setText(autoText);/*from   w  w w .  j  a  v  a  2 s.  c om*/
    autoTextView
            .setContentDescription(res.getString(R.string.trigger_label_icon_content_description, autoText));
    Drawable drawable = res.getDrawable(drawableId);
    DrawableCompat.setTint(drawable.mutate(), res.getColor(R.color.text_color_light_grey));
    autoTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, null, null, null);
}

From source file:com.google.android.gms.common.internal.C1339l.java

public static String m4746c(Context context, int i) {
    Resources resources = context.getResources();
    String a = C1339l.m4741a(context);
    switch (i) {//from www.  j  a  va2 s .c om
    case GoogleMap.MAP_TYPE_NORMAL /*1*/:
        return resources.getString(C1095b.common_google_play_services_install_text, new Object[] { a });
    case GoogleMap.MAP_TYPE_SATELLITE /*2*/:
        if (C1380f.m4943b(context)) {
            return resources.getString(C1095b.common_google_play_services_wear_update_text);
        }
        return resources.getString(C1095b.common_google_play_services_update_text, new Object[] { a });
    case GoogleMap.MAP_TYPE_TERRAIN /*3*/:
        return resources.getString(C1095b.common_google_play_services_enable_text, new Object[] { a });
    case C1096c.MapAttrs_cameraZoom /*5*/:
        return C1339l.m4744a(context, "common_google_play_services_invalid_account_text", a);
    case C1096c.MapAttrs_uiCompass /*7*/:
        return C1339l.m4744a(context, "common_google_play_services_network_error_text", a);
    case C1096c.MapAttrs_uiScrollGestures /*9*/:
        return resources.getString(C1095b.common_google_play_services_unsupported_text, new Object[] { a });
    case C1096c.MapAttrs_ambientEnabled /*16*/:
        return C1339l.m4744a(context, "common_google_play_services_api_unavailable_text", a);
    case C1096c.MapAttrs_cameraMinZoomPreference /*17*/:
        return C1339l.m4744a(context, "common_google_play_services_sign_in_failed_text", a);
    case C1096c.MapAttrs_cameraMaxZoomPreference /*18*/:
        return resources.getString(C1095b.common_google_play_services_updating_text, new Object[] { a });
    case C1096c.MapAttrs_latLngBoundsSouthWestLongitude /*20*/:
        return C1339l.m4744a(context, "common_google_play_services_restricted_profile_text", a);
    default:
        return resources.getString(C1095b.common_google_play_services_unknown_issue, new Object[] { a });
    }
}

From source file:com.android.mail.utils.EmptyStateUtils.java

/**
 * Given an empty folder, set the corresponding text for indicating the empty state.
 *///from w  w w  .j a va  2s  .co m
public static void bindEmptyFolderText(TextView view, Folder folder, Resources res, String searchQuery,
        BidiFormatter bidiFormatter) {
    if (folder == null) {
        view.setText(R.string.empty_folder);
    } else if (folder.isInbox()) {
        view.setText(R.string.empty_inbox);
    } else if (folder.isSearch()) {
        final String text = res.getString(R.string.empty_search, bidiFormatter.unicodeWrap(searchQuery));
        view.setText(text);
    } else if (folder.isSpam()) {
        view.setText(R.string.empty_spam_folder);
    } else if (folder.isTrash()) {
        view.setText(R.string.empty_trash_folder);
    } else {
        view.setText(R.string.empty_folder);
    }
}

From source file:com.dabay6.libraries.androidshared.ui.dialogs.changelog.util.ChangeLogDialogUtils.java

/**
 * @param context   {@link Context} used to retrieve resources.
 * @param assetName Name of the asset file containing the change log json.
 * @param style     The css style to be applied to the html.
 * @param callback  The callback to call upon dismissal of the change log alert dialog.
 *
 * @return {@link AlertDialog} used to display the change log.
 *//*from   w  w  w .  java 2s .  c o m*/
public static AlertDialog createDialog(final Context context, final String assetName, final String style,
        final OnChangeLogDialogListener callback) {
    final AlertDialog.Builder builder;
    final Resources res = context.getResources();
    final String title = res.getString(R.string.change_log_title, AppUtils.getApplicationVersion(context));
    final WebSettings settings;
    final WebView web = new WebView(context);
    String html;

    try {
        html = getHtmlChangeLog(context, assetName, style);

        settings = web.getSettings();
        settings.setSupportZoom(false);
    } catch (final IOException ex) {
        html = StringUtils.empty();

        Logger.error(TAG, ex.getMessage(), ex);
    }

    web.loadData(html, "text/html", "utf-8");

    builder = new AlertDialog.Builder(context);
    builder.setTitle(title).setView(web).setPositiveButton(R.string.change_log_close,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(final DialogInterface dialog, final int which) {
                    dialog.dismiss();
                    if (callback != null) {
                        callback.onChangeLogDismissed();
                    }
                }
            });

    return builder.create();
}

From source file:com.deepak.myclock.alarms.AlarmNotifications.java

public static void showSnoozeNotification(Context context, AlarmInstance instance) {
    Log.v("Displaying snoozed notification for alarm instance: " + instance.mId);
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Resources resources = context.getResources();
    NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
            .setContentTitle(instance.getLabelOrDefault(context))
            .setContentText(resources.getString(R.string.alarm_alert_snooze_until,
                    AlarmUtils.getFormattedTime(context, instance.getAlarmTime())))
            .setSmallIcon(R.drawable.stat_notify_alarm).setOngoing(true).setAutoCancel(false)
            .setPriority(NotificationCompat.PRIORITY_MAX);

    // Setup up dismiss action
    Intent dismissIntent = AlarmStateManager.createStateChangeIntent(context, "DISMISS_TAG", instance,
            AlarmInstance.DISMISSED_STATE);
    notification.addAction(android.R.drawable.ic_menu_close_clear_cancel,
            resources.getString(R.string.alarm_alert_dismiss_text), PendingIntent.getBroadcast(context,
                    instance.hashCode(), dismissIntent, PendingIntent.FLAG_UPDATE_CURRENT));

    // Setup content action if instance is owned by alarm
    long alarmId = instance.mAlarmId == null ? Alarm.INVALID_ID : instance.mAlarmId;
    Intent viewAlarmIntent = Alarm.createIntent(context, MainActivity.class, alarmId);
    //viewAlarmIntent.putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.ALARM_TAB_INDEX);
    viewAlarmIntent.putExtra(AlarmClockFragment.SCROLL_TO_ALARM_INTENT_EXTRA, alarmId);
    viewAlarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    notification.setContentIntent(PendingIntent.getActivity(context, instance.hashCode(), viewAlarmIntent,
            PendingIntent.FLAG_UPDATE_CURRENT));
    nm.cancel(instance.hashCode());/*  w ww  . j  a  va2 s.c  o m*/
    nm.notify(instance.hashCode(), notification.build());
}

From source file:com.embeddedlog.LightUpDroid.alarms.AlarmNotifications.java

public static void showSnoozeNotification(Context context, AlarmInstance instance) {
    Log.v("Displaying snoozed notification for alarm instance: " + instance.mId);
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Resources resources = context.getResources();
    NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
            .setContentTitle(instance.getLabelOrDefault(context))
            .setContentText(resources.getString(R.string.alarm_alert_snooze_until,
                    AlarmUtils.getFormattedTime(context, instance.getAlarmTime())))
            .setSmallIcon(R.drawable.stat_notify_alarm).setOngoing(true).setAutoCancel(false)
            .setPriority(NotificationCompat.PRIORITY_MAX).setCategory(NotificationCompat.CATEGORY_ALARM);

    // Setup up dismiss action
    Intent dismissIntent = AlarmStateManager.createStateChangeIntent(context, "DISMISS_TAG", instance,
            AlarmInstance.DISMISSED_STATE);
    notification.addAction(android.R.drawable.ic_menu_close_clear_cancel,
            resources.getString(R.string.alarm_alert_dismiss_text), PendingIntent.getBroadcast(context,
                    instance.hashCode(), dismissIntent, PendingIntent.FLAG_UPDATE_CURRENT));

    // Setup content action if instance is owned by alarm
    long alarmId = instance.mAlarmId == null ? Alarm.INVALID_ID : instance.mAlarmId;
    Intent viewAlarmIntent = Alarm.createIntent(context, DeskClock.class, alarmId);
    viewAlarmIntent.putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.ALARM_TAB_INDEX);
    viewAlarmIntent.putExtra(AlarmClockFragment.SCROLL_TO_ALARM_INTENT_EXTRA, alarmId);
    viewAlarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    notification.setContentIntent(PendingIntent.getActivity(context, instance.hashCode(), viewAlarmIntent,
            PendingIntent.FLAG_UPDATE_CURRENT));
    nm.cancel(instance.hashCode());//from www  .j a v a  2 s  .c o m
    nm.notify(instance.hashCode(), notification.build());
}