Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Arslan Asif
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Sangel Kapoor
Ranch Hand
Posts: 149
1
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just curious to know when should we ideally use assets folder ???
 
Ragavan Raju
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this http://www.compiletimeerror.com/2013/01/how-to-set-background-image-in-android.html for your problem, may help...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic