0

I'm actually trying to fit a fullScreen activity on a little mobile. Here is my layout :

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello 1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello 2"/>

        <LinearLayout
            android:id="@+id/theLinearLayoutIWantToSee"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <CustomEditText
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <CustomEditText
                android:id="@+id/editText2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="click"/>
        </LinearLayout>

    <LinearLayout
        android:id="@+id/myLinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"></LinearLayout>

        ...Some TextViews...

    <LinearLayout
        android:id="@+id/myLinearLayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        ...Some TextViews...

    </LinearLayout>


    </LinearLayout>

</ScrollView>

My problem is, when the keyboard shows up by clicking on one of the editTexts, I want to center the screen on theLinearLayoutIWantToSee, with the button at the bottom.

Actually, if I click for example on the first editText, I'll only see the keyboard, the editText, and the things on top of it.

I've obviously tried to put adjustPan and adjustResize in my manifest but it doesn't solve my problem.

Do you have some suggestions please?

Kulbuto
  • 79
  • 1
  • 13

2 Answers2

0

You can try out binding.theLinearLayoutIWantToSee.requestFocus().

If you are not using viewBinding()/binding as I have mentioned above, just replace the binding part with findViewById<LinearLayout(R.id.theLinearLayoutIWantToSee).requestFocus()

Also note that, you can apply requestFocus() part on the click event of your desired editText. So the it will request the focus only at the click event and not instantly.

If this solution doesn't help out, give some detailed explanation. So that anyone can help you with your desired goal.

Vishal Shah
  • 220
  • 3
  • 11
0

In java you can try this:

EditText editText = findViewById(R.id.editText);
final View parentLayout = findViewById(R.id.parentLayout); // Replace with your parent layout or ScrollView

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            // Calculate desired scroll position or offset
            int[] location = new int[2];
            editText.getLocationOnScreen(location);
            int editTextBottom = location[1] + editText.getHeight();
            int keyboardHeight = /* Calculate the keyboard height */;

            int scrollAmount = Math.max(0, editTextBottom - (parentLayout.getHeight() - keyboardHeight));

            // Scroll the parent layout or ScrollView to the desired position
            parentLayout.scrollTo(0, scrollAmount);

            // Adjust window's soft input mode to allow resizing
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        }
    }
});
Jamil Hasnine Tamim
  • 4,389
  • 27
  • 43