How do i set background image in Linear Layout so that it could fit every devices and what is the resolution for that image?
-
for different devices you have to put your assets in drawable-hdpi, mdpi,ldpi,xhdpi. – Pradeep Kumar May 15 '14 at 18:38
-
Try to set this android:background:"@drawable/imagename" properties to your parent layout and put all size background various drawable size for this look my ans here which can explain you to what size required for various drawable http://stackoverflow.com/questions/19875158/android-background-image-size-in-pixel/19875228#19875228 – Haresh Chhelana May 16 '14 at 04:04
5 Answers
If you want to set through xml, then you need to do as below:
android:background="@android:color/white"
in case if you decide to use android's default color code or if you have colors specified in colors.xml, then use
android:background="@colors/white"
If you want to do programmatically, then do:
linearlayout.setBackgroundColor(Color.WHITE);
and if you want to set an image
android:background="@drawable/image"

- 14,726
- 4
- 45
- 70
-
and what about supporting it in all devices? and the backgrpund res? – Yogesh Ojha May 15 '14 at 18:38
-
"supporting it all devices" is up to you to ensure you are targeting the correct versions and use the correct support libraries. – RichieHH Jul 19 '14 at 12:18
Take this repeatable drawable app_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/your_image"
android:tileMode="repeat" />
And use it like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/app_background"/>
And if you want to scale your_image to all screen resolutions, define it as a 9-patch image: http://developer.android.com/tools/help/draw9patch.html

- 708
- 8
- 17
Use this link for supporting different screen sizes. You will have to make same image with different resolutions
And to set the image as background,
usage:
android:background:"@drawable/background"

- 801
- 1
- 15
- 28

- 2,857
- 1
- 23
- 38
You can keep your image in res/drawable folders or create a folder inside res main folder which is name drawable-nodpi which is support all dpi device. and also you can follow Simple Nine-patch Generator
then set background image thus. such as
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher">
Best of luck!

- 629
- 1
- 7
- 26
For using a background from drawable folder,
android:background="@drawable/background" >
prepare 4 images and place them in their respective folders.
xhdpi (xlarge screens) 960dp x 720dp
hdpi (large screens) 640dp x 480dp
mdpi (normal screens) 470dp x 320dp
ldpi (small screens) 426dp x 320dp

- 801
- 1
- 15
- 28