Set background image randomly from gallery in android

How do I set a picture as my background on my Android?

Set background image randomly from gallery in android

Hmm, I think you want to show the background image in your open activity view. But you want to show this one from your gallery and it should be selected randomly from your gallery. If Iam right then you are in right place.

You just need to fetch data from gallery, your code should be like this:

String[] projection = new String[]{
    MediaStore.Images.Media.DATA,
};

Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = managedQuery(images,
                projection,
                "",
                null,
                ""
);

 

So, don't waste your time, let's see the code:

Your Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.legendblogs.showbackground">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

Activity_main.xml

package com.legendblogs.showbackground;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;

public class MainActivity extends AppCompatActivity {

    Context context;
    private RelativeLayout main_layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.context = this;

        main_layout = (RelativeLayout) findViewById(R.id.main_layout);

        String[] projection = new String[]{
                MediaStore.Images.Media.DATA,
        };

        Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        Cursor cursor = managedQuery(images,
                projection,
                "",
                null,
                ""
        );

        final ArrayList<String> imagesPath = new ArrayList<String>();
        if (cursor.moveToFirst()) {
            int dataColumn = cursor.getColumnIndex(
                    MediaStore.Images.Media.DATA);
            do {
                imagesPath.add(cursor.getString(dataColumn));
                if(imagesPath.size() > 100) break;
            } while (cursor.moveToNext());
        }

        cursor.close();
        final Random random = new Random();
        final int count = imagesPath.size();

        handler.post(new Runnable() {
            @Override
            public void run() {
                int number = random.nextInt(count);
                String path = imagesPath.get(number);
                if (currentBitmap != null)
                    currentBitmap.recycle();
                currentBitmap = BitmapFactory.decodeFile(path);

                final int sdk = android.os.Build.VERSION.SDK_INT;
                if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
                    BitmapDrawable ob = new BitmapDrawable(getResources(), currentBitmap);
                    main_layout.setBackgroundDrawable(ob);
                } else {
                    Drawable drawable = new BitmapDrawable(getResources(), currentBitmap);
                    main_layout.setBackground(drawable);
                }
                handler.postDelayed(this, 1000);
            }
        });

    }
}

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/main_layout"
    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.legendblogs.asynctask.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome to Legend Blogs Exmpale"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textStyle="bold"
        android:textSize="20dp" />


</RelativeLayout>
Top