happydude
happydude

Reputation: 3889

Is it possible to create a custom class that utilizes API-specific XML tags?

I am attempting to create a drawable in xml using a Drawable subclass (in this case LayerDrawable) to use as a background in a View, but the drawable does not calculate and display its layers the way I had intended. With help from this forum I have found the particular lines of source code that are causing my issue and feel I can very easily create a subclass or change the code or to do what I want.

My question is, assuming I make a new class with no unintended side-effects and I will never need access the old functionality, is it possible to use the new Drawable with the original Drawable's xml tag structure (ex. layer-list) to create my updated drawable in the same way?

For example, replace:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/resource_name"
        android:drawable=drawable/drawable_resource" />

</layer-list>

with this:

<?xml version="1.0" encoding="utf-8"?>
<package.MyLayerDrawable xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/resource_name"
        android:drawable=drawable/drawable_resource" />

</package.MyLayerDrawable>

Upvotes: 2

Views: 310

Answers (2)

Maurice Lam
Maurice Lam

Reputation: 354

Custom drawables are not allowed from XML, mostly for security reasons. XML drawables can be loaded as resources by other processes (including Launcher or the system process) and it would be a terrible idea to run random 3rd party code in these processes.

Very unfortunately, instead of properly sandboxing custom drawables they decided to completely disallow it.

http://groups.google.com/group/android-developers/browse_thread/thread/825a5d8b401d3332

Upvotes: 3

ademar111190
ademar111190

Reputation: 14505

like this? :

 package your.packepage;

 public class RemoteImageView extends ImageView {
     ....
 }

 <your.packpage.RemoteImageView 
 android:id="@+id/img_id" 
 android:layout_width="10dp" 
 android:layout_height="fill_parent" 
 android:src="@drawable/ic_launcher" />

Upvotes: 0

Related Questions