using theme or ImageView ?
7 Answers
use the android:background
attribute in your xml. Easiest way if you want to apply it to a whole activity is to put it in the root of your layout. So if you have a RelativeLayout as the start of your xml, put it in here:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootRL"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background">
</RelativeLayout>
-
7This can, unfortunately, cause some problems...sure it will work for your main activity, but if you open a new activity with the same android:background="@drawable/background", as soon as you rotate the emulator in most cases, and some devices, you will crash with an "Out of Memory.." error if your bgImg is a high resolution. @Sephy, how would you handle this issue? – whyoz Apr 11 '13 at 23:45
You can set the "background image" to an activity by setting android:background
xml attributes as followings:
(Here, for example, Take a LinearLayout for an activity and setting a background image for the layout(i.e. indirectly to an activity))
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/icon">
</LinearLayout>

- 127,700
- 71
- 241
- 295
-
1I've noticed that doing this works fine for a LinearLayout, but for RelativeLayouts, I get OOM errors. I've read plenty of decodeBitmap posts or layout.invalidate() posts, or Bitmap.recycle posts, etc...How would you persist an image as the background of the app using RelativeLayouts? Should I be caching any image that will appear in more than one activity? What is the best practice? – whyoz Apr 11 '13 at 23:50
Place the Image in the folder drawable. drawable folder is in res. drawable have 5 variants drawable-hdpi drawable-ldpi drawable-mdpi drawable-xhdpi drawable-xxhdpi

- 491
- 1
- 5
- 15
We can easily place the background image in PercentFrameLayout using the ImageView. We have to set the scaleType attribute value="fitXY" and in the foreground we can also display other view's like textview or button.
<android.support.percent.PercentFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<ImageView
android:src="@drawable/logo"
android:id="@+id/im1"
android:scaleType="fitXY"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<EditText android:layout_gravity="center_horizontal"
android:hint="Enter Username"
android:id="@+id/et1"
android:layout_height="wrap_content"
app:layout_widthPercent="50%"
app:layout_marginTopPercent="30%"
/>
<Button
android:layout_gravity="center_horizontal"
android:text="Login"
android:id="@+id/b1"
android:layout_height="wrap_content"
app:layout_widthPercent="50%"
app:layout_marginTopPercent="40%"/>
</android.support.percent.PercentFrameLayout>

- 21
- 1
ez way : copy your picture in this location: C:\Users\Your user name\AndroidStudioProjects\yourProjectName\app\src\main\res\drawable
and write this code in your xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background">
</RelativeLayout>
in line 7 write your file name instand of background
and Tamam...

- 155
- 10
Nowadays we have to use match_parent
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background">
</RelativeLayout>

- 124,308
- 23
- 334
- 268
and dont forget to clean your project after writing these lines you`ll a get an error in your xml file until you´ve cleaned your project in eclipse: Project->Clean...

- 1,104
- 1
- 9
- 23