Set activity layout background image programmatically android

Change android app screen background image on button click used in themes design.

Mostly background image changing feature through MainActivity.java programming file is used to create themes on android apps so app user can choose it own most like image and set that image as activity background. This feature are currently available in most of android online chatting apps where developer select image from given multiple selection. So here is the complete step by step tutorial for Set activity layout background image programmatically android .

android-project-download-code-button

Note: Upload all images in res->drawable-hdpi folder.

drawable hdpi folder

How to Set activity layout background image programmatically android.

Code for MainActivity.java file.

 package com.android_examples.com.layoutbackgroundimageprogrammatically;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;


public class MainActivity extends Activity {

 RelativeLayout rl;
 Button image1,image2,image3;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 rl = (RelativeLayout)findViewById(R.id.RelativeLayout1);
 image1 = (Button)findViewById(R.id.button1);
 image2 = (Button)findViewById(R.id.button2);
 image3 = (Button)findViewById(R.id.button3);
 
 image1.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 rl.setBackgroundResource(R.drawable.background_image);
 
 }
 });
 
 
 image2.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 rl.setBackgroundResource(R.drawable.background_image_2);
 
 }
 });


 image3.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 rl.setBackgroundResource(R.drawable.background_image_3);
 
 }
 });
 
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/RelativeLayout1"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.layoutbackgroundimageprogrammatically.MainActivity"
 >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="APPLY BACKGROUND IMAGE 2" />

 <Button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@+id/button1"
 android:layout_alignRight="@+id/button1"
 android:layout_marginBottom="17dp"
 android:text="APPLY BACKGROUND IMAGE 1" />

 <Button
 android:id="@+id/button3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignLeft="@+id/button1"
 android:layout_below="@+id/button1"
 android:layout_marginTop="27dp"
 android:text="APPLY BACKGROUND IMAGE 3" />

</RelativeLayout>

Screenshot:
Set activity layout background image programmatically android

dynamic image 2

Click here to download Set activity layout background image programmatically android project.

Leave a Reply

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