Sunteți pe pagina 1din 9

sign up

Stack Overflow
Questions Tags Users Badges Unanswered Ask

Read this post in our app!

74

Full screen background image in an activity


android

background

imageview

framelayout

I see many applications that use a full-screen image as background. This is an example:

log in

I want to use this in a project, the best way I've found so far to do this is to use an image with a large size, put it in a ImageView and use android:
adjustViewBounds="true" to adjust the margins
The problem is that if a screen with a very high resolution, the image falls short.
Another option I thought of is to use the image in a FrameLayout, with match_parent in width and height as background... this stretches the image, but I think
the result is not very good.
I can ask how would you do it?
Thanks in advance Regrads

share

improve this question


MiguelC
750 6 28 54

Asked
Apr 21 '13 at 20:40

user1987392
1,797 2 17 38

Edited
Feb 16 '14 at 18:17

Don't believe this works on backgrounds, but it should work on images. android:scaleType="centerCrop" EGHDK Apr 21 '13 at 20:59
add a comment

7 Answers

98

There are several ways you can do it.


Option 1:
Create different perfect images for different dpi and place them in related drawable folder. Then set
android:background="@drawable/your_image

Option 2:

Order By

Votes

Add a single large image. Use FrameLayout. As a first child add an ImageView. Set the following in your ImageView.
android:src="@drawable/your_image"
android:scaleType = "centerCrop"

share

improve this answer


StinePike
29.2k 5 49 73

Answered
Apr 22 '13 at 3:39

Community
1

Edited
Dec 9 '13 at 4:18

where do you store 'your_image'? Atharva Johri May 18 '14 at 13:20

In the res->drawable folders. There may be multiple - each stand for a different resolution (ex. low resolution/high resolution). If you don't have specific images for each resolution,
any one will work. krodmannix May 27 '14 at 23:52

The answer is good. But i wonder if someone found the typical phone sizes for each dpi group, which would be quite useful when preparing images. Edited: found stackoverflow.com/questions/10574363/ fox Jun 19 '14 at 13:15

last option -> out of memory :D ternes3 Jul 15 '14 at 17:15

Wouldn't mixing option 1 and 2 be the best option? Lay Gonzlez Oct 14 '14 at 15:46
show 5 more comments

14

Another option is to add a single image (not necessarily big) in the drawables (let's name it backgroung.jpg), create an ImageView
iv_background at the root of your xml without a "src" attribute. Then in the onCreate method of the corresponding activity:

/* create a full screen window */


requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.your_activity);
/* adapt the image to the size of the display */
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
Bitmap bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(
getResources(),R.drawable.background),size.x,size.y,true);
/* fill the background ImageView with the resized image */
ImageView iv_background = (ImageView) findViewById(R.id.iv_background);
iv_background.setImageBitmap(bmp);

No cropping, no many different sized images. Hope it helps!

share

improve this answer


axplusb
154 1 5

Answered
Aug 9 '14 at 13:43

This is a useful solution, but remember to do bitmap processing off of the main thread: developer.android.com/training/displaying-bitmaps/ Kyle Ivey Sep 22 '14 at 21:47
add a comment

You should put the various size images into the followings folder
for more detail visit this link
ldpi
mdpi
hdpi
xhdpi
xxhdpi

and use RelativeLayout or LinearLayout background instead of using ImageView as follwoing example
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/your_image">
</RelativeLayout>

share

improve this answer


Munish Kapoor
1,173 8 22

Answered
Apr 22 '13 at 6:16

m vai
7,253 4 24 74

Edited
Jun 13 '15 at 18:06

I'm getting an error that Unexpected namespace prefix "xmlns" found for tag RelativeLayout CodyBugstein Jun 18 '14 at 11:59
Is this in mitmap? Ruchir Baronia Nov 11 '15 at 18:43
@rich its drawable Munish Kapoor Nov 12 '15 at 2:58
add a comment

What about
android:background="@drawable/your_image"

on the main layout of your activity?


This way you can also have different images for different screen densities by placing them in the appropriate res/drawable-**dpi folders.

share

improve this answer

NoToast
358 2 13

share

In lines with the answer of NoToast, you would need to have multiple versions of "your_image" in your res/drawable-ldpi,mdpi, hdpi, x-hdpi (for
xtra large screens), remove match_parent and keep android: adjustViewBounds="true"

improve this answer


sb_269
318 5 15

Answered
Apr 21 '13 at 20:54

Answered
Apr 22 '13 at 4:29

If you have bg.png as your background image then simply:


<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>

share

improve this answer


Muhammad Rehan Qadri
182 1 10

use this
android:background="@drawable/your_image

in your activity very first linear or relative layout.

Answered
Jan 3 '15 at 17:54

share

improve this answer


Syed
29 2

Answered
Nov 12 '14 at 13:46

Yawar
685 2 8 26

Edited
Dec 21 '15 at 16:52

Your Answer

log in
or

Name

Email

By posting your answer, you agree to the privacy policy and terms of service.

Post Your Answer

Post Your Answer

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc

S-ar putea să vă placă și