windowSoftInputMode="adjustResize" doesn't seems to work in case of Absolute Positioning in Relative Layout

49 Views Asked by At

I have an EditText in the lower part of my Android screen as shown in image. Due to some project contraints I'm supposed to use only Absolute positioning.

When clicked on the EditText the soft keyboard hides the EditText as shown in the other image.

enter image description here enter image description here

I have tried putting android:windowSoftInputMode="adjustResize" in the manifest file (also tried adjustPan) It only works for relative positioning (commented textField_layoutparams code below) but doesn't seems to be working for absolute positioning.

Even tried putting a ScrollView (commented code below) but it doesn't work.

Code:

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

        val scrollView: ScrollView
        val relativeLayout: RelativeLayout
        val textField: EditText

    /*scrollView = ScrollView (this)
    scrollView.layoutParams = RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.MATCH_PARENT,
        RelativeLayout.LayoutParams.MATCH_PARENT
    )*/
    //scrollView.isFillViewport = true

    //setContentView(scrollView)

    relativeLayout = RelativeLayout(this)
    relativeLayout.layoutParams = RelativeLayout.LayoutParams (
        RelativeLayout.LayoutParams.MATCH_PARENT,
        RelativeLayout.LayoutParams.MATCH_PARENT
    )
    relativeLayout.setBackgroundColor(Color.parseColor("#0000FF"))

    //scrollView.addView(relativeLayout)
    setContentView (relativeLayout)

    textField = EditText(this)

    val textField_layoutparams: LayoutParams
    textField_layoutparams = RelativeLayout.LayoutParams (
        400,
        150
    )
    textField.setBackgroundColor(Color.parseColor("#FFFFFF"))

    //textField_layoutparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE)
    //textField_layoutparams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE)
    //textField_layoutparams.setMargins(0, 0, 0, 200)
    textField.layoutParams = textField_layoutparams
    textField.hint = "Enter Text"
    textField.x = 500F
    textField.y = 2200F

    relativeLayout.addView(textField)
}

/**
 * A native method that is implemented by the 'scrollviewpoc' native library,
 * which is packaged with this application.
 */
external fun stringFromJNI(): String

companion object {
    // Used to load the 'scrollviewpoc' library on application startup.
    init {
        System.loadLibrary("scrollviewpoc")
    }
}
}

Is there any way to achieve the desired behaviour in case of Absolute positioning?

1

There are 1 best solutions below

0
Urvish Shiroya On

I think that issue generated when you tried to set EditText x and y from Kotlin side.

the property android:windowSoftInputMode="adjustResize" into in Manifest. that is work for all Android device, but in your case i noticed a difference when you set X and Y programmatically.

You should try this one. i hope its work for you

override fun onCreate(){
    // ...
    relativeLayout.addView(textField) // your last line add bellow of that code

    val rootView = findViewById<View>(android.R.id.content)

    rootView.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
        override fun onPreDraw(): Boolean {
            val heightDiff = rootView.height - relativeLayout.height

            if (heightDiff > 100) { // Adjust this threshold according to your needs
                // Soft keyboard is shown
                val newTopMargin = 2200 - heightDiff
                textField_layoutparams.topMargin = newTopMargin
                textField.layoutParams = textField_layoutparams
            } else {
                // Soft keyboard is hidden
                textField_layoutparams.topMargin = 2200
                textField.layoutParams = textField_layoutparams
            }

            return true
        }
    })
}