luthier
luthier

Reputation: 2861

Create a custom icon from a drawable or layout in Android

I'm trying to create custom shortcut icons that, via Intents, call an activity inside my app. The problem is that I want to create them either by modifying one of my app's drawables or (preferably) one of my layouts.

For the drawables, I have tried this

Drawable iconDrawable = getResources().getDrawable(iconResource);                       
iconDrawable.mutate();                      
iconDrawable.setColorFilter(0xff00ff00, PorterDuff.Mode.SRC_ATOP);                      
BitmapDrawable bd = (BitmapDrawable) iconDrawable;
bd.mutate();
bd.setColorFilter(0xff00ff00, PorterDuff.Mode.SRC_ATOP);
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bd.getBitmap());

but it doesn't work, the drawable is shown as-is, without modifying its color.

And as for creating the icon from a custom layout, which is what I really want to do... I simply don't know where to begin.

Could anyone please tell me how to do it or at least point me in the right direction?

Thanks!

Upvotes: 0

Views: 2610

Answers (1)

luthier
luthier

Reputation: 2861

Okay, I found the answer by studying the AnyCut source code, and the "trick" is to use a Canvas:

http://code.google.com/p/apps-for-android/source/browse/AnyCut/src/com/example/anycut/CreateShortcutActivity.java

(more precisely, the generatePhoneNumberIcon function).

Upvotes: 1

Related Questions