How to reference style attributes from a drawable?

In my experience it is not possible to reference an attribute in an XML drawable. In order to make your theme you need to: Create one XML drawable per theme. Include the needed color into you drawable directly with the @color tag or #RGB format. Make an attribute for your drawable in attrs.xml. <?xml version=”1.0″ … Read more

Open-sided Android stroke?

I achieved a good solution with this one: <?xml version=”1.0″ encoding=”utf-8″?> <layer-list xmlns:android=”http://schemas.android.com/apk/res/android”> <!– This is the line –> <item android:top=”-1dp” android:right=”-1dp” android:left=”-1dp”> <shape> <solid android:color=”@android:color/transparent” /> <stroke android:width=”1dp” android:color=”#ffffff” /> </shape> </item> </layer-list> This works well in case you need a transparent background but still an open stroke color (In my case I only … Read more

how to change a drawableLeft icon size on a button?

Wrap your resource in a drawable that defines your desired size similar to: <?xml version=”1.0″ encoding=”utf-8″?> <layer-list xmlns:android=”http://schemas.android.com/apk/res/android” > <item android:drawable=”@drawable/icon” android:width=”@dimen/icon_size” android:height=”@dimen/icon_size” /> </layer-list > After that, use this drawable in your android:drawableLeft tag

How to center vector drawable in layer-list without scaling

I ran into the same problem trying to center vectors drawables on a layered list. I have a workaround, its not exactly the same but it works, you need to set a size for the entire drawable and add padding to the vector item: <?xml version=”1.0″ encoding=”utf-8″?> <layer-list xmlns:android=”http://schemas.android.com/apk/res/android”> <item> <shape> <size android:height=”120dp” android:width=”120dp”/> <solid … Read more

Progress bar with rounded corners?

How to make the progress shape a bit smaller? You need to give the progress item a little padding, like so: <item android:id=”@android:id/progress” android:top=”2dp” android:bottom=”2dp” android:left=”2dp” android:right=”2dp”> How to make the progress bar to be curved as per design? Replace the <clip></clip> element, with <scale android:scaleWidth=”100%”></scale>. That will make the shape keep its form as … Read more

Style bottom Line in Android

It’s kind of a hack, but I think this is probably the best way to do it. The dashed line will always be on the bottom, regardless of the height. <layer-list xmlns:android=”http://schemas.android.com/apk/res/android”> <item> <shape android:shape=”rectangle” > <solid android:color=”#1bd4f6″ /> </shape> </item> <item android:top=”-2dp” android:right=”-2dp” android:left=”-2dp”> <shape> <solid android:color=”@android:color/transparent” /> <stroke android:dashGap=”10px” android:dashWidth=”10px” android:width=”1dp” android:color=”#ababb2″ /> … Read more

How to fix: android.app.RemoteServiceException: Bad notification posted from package *: Couldn’t create icon: StatusBarIcon

What was happening was, I was including the integer reference to the icon in the PendingIntent bundle, and that integer was later being referenced while being posted to the NotificationManager. In between getting the integer reference and the pending intent going off, the app was updated and all of the drawable references changed. The integer … Read more

Storing R.drawable IDs in XML array

You use a typed array in arrays.xml file within your /res/values folder that looks like this: <?xml version=”1.0″ encoding=”utf-8″?> <resources> <integer-array name=”random_imgs”> <item>@drawable/car_01</item> <item>@drawable/balloon_random_02</item> <item>@drawable/dog_03</item> </integer-array> </resources> Then in your activity, access them like so: TypedArray imgs = getResources().obtainTypedArray(R.array.random_imgs); // get resource ID by index, use 0 as default to set null resource imgs.getResourceId(i, 0) … Read more

Getting Bitmap from vector drawable

Checked on API: 17, 21, 23 public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) { Drawable drawable = ContextCompat.getDrawable(context, drawableId); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { drawable = (DrawableCompat.wrap(drawable)).mutate(); } 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; } UPDATE: Project gradle: dependencies { classpath ‘com.android.tools.build:gradle:2.2.0-alpha5’ } … Read more