aspose file tools *
The moose likes Android and the fly likes How to add a background image in main.xml in anyother directory structure except @drawable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "How to add a background image in main.xml in anyother directory structure except @drawable" Watch "How to add a background image in main.xml in anyother directory structure except @drawable" New topic
Author

How to add a background image in main.xml in anyother directory structure except @drawable

Arslan Asif
Greenhorn

Joined: Jun 12, 2010
Posts: 8
Hello
I am trying to use this but it is giving error

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="/assets/images/mybackgrounds/bg.png"

I want to know how can we give background image at a particular location in layout file like main.xml
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

Welcome to JavaRanch Asif!

If you have your image resource in drawable folder you can refer them as follows:
android:background="@drawable/resource_name".

If you bundle them in assets, then you will have to read these files as streams using AssetManager and then create a Bitmap from these stream using BitmapFactory. After this is done, you can set this Bitmap as background property of your View or ViewGroup in code.

Putting an image resource in drawable folder is NOT JUST copying an image file to a folder in the Project's structure.(Read official documentation on Application Resources). Android tools(aapt et al.) do a lot of stuff behind the scenes.


[List of FAQs] | [Android FAQ] | [Samuh Varta]
Sangel Kapoor
Ranch Hand

Joined: Jan 12, 2013
Posts: 92

Just curious to know when should we ideally use assets folder ???
Ragavan Raju
Greenhorn

Joined: Dec 28, 2012
Posts: 10
Have a look at this http://www.compiletimeerror.com/2013/01/how-to-set-background-image-in-android.html for your problem, may help...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to add a background image in main.xml in anyother directory structure except @drawable