How to Set Background Image Android Studio
Set background image android studio is very simple to do. I will give an example of the XML file, the example is easy to understand and follow the steps. For your information, I have another article about Android too, please read the How to set Gradient Background color in Android Studio and Animated Background on Android studio. I wrote these two articles a few days ago.
This article was made based on Android Studio version 3.4 built on April 10, 2019. I like to use Android Studio because it’s free software and open source and is supported by Google. Not only free, but many android studio tutorials are also found on the internet, so we can easily find help when we need help.
Steps To Set Background Image Android Studio
1. Prepare the Image
You must prepare your image for the background. In this example, I will use a picture of a building. You can use photos of your own child or wife. You can also use your beloved family photo as a background image. The best size picture for the background is as below.
Background Image Size (in Pixel) which Support All Devices:
- XHDPI -> width 720 and height 1280
- HDPI -> width 480 and height 800
- MDPI -> width 320 and height 480
- LDPI -> width 240 and height 320
Maybe some of you ask why should the image size be like that? You can be using free image size but the image will be distorted in the final result. Provide four types of image sizes with the same image. Save all images in their respective folders. For more details please see the following picture:
The file extension for the background can use JPG or PNG, I choose to use JPG. For images with other extensions, I haven’t tried it yet.
2. Android XML Background Image
First, open the Android Studio program that you have installed. After that create your project (choose Empty Activity), named the project as you like. In the picture below, I named my project How to Set Background Image to Android Studio, language choices choose Java and API Level, I chose API level 15.
Second, open the activity_main.xml file and add the background attribute. More details, please see the activity_main.xml code below:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:background="@drawable/image720x1280" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
You add android:background=”@drawable/image720x1280″ in TextView, look at the code above. Hopefully, this Android Studio tutorial can help you and if there are questions please write in the comment form.
Update: April 12, 2020
You can create an android studio background image with ImageView:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/motherandson" android:scaleType="fitXY" /> </LinearLayout>