I am making a wallpaper app. I am taking images from my firebase database. But when i tap on set as wallpaper button in my app the wallpaper leaves some empty black space at the top as well as at the bottom. Can't seem to get it to work even after trying out different similar answers on stackoverflow.
My wallpaper set activity :
import android.app.WallpaperManager
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Point
import android.graphics.Rect
import android.os.Build
import android.os.Bundle
import android.view.WindowManager
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.drawToBitmap
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.activity_set_wallpaper.*
import java.io.IOException
class SetWallpaperActivity : AppCompatActivity() {
var image: String?=null
@RequiresApi(Build.VERSION_CODES.N)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_set_wallpaper)
val intent=intent
image=intent.getStringExtra("image")
Picasso.get().load(intent.getStringExtra("image")).into(image_set_wallpaper)
val wm = this.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val display = wm.defaultDisplay
val size = Point()
display.getSize(size)
val height: Int = size.y
val width: Int = size.x
set_wallpaper.setOnClickListener {
val result:Bitmap=rl_iv.drawToBitmap()
val wallpaperManager = WallpaperManager.getInstance(this)
try {
wallpaperManager.setBitmap(result)
}
catch (ex: IOException) {
ex.printStackTrace()
}
}
set_lock_wallpaper.setOnClickListener {
val wpManager = WallpaperManager.getInstance(this)
val myBitmap: Bitmap = rl_iv.drawToBitmap()
try {
wpManager.setBitmap(myBitmap, null, true, WallpaperManager.FLAG_LOCK)
}
catch (ex: IOException) {
ex.printStackTrace()
}
}
}
}
Its xml code is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".SetWallpaperActivity">
<RelativeLayout
android:id="@+id/rl_iv"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image_set_wallpaper"
android:layout_width="match_parent"
android:scaleType="fitXY"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:orientation="horizontal">
<Button
android:id="@+id/set_wallpaper"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="SET as HOME SCREEN WALLPAPER"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:background="@drawable/shape_button_rounded"
android:focusableInTouchMode="true"
android:gravity="center"/>
<Button
android:id="@+id/set_lock_wallpaper"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="SET as LOCK SCREEN WALLPAPER"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@id/set_wallpaper"
android:background="@drawable/shape_button_rounded"
android:focusableInTouchMode="true"
android:layout_marginBottom="20dp"
android:gravity="center"/>
</LinearLayout>
</RelativeLayout>
Please suggest what i should edit in my code to get it work. I am new to this. Please use Kotlin to explain
You are getting the black space because of the margins in the XML file.
<ImageView
android:id="@+id/image_set_wallpaper"
android:layout_width="match_parent"
android:scaleType="fitXY"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"/>
Remove the layout_margin-xx-
in the ImageView
attribute to get your desired output.
Your ImageView
in the XML file should be like this:
<ImageView
android:id="@+id/image_set_wallpaper"
android:layout_width="match_parent"
android:scaleType="fitXY"
android:layout_height="match_parent"/>
This should solve your problem.