Android - How do I re scale eveything when the softKeyboard appears (adjustResize) but keep the background the same size?

Viewed 17

So in my App, when the keyboard appears, the whole layout is resized to fit above it, which is exactly what I want except for the background image, I want it to stay the same size.
After some research, I saw a solution was to set the background programmatically (not using an ImageView in the XML):

getWindow().setBackgroundDrawableResource(R.drawable.background);

Which works to some extent, as it dosn't get resized when the keyboard appears, but the image is stretched to fit the screen's ratio. I found another solution to combine using xml resources:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/background"
    android:tileMode="repeat">
</bitmap>

Using this, the drawable stays the right ratio but isn't scaled down to the size of the screen (tried using the gravity attribute but it didn't do anything).

1 Answers

You don't have control over scaling with a background drawable. What you could do is put an image view over the root layout taking the entire size. Then you can use the scale options of the ImageView to scale it as you would any other image (probably FITX in this case, since you don't want it to scale with a change in Y). Just make the ImageView the first view inside the parent so it takes the bottom of the z order.

Related