Example usage for android.content.res Resources getQuantityString

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

Introduction

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

Prototype

@NonNull
public String getQuantityString(@PluralsRes int id, int quantity, Object... formatArgs)
        throws NotFoundException 

Source Link

Document

Formats the string necessary for grammatically correct pluralization of the given resource ID for the given quantity, using the given arguments.

Usage

From source file:com.gsoc.ijosa.liquidgalaxycontroller.PW.UrlDeviceDiscoveryService.java

/**
 * Create or update the a single notification that is a collapsed version
 * of the top two beacon notifications./*from ww w. j  a v a  2 s .  c om*/
 */
private void updateSummaryNotification(List<PwPair> pwPairs) {
    int numNearbyBeacons = pwPairs.size();
    String contentTitle = String.valueOf(numNearbyBeacons);
    Resources resources = getResources();
    contentTitle += " "
            + resources.getQuantityString(R.plurals.numFoundBeacons, numNearbyBeacons, numNearbyBeacons);
    String contentText = getString(R.string.summary_notification_pull_down);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.drawable.ic_notification).setContentTitle(contentTitle).setContentText(contentText)
            .setSmallIcon(R.drawable.ic_notification).setGroup(NOTIFICATION_GROUP_KEY).setGroupSummary(true)
            .setPriority(NOTIFICATION_PRIORITY).setContentIntent(createReturnToAppPendingIntent());
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder.setVisibility(NOTIFICATION_VISIBILITY);
    }
    Notification notification = builder.build();

    // Create the big view for the notification (viewed by pulling down)
    RemoteViews remoteViews = updateSummaryNotificationRemoteViews(pwPairs);
    notification.bigContentView = remoteViews;

    mNotificationManager.notify(SUMMARY_NOTIFICATION_ID, notification);
}

From source file:com.androzic.track.TrackDetails.java

private void updateTrackDetails() {
    AppCompatActivity activity = (AppCompatActivity) getActivity();
    Resources resources = getResources();

    activity.getSupportActionBar().setTitle(track.name);

    View view = getView();//from  w  w w .  j a  va 2 s .c om

    int pointCount = track.getPointCount();
    ((TextView) view.findViewById(R.id.point_count))
            .setText(resources.getQuantityString(R.plurals.numberOfPoints, pointCount, pointCount));

    String distance = StringFormatter.distanceH(track.distance);
    ((TextView) view.findViewById(R.id.distance)).setText(distance);

    Track.TrackPoint ftp = track.getPoint(0);
    Track.TrackPoint ltp = track.getLastPoint();

    String start_coords = StringFormatter.coordinates(" ", ftp.latitude, ftp.longitude);
    ((TextView) view.findViewById(R.id.start_coordinates)).setText(start_coords);
    String finish_coords = StringFormatter.coordinates(" ", ltp.latitude, ltp.longitude);
    ((TextView) view.findViewById(R.id.finish_coordinates)).setText(finish_coords);

    Date start_date = new Date(ftp.time);
    ((TextView) view.findViewById(R.id.start_date))
            .setText(DateFormat.getDateFormat(activity).format(start_date) + " "
                    + DateFormat.getTimeFormat(activity).format(start_date));
    Date finish_date = new Date(ltp.time);
    ((TextView) view.findViewById(R.id.finish_date))
            .setText(DateFormat.getDateFormat(activity).format(finish_date) + " "
                    + DateFormat.getTimeFormat(activity).format(finish_date));

    long elapsed = (ltp.time - ftp.time) / 1000;
    String timeSpan;
    if (elapsed < 24 * 60 * 60 * 3) {
        timeSpan = DateUtils.formatElapsedTime(elapsed);
    } else {
        timeSpan = DateUtils.formatDateRange(activity, ftp.time, ltp.time, DateUtils.FORMAT_ABBREV_MONTH);
    }
    ((TextView) view.findViewById(R.id.time_span)).setText(timeSpan);

    // Gather statistics
    int segmentCount = 0;
    double minElevation = Double.MAX_VALUE;
    double maxElevation = Double.MIN_VALUE;
    double maxSpeed = 0;

    MeanValue mv = new MeanValue();

    for (Track.TrackSegment segment : track.getSegments()) {
        Track.TrackPoint ptp = null;
        if (segment.independent)
            segmentCount++;

        for (Track.TrackPoint tp : segment.getPoints()) {
            if (ptp != null) {
                double d = Geo.distance(tp.latitude, tp.longitude, ptp.latitude, ptp.longitude);
                double speed = d / ((tp.time - ptp.time) / 1000);
                if (speed == Double.POSITIVE_INFINITY)
                    continue;
                mv.addValue(speed);
                if (speed > maxSpeed)
                    maxSpeed = speed;
            }
            ptp = tp;
            if (tp.elevation < minElevation && tp.elevation != 0)
                minElevation = tp.elevation;
            if (tp.elevation > maxElevation)
                maxElevation = tp.elevation;
        }
    }

    double averageSpeed = mv.getMeanValue();

    ((TextView) view.findViewById(R.id.segment_count))
            .setText(resources.getQuantityString(R.plurals.numberOfSegments, segmentCount, segmentCount));

    ((TextView) view.findViewById(R.id.max_elevation)).setText(StringFormatter.elevationH(maxElevation));
    ((TextView) view.findViewById(R.id.min_elevation)).setText(StringFormatter.elevationH(minElevation));

    ((TextView) view.findViewById(R.id.max_speed)).setText(String.format(Locale.getDefault(), "%s: %s",
            resources.getString(R.string.max_speed), StringFormatter.speedH(maxSpeed)));
    ((TextView) view.findViewById(R.id.average_speed)).setText(String.format(Locale.getDefault(), "%s: %s",
            resources.getString(R.string.average_speed), StringFormatter.speedH(averageSpeed)));
}

From source file:com.karma.konnect.PwoDiscoveryService.java

/**
 * Create or update the a single notification that is a collapsed version
 * of the top two beacon notifications.//from  w  w  w  . j  a va2  s.  c  o  m
 */
private void updateSummaryNotification(List<PwoMetadata> pwoMetadataList) {
    int numNearbyBeacons = pwoMetadataList.size();
    String contentTitle = String.valueOf(numNearbyBeacons);
    Resources resources = getResources();
    contentTitle += " "
            + resources.getQuantityString(R.plurals.numFoundBeacons, numNearbyBeacons, numNearbyBeacons);
    String contentText = getString(R.string.summary_notification_pull_down);
    PendingIntent pendingIntent = createReturnToAppPendingIntent();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.drawable.ic_notification).setContentTitle(contentTitle).setContentText(contentText)
            .setSmallIcon(R.drawable.ic_notification).setGroup(NOTIFICATION_GROUP_KEY).setGroupSummary(true)
            .setPriority(NOTIFICATION_PRIORITY).setContentIntent(pendingIntent);
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder.setVisibility(NOTIFICATION_VISIBILITY);
    }
    Notification notification = builder.build();

    // Create the big view for the notification (viewed by pulling down)
    RemoteViews remoteViews = updateSummaryNotificationRemoteViews(pwoMetadataList);
    notification.bigContentView = remoteViews;

    mNotificationManager.notify(SUMMARY_NOTIFICATION_ID, notification);
}

From source file:com.novoda.downloadmanager.lib.DownloadNotifier.java

/**
 * Return given duration in a human-friendly format. For example, "4
 * minutes" or "1 second". Returns only largest meaningful unit of time,
 * from seconds up to hours.//w  ww. j a  v a 2  s  . co m
 */
public CharSequence formatDuration(long millis) {
    final Resources res = mContext.getResources();
    if (millis >= DateUtils.HOUR_IN_MILLIS) {
        final int hours = (int) ((millis + 1800000) / DateUtils.HOUR_IN_MILLIS);
        return res.getQuantityString(R.plurals.dl__duration_hours, hours, hours);
    } else if (millis >= DateUtils.MINUTE_IN_MILLIS) {
        final int minutes = (int) ((millis + 30000) / DateUtils.MINUTE_IN_MILLIS);
        return res.getQuantityString(R.plurals.dl__duration_minutes, minutes, minutes);
    } else {
        final int seconds = (int) ((millis + 500) / DateUtils.SECOND_IN_MILLIS);
        return res.getQuantityString(R.plurals.dl__duration_seconds, seconds, seconds);
    }
}

From source file:org.onebusaway.android.ui.RegionsFragment.java

/**
 * Sets the text view that contains distance with units based on input parameters
 *
 * @param text     the TextView to be set
 * @param distance the distance to be used, in miles (for imperial) or kilometers (for metric)
 * @param units    the units to be used from strings.xml, either preferences_preferred_units_option_metric
 *                 or preferences_preferred_units_option_imperial
 *///from  w w w  .  j  a  v a2  s .  co m
private void setDistanceTextView(TextView text, double distance, String units) {
    Resources r = getResources();
    NumberFormat fmt = NumberFormat.getInstance();
    if (fmt instanceof DecimalFormat) {
        fmt.setMaximumFractionDigits(1);
    }

    if (units.equalsIgnoreCase(getString(R.string.preferences_preferred_units_option_imperial))) {
        text.setText(r.getQuantityString(R.plurals.distance_miles, (int) distance, fmt.format(distance)));
    } else if (units.equalsIgnoreCase(getString(R.string.preferences_preferred_units_option_metric))) {
        text.setText(r.getQuantityString(R.plurals.distance_kilometers, (int) distance, fmt.format(distance)));
    }
}

From source file:org.physical_web.physicalweb.UriBeaconDiscoveryService.java

/**
 * Create or update the a single notification that is a collapsed version
 * of the top two beacon notifications/*from   w  ww  .  j  a  va 2  s  . co  m*/
 */
private void updateSummaryNotification() {
    int numNearbyBeacons = mSortedDevices.size();
    String contentTitle = String.valueOf(numNearbyBeacons) + " ";
    Resources resources = getResources();
    contentTitle += " "
            + resources.getQuantityString(R.plurals.numFoundBeacons, numNearbyBeacons, numNearbyBeacons);
    String contentText = getString(R.string.summary_notification_pull_down);
    PendingIntent pendingIntent = createReturnToAppPendingIntent();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    Notification notification = builder.setSmallIcon(R.drawable.ic_notification).setContentTitle(contentTitle)
            .setContentText(contentText).setSmallIcon(R.drawable.ic_notification)
            .setGroup(NOTIFICATION_GROUP_KEY).setGroupSummary(true).setPriority(NOTIFICATION_PRIORITY)
            .setContentIntent(pendingIntent).build();

    // Create the big view for the notification (viewed by pulling down)
    RemoteViews remoteViews = updateSummaryNotificationRemoteViews();
    notification.bigContentView = remoteViews;

    mNotificationManager.notify(SUMMARY_NOTIFICATION_ID, notification);
}

From source file:org.physical_web.physicalweb.UrlDeviceDiscoveryService.java

/**
 * Create or update the a single notification that is a collapsed version
 * of the top two beacon notifications.// ww w  .  j  a v a2 s  . com
 */
private void updateSummaryNotification(List<PwPair> pwPairs) {
    int numNearbyBeacons = pwPairs.size();
    String contentTitle = String.valueOf(numNearbyBeacons);
    Resources resources = getResources();
    contentTitle += " "
            + resources.getQuantityString(R.plurals.numFoundBeacons, numNearbyBeacons, numNearbyBeacons);
    String contentText = getString(R.string.summary_notification_pull_down);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.drawable.ic_notification).setContentTitle(contentTitle).setContentText(contentText)
            .setSmallIcon(R.drawable.ic_notification).setGroup(NOTIFICATION_GROUP_KEY).setGroupSummary(true)
            .setPriority(Utils.containsFavorite(pwPairs) ? NotificationCompat.PRIORITY_DEFAULT
                    : NotificationCompat.PRIORITY_MIN)
            .setContentIntent(createReturnToAppPendingIntent());
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder.setVisibility(NOTIFICATION_VISIBILITY);
    }
    Notification notification = builder.build();

    // Create the big view for the notification (viewed by pulling down)
    RemoteViews remoteViews = updateSummaryNotificationRemoteViews(pwPairs);
    notification.bigContentView = remoteViews;

    mNotificationManager.notify(SUMMARY_NOTIFICATION_ID, notification);
}

From source file:org.totschnig.myexpenses.dialog.ContribDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Activity ctx = getActivity();/*from  w w w. j  a  v  a2  s .c o m*/
    Resources res = getResources();
    Context wrappedCtx = DialogUtils.wrapContext2(ctx);
    String featureList = Utils.getContribFeatureLabelsAsFormattedList(ctx, feature);
    String featureDescription;
    if (feature.hasTrial)
        featureDescription = getString(R.string.dialog_contrib_premium_feature,
                "<i>" + getString(res.getIdentifier("contrib_feature_" + feature + "_label", "string",
                        ctx.getPackageName())) + "</i>")
                + (usagesLeft > 0
                        ? res.getQuantityString(R.plurals.dialog_contrib_usage_count, usagesLeft, usagesLeft)
                        : getString(R.string.dialog_contrib_no_usages_left));
    else
        featureDescription = getString(res.getIdentifier("contrib_feature_" + feature + "_description",
                "string", ctx.getPackageName()));
    CharSequence message = Html.fromHtml((String) TextUtils.concat(featureDescription, " ",
            getString(R.string.dialog_contrib_reminder_remove_limitation), " ",
            getString(R.string.dialog_contrib_reminder_gain_access), "<br>", featureList));
    return new AlertDialog.Builder(wrappedCtx).setTitle(R.string.dialog_title_contrib_feature)
            .setMessage(message).setNegativeButton(R.string.dialog_contrib_no, this)
            .setPositiveButton(R.string.dialog_contrib_yes, this).create();
}

From source file:com.joulespersecond.seattlebusbot.RegionsFragment.java

/**
 * Sets the text view that contains distance with units based on input parameters
 *
 * @param text     the TextView to be set
 * @param distance the distance to be used, in miles (for imperial) or kilometers (for metric)
 * @param units    the units to be used from strings.xml, either preferences_preferred_units_option_metric
 *                 or preferences_preferred_units_option_imperial
 */// ww  w .  j av a2  s.co m
private void setDistanceTextView(TextView text, double distance, String units) {
    Resources r = getResources();
    NumberFormat fmt = NumberFormat.getInstance();
    if (fmt instanceof DecimalFormat) {
        ((DecimalFormat) fmt).setMaximumFractionDigits(1);
    }

    if (units.equalsIgnoreCase(getString(R.string.preferences_preferred_units_option_imperial))) {
        text.setText(
                r.getQuantityString(R.plurals.region_distance_miles, (int) distance, fmt.format(distance)));
    } else if (units.equalsIgnoreCase(getString(R.string.preferences_preferred_units_option_metric))) {
        text.setText(r.getQuantityString(R.plurals.region_distance_kilometers, (int) distance,
                fmt.format(distance)));
    }
}