How to Add Background Image in Android Studio

7 Answers 7

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>                              

tommyk

2,913 5 gold badges 35 silver badges 55 bronze badges

answered Jul 22 '10 at 8:44

1

  • This 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?

    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>                              

answered Jul 22 '10 at 8:53

1

  • I'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?

    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

answered Apr 30 '14 at 9:53

1

  • Which folder that we have to use? @Jesvin

    Jul 7 '14 at 20:05

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>                          

enter image description here

answered Apr 5 '16 at 18:12

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>                          

answered May 20 '17 at 9:42

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

answered Sep 1 '13 at 11:58

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

answered Nov 4 at 14:04

Not the answer you're looking for? Browse other questions tagged android image background or ask your own question.

How to Add Background Image in Android Studio

Source: https://stackoverflow.com/questions/3307090/how-to-add-background-image-to-activity

0 Response to "How to Add Background Image in Android Studio"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel