How To Set Background Image in Android Application


           It's quite simple to set background image for your android application. Below I have an example code for this as well. Now, just follow these steps..

1. Create a new android application in Eclipse. (click here if you need some help)


2. Copy your image file to /res/drawable directory

  
3. Open your  layout xml file activity_main.xml and add below lines
Eclipse Project directory structure

android:background="@drawable/bg"
 Here 'bg' is the name of the image without extension

So final layout file will look like :



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"/>
</RelativeLayout>


4. Run your application. The image will be applied as background for the layout.



 




Reactions:

27 comments :

  1. Nice tutorial it helped a lot!

    ReplyDelete
  2. Replies
    1. I used an image of size 1024 × 768px and 15.5 KB png file

      Delete
  3. how to change background from MainActivity.java?
    BSearch.setBackground("@drawable/red"); - dont work ((

    ReplyDelete
  4. already know))
    BSearch.setBackgroundResource(R.drawable.red);

    ReplyDelete
  5. Which size should be used for background image normally?

    ReplyDelete
  6. Wow thanks! Probably the easiest thing I've done for android!

    ReplyDelete
  7. Thanks to you this code used useful to me.

    ReplyDelete
  8. Thanks man! very helpful!

    ReplyDelete
  9. thanks.. it works for me.... (y) :)

    ReplyDelete
  10. Can I use 2 background for horizontal and vertical orientation? How will I type this code?

    ReplyDelete
  11. A valuable information here.Thanks for sharing with us.

    ReplyDelete
  12. i have a probel in :
    tools:context=""......
    it said that namespace tool is not bound

    ReplyDelete
  13. I am getting this error:
    error: Error: No resource found that matches the given name (at 'background' with value '@drawable/background_image')

    ReplyDelete
    Replies
    1. in place of background_image can u give your exact "image name"

      Delete
  14. Cheers fella! I've been trying to do this for a long time

    ReplyDelete
  15. Easy and cool. Thank you for sharing.
    BR

    ReplyDelete
  16. wow!! way to easy....
    i've been tinkering (making an app for a friends business) and i think i need to read 1000+ tutorials...
    thanks so much....

    ReplyDelete
  17. i want to set the same image for all the xml files. . . please help!!

    ReplyDelete
  18. i already tried that way. but it's not working. @ sign is not accepted by editor

    ReplyDelete