choco
choco

Reputation: 255

load images from assets through XML

I use in my application more than 150 images, and 30 icons ! so I find it a little bit messy to put those images all together in Drawable folder, and as it is impossible to have sub-folders in drawable , I am trying to load my images from the Assets folder. So to load images from assets programmatically its OK.

MY QUESTION is how to access Assets through XML?!

with drawable we use this :

  <ImageView    
    android:id="@+id/img"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:src=" **@drawable/ic_rate** "
      />

So what is the equivalent using assets.

Upvotes: 1

Views: 4059

Answers (2)

Onur
Onur

Reputation: 5625

Despite the fact that you should use drawables, you can define your own asset_drawable attribute as string than override the ImageViewto look for that atttibute and load the bitmap from assets. Your xml will be look like this

 <com.foo.bar.AssetImageView    
     android:id="@+id/img"
     android:layout_width="25dp"
     android:layout_height="25dp"
     app:asset_drawable="some/path/asset"
  />

Upvotes: 3

aleph_null
aleph_null

Reputation: 5786

You don't. An ImageView expects a drawable. Not all assets are drawables.

Drawables let you use images of different screen densities. Just use those. If you're struggling to stay organized, perhaps you could try using prefixes (e.g., prefix icons with ic). Another option would be to create a library project exclusively for icons, though it may be overkill.

Upvotes: 2

Related Questions