Displaying a background image in Android

Goto your res folder as shown in image below add a background image.

After adding a background image, goto the main.xml and add the below text:
<ImageView
android:layout_height=”wrap_content”
android:layout_width=”wrap_content”
android:src=”@drawable/background” />
Note: Do not add the extn to the image name, it wont recognize the file and give an error.
Next goto your main class and add the following:
super(context);
// Set the background
this.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.background));
Another way to do this would be:
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.background);

Leave a Reply

Your email address will not be published. Required fields are marked *