How to Set an image as a background for whole activities in an android application? ( I know how to set it for each activity. I want to know is there any way to write one line of code that can affect all activities?)
Asked
Active
Viewed 8,701 times
4 Answers
2
You can do this:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowBackground">@drawable/PICTURE_NAME</item>
</style>

guipivoto
- 18,327
- 9
- 60
- 75
1
define it in your theme in style.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:background">@drawable/</item>
</style>

Shantanu
- 692
- 5
- 20
0
Put your image file .png in the drawables file and do this in all your xml layouts for all your activities
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:clipToPadding="false"
android:paddingBottom="4dp"
android:background="@drawable/yourImage"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>

John Sardinha
- 3,566
- 6
- 25
- 55
-
Thanks, I already knew about that. I wanted to find a way to not to need write it in each activity. – Maedeh HM Jul 25 '16 at 17:10
-
This is the right way to do it, doing it like the other answers suggest it can cause issues, like when the soft keyboard opens it shows the image behind it – John Sardinha Jul 25 '16 at 17:13
0
Well, depending on how many activities you have, you would need to set background attribute to each of the activities' root element like this:
<LinearLayout
...
...
android:background="@drawable/background_image"
</LinearLayout>
I have used LinearLayout as an example above. You should replace it with whatever is your root element in your activity layout file!
I hope this helps!

Eenvincible
- 5,641
- 2
- 27
- 46