Coding Bihar

How to set wallpaper using jetpack compose

How to set wallpaper using jetpack compose - Coding Bihar
How to set wallpaper using jetpack compose
How to set wallpaper using jetpack compose. Here is the output of the tutorial we are going to learn today. In this tutorial we will build an App, to set image as a wallpaper using jetpack compose. The image stored in device is used to set up as a wallpaper.





Permission Required in Manifest file to access images from device memory

  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
        android:maxSdkVersion="32" />
    <uses-permission android:name="android.permission.SET_WALLPAPER" />

Source Code is below👇

MainActivity

Copy this code →

package com.example.myapplication

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import com.example.myapplication.ui.theme.MyApplicationTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            MyApplicationTheme {
                WallpaperApp()
            }
        }
    }
}

WallPaperApp

Copy this code →

package com.example.myapplication

import android.app.WallpaperManager
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.widget.Toast
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import java.io.IOException

@Composable
fun SelectImageScreen(onImageSelected: (Uri) -> Unit) {
    LocalContext.current
    val launcher = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.GetContent()
    ) { uri: Uri? ->
        uri?.let {
            onImageSelected(it)
        }
    }

    Button(onClick = {
        launcher.launch("image/*")
    }) {
        Text("Select Image")
    }
}

fun loadBitmapFromUri(uri: Uri, context: Context): Bitmap? {
    return try {
        val inputStream = context.contentResolver.openInputStream(uri)
        BitmapFactory.decodeStream(inputStream)
    } catch (e: Exception) {
        e.printStackTrace()
        null
    }
}
@Composable
fun SetWallpaperButton(bitmap: Bitmap) {
    val context = LocalContext.current
    val wallpaperManager = WallpaperManager.getInstance(context)

    Button(onClick = {
        try {
            wallpaperManager.setBitmap(bitmap)
            Toast.makeText(context, "Wallpaper set successfully!", Toast.LENGTH_SHORT).show()
        } catch (e: IOException) {
            e.printStackTrace()
            Toast.makeText(context, "Failed to set wallpaper", Toast.LENGTH_SHORT).show()
        }
    }) {
        Text("Set as Wallpaper")
    }
}
@Composable
fun WallpaperApp() {
    var selectedImageUri by remember { mutableStateOf(null) }

    Column(
        modifier = Modifier.fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        SelectImageScreen { uri ->
            selectedImageUri = uri
        }

        Spacer(modifier = Modifier.height(16.dp))

        selectedImageUri?.let { uri ->
            val context = LocalContext.current
            val bitmap = loadBitmapFromUri(uri, context)

            bitmap?.let {
                Image(
                    bitmap = it.asImageBitmap(),
                    contentDescription = null,
                    modifier = Modifier.size(200.dp)
                )
                Spacer(modifier = Modifier.height(16.dp))
                SetWallpaperButton(it)
            } ?: run {
                Toast.makeText(context, "Failed to load image", Toast.LENGTH_SHORT).show()
            }
        }
    }
}
 Sandeep Gupta

Posted by Sandeep Gupta

Please share your feedback us at:sandeep@codingbihar.com. Thank you for being a part of our community!

Special Message

Welcome to coding bihar!