3

i want to set same background image to all my app layouts. My app supports all devices from mini phone to 10.1 tablets.

Is there one way to do this or i need to set for every layout re-sized image that quality would be good.

I found this in one app: app_background.xml in drawable folder:

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffff00" />
</shape>   

Also maybe some one could give me good explanation how to separate design and functions in java.

Thanks.

1
  • this will add background color not image Commented Feb 17, 2012 at 7:21

2 Answers 2

13

Use styles to do this.
1. Create a appropriate drawable image for background
2. Create a values/styles.xml file with following content

<style name="AppTheme" parent="android:style/Theme.Black">
      <item name="android:background">@drawable/main_app_bg</item>
   </style>


3. Use theme for whole app in AndroidManifest.xml

<application android:icon="@drawable/icon"
                 android:label="@string/app_name"
                 android:theme="@style/AppTheme">
Sign up to request clarification or add additional context in comments.

1 Comment

Don't do this unless you want every view in the app to reuse this background. If your background is a gradient this will look very strange. Using windowBackground instead of background is better.
1

Well, you can configure a single image to be auto-scaled for each device's screen size, it explains how here:

http://developer.android.com/guide/practices/screens_support.html

The issue with that is that if you want good aspect, you have to make your original image pretty big (as big as the biggest resolution you expect you app to be used on). Then, when scaled down for smaller screens, that big picture will still use up more memory then needed.

In reality there're not so many different resolutions out there, maybe 7-8 major ones. You should make individual background images of sizes specific to each target resolution, then, in your Action's onCreate(...) method you can do something like this to get the curren't device screen resolution:

Display display = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();

and load the proper background for it (adjusting for eventual title and status bar sizes)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.