Java Code Examples for androidx.core.content.res.ResourcesCompat#getDrawable()

The following examples show how to use androidx.core.content.res.ResourcesCompat#getDrawable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NavigationMenuItemView.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@Override
public void setIcon(@Nullable Drawable icon) {
  if (icon != null) {
    if (hasIconTintList) {
      Drawable.ConstantState state = icon.getConstantState();
      icon = DrawableCompat.wrap(state == null ? icon : state.newDrawable()).mutate();
      DrawableCompat.setTintList(icon, iconTintList);
    }
    icon.setBounds(0, 0, iconSize, iconSize);
  } else if (needsEmptyIcon) {
    if (emptyDrawable == null) {
      emptyDrawable =
          ResourcesCompat.getDrawable(
              getResources(), R.drawable.navigation_empty_icon, getContext().getTheme());
      if (emptyDrawable != null) {
        emptyDrawable.setBounds(0, 0, iconSize, iconSize);
      }
    }
    icon = emptyDrawable;
  }
  TextViewCompat.setCompoundDrawablesRelative(textView, icon, null, null, null);
}
 
Example 2
Source File: AvatarService.java    From Pix-Art-Messenger with GNU General Public License v3.0 6 votes vote down vote up
private static Bitmap getRoundLauncherIcon(Resources resources) {

        final Drawable drawable = ResourcesCompat.getDrawable(resources, R.drawable.ic_launcher, null);
        if (drawable == null) {
            return null;
        }

        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }
 
Example 3
Source File: NotificationData.java    From Status with Apache License 2.0 6 votes vote down vote up
@Nullable
private Drawable getDrawable(Context context, int resource, String packageName) {
    if (packageName == null) return null;

    Resources resources = null;
    PackageInfo packageInfo = null;

    try {
        resources = context.getPackageManager().getResourcesForApplication(packageName);
        packageInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_META_DATA);
    } catch (PackageManager.NameNotFoundException ignored) {
    }

    if (resources == null || packageInfo == null) return null;

    Resources.Theme theme = resources.newTheme();
    theme.applyStyle(packageInfo.applicationInfo.theme, false);

    try {
        return ResourcesCompat.getDrawable(resources, resource, theme);
    } catch (Resources.NotFoundException | OutOfMemoryError e) {
        return null;
    }
}
 
Example 4
Source File: ActionData.java    From Status with Apache License 2.0 6 votes vote down vote up
@Nullable
public Drawable getIcon(Context context) {
    Resources resources = null;
    PackageInfo packageInfo = null;

    try {
        resources = context.getPackageManager().getResourcesForApplication(packageName);
        packageInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_META_DATA);
    } catch (PackageManager.NameNotFoundException ignored) {
    }

    if (resources == null || packageInfo == null) return null;

    Resources.Theme theme = resources.newTheme();
    theme.applyStyle(packageInfo.applicationInfo.theme, false);

    try {
        return ResourcesCompat.getDrawable(resources, getIcon(), theme);
    } catch (Resources.NotFoundException e) {
        return null;
    }
}
 
Example 5
Source File: MarkerDemoActivity.java    From android-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Demonstrates converting a {@link Drawable} to a {@link BitmapDescriptor},
 * for use as a marker icon.
 */
private BitmapDescriptor vectorToBitmap(@DrawableRes int id, @ColorInt int color) {
    Drawable vectorDrawable = ResourcesCompat.getDrawable(getResources(), id, null);
    Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
            vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    DrawableCompat.setTint(vectorDrawable, color);
    vectorDrawable.draw(canvas);
    return BitmapDescriptorFactory.fromBitmap(bitmap);
}
 
Example 6
Source File: ActionsModule.java    From tv-samples with Apache License 2.0 5 votes vote down vote up
@PerFragment
@RentActionQualifier
@Provides
Action provideRentAction() {

  return new Action(ACTION_RENT,
      SampleApplication.getInstance().getString(R.string.livedata_actoin_rent),
      SampleApplication.getInstance().getString(R.string.livedata_rent_price), ResourcesCompat
      .getDrawable(
          SampleApplication.getInstance().getResources(),
          R.drawable.ic_favorite_border_white_24dp,
          SampleApplication.getInstance().getTheme()));
}
 
Example 7
Source File: RouteDirectionsFragment.java    From nearby-android with Apache License 2.0 5 votes vote down vote up
private Drawable getRoutingIcon(final DirectionManeuverType maneuver) {

      try {
        Integer id = getResourceIdForManeuverType(maneuver);
        return ResourcesCompat.getDrawable(getActivity().getResources(),id,null);
      } catch (final Resources.NotFoundException e) {
        Log.w(RouteDirectionsFragment.TAG, "No drawable found for" + maneuver.name());
        return null;
      }
    }
 
Example 8
Source File: CategoryHelper.java    From nearby-android with Apache License 2.0 5 votes vote down vote up
/**
 * Return appropriate drawable base on place type
 * @param p - Place item
 * @param a - Activity
 * @return - Drawable
 */
public static Drawable getDrawableForPlace(final Place p, final Activity a){

  final String placeType = p.getType();
  final String category = CategoryHelper.getCategoryForFoodType(placeType);
  final Drawable d;
  switch (category){
    case "Pizza":
      d = ResourcesCompat.getDrawable(a.getResources(), R.drawable.ic_local_pizza_black_24dp,null);
      break;
    case "Hotel":
      d = ResourcesCompat.getDrawable(a.getResources(), R.drawable.ic_hotel_black_24dp,null);
      break;
    case "Food":
      d = ResourcesCompat.getDrawable(a.getResources(), R.drawable.ic_local_dining_black_24dp,null);
      break;
    case "Bar or Pub":
      d = ResourcesCompat.getDrawable(a.getResources(), R.drawable.ic_local_bar_black_24dp,null);
      break;
    case "Coffee Shop":
      d = ResourcesCompat.getDrawable(a.getResources(), R.drawable.ic_local_cafe_black_24dp,null);
      break;
    default:
      d = ResourcesCompat.getDrawable(a.getResources(), R.drawable.ic_place_black_24dp,null);
  }
  return d;
}
 
Example 9
Source File: ChronusResourceProvider.java    From GeometricWeather with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Nullable
private Drawable getDrawable(@NonNull String resName) {
    try {
        return ResourcesCompat.getDrawable(
                context.getResources(),
                ResourceUtils.nonNull(getResId(context, resName, "drawable")),
                null
        );
    } catch (Exception e) {
        return null;
    }
}
 
Example 10
Source File: DefaultResourceProvider.java    From GeometricWeather with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Nullable
private Drawable getDrawable(@NonNull String resName) {
    try {
        return ResourcesCompat.getDrawable(
                context.getResources(),
                ResourceUtils.nonNull(getResId(context, resName, "drawable")),
                null
        );
    } catch (Exception e) {
        return null;
    }
}
 
Example 11
Source File: MarkerDemoActivity.java    From android-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Demonstrates converting a {@link Drawable} to a {@link BitmapDescriptor},
 * for use as a marker icon.
 */
private BitmapDescriptor vectorToBitmap(@DrawableRes int id, @ColorInt int color) {
    Drawable vectorDrawable = ResourcesCompat.getDrawable(getResources(), id, null);
    Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
            vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    DrawableCompat.setTint(vectorDrawable, color);
    vectorDrawable.draw(canvas);
    return BitmapDescriptorFactory.fromBitmap(bitmap);
}
 
Example 12
Source File: AlbumItemViewModel.java    From Jockey with Apache License 2.0 5 votes vote down vote up
public void setAlbum(Album album) {
    mAlbum = album;

    mArtistImage = new ObservableField<>();
    mTitleTextColor = new ObservableInt();
    mArtistTextColor = new ObservableInt();
    mBackgroundColor = new ObservableInt();

    defaultColors();

    if (mAlbum.getArtUri() != null) {
        int imageSize = mContext.getResources().getDimensionPixelSize(R.dimen.grid_width);

        Glide.with(mContext)
                .load(new File(mAlbum.getArtUri()))
                .placeholder(R.drawable.art_default)
                .error(R.drawable.art_default)
                .listener(new PaletteListener(mTitleTextColor, mArtistTextColor,
                        mBackgroundColor))
                .into(new ObservableTarget(imageSize, mArtistImage));
    } else {
        Drawable fallback = ResourcesCompat.getDrawable(mContext.getResources(),
                R.drawable.art_default, mContext.getTheme());

        mArtistImage.set(fallback);
    }

    notifyChange();
}
 
Example 13
Source File: PinView.java    From PinView with Apache License 2.0 5 votes vote down vote up
/**
 * Set the item background to a given resource. The resource should refer to
 * a Drawable object or 0 to remove the item background.
 *
 * @param resId The identifier of the resource.
 * @attr ref R.styleable#PinView_android_itemBackground
 */
public void setItemBackgroundResources(@DrawableRes int resId) {
    if (resId != 0 && mItemBackgroundResource != resId) {
        return;
    }
    mItemBackground = ResourcesCompat.getDrawable(getResources(), resId, getContext().getTheme());
    setItemBackground(mItemBackground);
    mItemBackgroundResource = resId;
}
 
Example 14
Source File: Clock.java    From Clock-view with Apache License 2.0 5 votes vote down vote up
public void setStopwatchTheme(StopwatchTheme stopwatchTheme) {

        this.clockType = stopwatchTheme.getClockType();

        this.clockBackground = ResourcesCompat.getDrawable(getContext().getResources(), stopwatchTheme.getClockBackground(), null);

        this.valuesFont = ResourcesCompat.getFont(getContext(), stopwatchTheme.getValuesFont());
        this.valuesColor = ResourcesCompat.getColor(getContext().getResources(), stopwatchTheme.getValuesColor(), null);

        this.showBorder = stopwatchTheme.isShowBorder();
        this.borderColor = stopwatchTheme.getBorderColor();
        this.borderRadiusRx = stopwatchTheme.getBorderRadiusRx();
        this.borderRadiusRy = stopwatchTheme.getBorderRadiusRy();
    }
 
Example 15
Source File: BottomDialog.java    From ImageSaveandShare with Apache License 2.0 4 votes vote down vote up
public Builder setIcon(@DrawableRes int iconRes) {
    this.icon = ResourcesCompat.getDrawable(context.getResources(), iconRes, null);
    return this;
}
 
Example 16
Source File: Clock.java    From Clock-view with Apache License 2.0 4 votes vote down vote up
public void setNumericTheme(NumericTheme numericTheme) {

        this.clockType = numericTheme.getClockType();

        this.clockBackground = ResourcesCompat.getDrawable(getContext().getResources(), numericTheme.getClockBackground(), null);

        this.valuesFont = ResourcesCompat.getFont(getContext(), numericTheme.getValuesFont());
        this.valuesColor = ResourcesCompat.getColor(getContext().getResources(), numericTheme.getValuesColor(), null);

        this.showBorder = numericTheme.isShowBorder();
        this.borderColor = numericTheme.getBorderColor();
        this.borderRadiusRx = numericTheme.getBorderRadiusRx();
        this.borderRadiusRy = numericTheme.getBorderRadiusRy();

        this.numericFormat = numericTheme.getNumericFormat();
    }
 
Example 17
Source File: Clock.java    From Clock-view with Apache License 2.0 4 votes vote down vote up
public void setAnalogicalTheme(AnalogicalTheme analogicalTheme) {

        this.clockType = analogicalTheme.getClockType();

        this.clockBackground = ResourcesCompat.getDrawable(getContext().getResources(), analogicalTheme.getClockBackground(), null);

        this.showCenter = analogicalTheme.isShowCenter();
        this.centerInnerColor = analogicalTheme.getCenterInnerColor();
        this.centerOuterColor = analogicalTheme.getCenterOuterColor();

        this.showBorder = analogicalTheme.isShowBorder();
        this.borderColor = analogicalTheme.getBorderColor();

        this.showSecondsNeedle = analogicalTheme.isShowSecondsNeedle();
        this.needleHoursColor = analogicalTheme.getNeedleHoursColor();
        this.needleMinutesColor = analogicalTheme.getNeedleMinutesColor();
        this.needleSecondsColor = analogicalTheme.getNeedleSecondsColor();

        this.showProgress = analogicalTheme.isShowProgress();
        this.progressColor = analogicalTheme.getProgressColor();
        this.showMinutesProgress = analogicalTheme.isShowMinutesProgress();
        this.minutesProgressColor = analogicalTheme.getMinutesProgressColor();
        this.minutesProgressFactor = analogicalTheme.getMinutesProgressFactor();
        this.showSecondsProgress = analogicalTheme.isShowSecondsProgress();
        this.secondsProgressColor = analogicalTheme.getSecondsProgressColor();
        this.secondsProgressFactor = analogicalTheme.getSecondsProgressFactor();

        this.showDegrees = analogicalTheme.isShowDegrees();
        this.degreesColor = ResourcesCompat.getColor(getContext().getResources(), analogicalTheme.getDegreesColor(), null);
        this.degreesType = analogicalTheme.getDegreesType();
        this.degreesStep = analogicalTheme.getDegreesStep();

        this.valuesFont = ResourcesCompat.getFont(getContext(), analogicalTheme.getValuesFont());
        this.valuesColor = ResourcesCompat.getColor(getContext().getResources(), analogicalTheme.getValuesColor(), null);
        this.showHoursValues = analogicalTheme.isShowHoursValues();
        this.showMinutesValues = analogicalTheme.isShowMinutesValues();
        this.minutesValuesFactor = analogicalTheme.getMinutesValuesFactor();
        this.valueStep = analogicalTheme.getValueStep();
        this.valueType = analogicalTheme.getValueType();
        this.valueDisposition = analogicalTheme.getValueDisposition();
    }
 
Example 18
Source File: Clock.java    From Clock-view with Apache License 2.0 4 votes vote down vote up
public void setClockBackground(int clockBackground) {
    this.clockBackground = ResourcesCompat.getDrawable(getContext().getResources(), clockBackground, null);
}
 
Example 19
Source File: Global.java    From NClientV2 with Apache License 2.0 4 votes vote down vote up
public static Drawable getLogo(Resources resources){
    return ResourcesCompat.getDrawable(resources,getLogo(),null);
}
 
Example 20
Source File: Clock.java    From Clock-view with Apache License 2.0 3 votes vote down vote up
public void setTimeCounterTheme(TimeCounterTheme timeCounterTheme) {

        this.clockType = timeCounterTheme.getClockType();

        this.clockBackground = ResourcesCompat.getDrawable(getContext().getResources(), timeCounterTheme.getClockBackground(), null);

        this.valuesFont = ResourcesCompat.getFont(getContext(), timeCounterTheme.getValuesFont());
        this.valuesColor = ResourcesCompat.getColor(getContext().getResources(), timeCounterTheme.getValuesColor(), null);

        this.showProgress = timeCounterTheme.isShowProgress();
        this.progressColor = timeCounterTheme.getProgressColor();
        this.borderColor = timeCounterTheme.getBorderColor();
    }