I'm trying to make a LinearLayout align with the Software Keyboard when it is showing.
My LinearLayout contains an EditText to which the Software Keyboard is always being aligned.
The whole View (LinearLayout) looks like this:
When gaining focus to edit the EditText keyboard aligns itself to the EditText instead of the whole View:
I know that in the Material Design Library there is a TextInputLayout which also is a LinearLayout containing an EditText. When focusing on the EditText wrapped in the TextInputLayout the whole LinearLayout is aligned with the keyboard:
Now, I've seen people tell that I should add a windowSoftInputMode parameter in the <activity>:
android:windowSoftInputMode="adjustResize"
which does not work. Also I've seen many other answers on that issue but all of them referred to setting flags and parameters outside of the View itself but within the Activity's Window which is not what I want.
Here is the code that approximates mine - it has the dependencies that the CustomEditText has:
public class TestEditText extends LinearLayoutCompat {
public TestEditText(@NonNull Context context) {
super(context);
initTestEditText();
}
public TestEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initTestEditText();
}
public TestEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initTestEditText();
}
// Layout in which there is an EditText - addiationla views, like buttons can be here
private LinearLayoutCompat textInputLayout;
private AppCompatEditText editText;
private void initTestEditText() {
setOrientation(VERTICAL);
setGravity(Gravity.CENTER_VERTICAL);
setBackgroundColor(Color.BLUE);
// Getting states from children - in this case from another LinearLayout
setAddStatesFromChildren(true);
prepareTextInputLayout();
prepareEditText();
}
private void prepareTextInputLayout() {
textInputLayout = new LinearLayoutCompat(getContext());
textInputLayout.setLayoutParams(
new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dpToPx(128))
);
textInputLayout.setGravity(Gravity.CENTER_VERTICAL);
// Getting the states from children - in this case from an editText
textInputLayout.setAddStatesFromChildren(true);
addView(textInputLayout);
}
private void prepareEditText() {
editText = new AppCompatEditText(getContext());
LayoutParams params = new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT,
1
);
editText.setLayoutParams(params);
editText.setGravity(Gravity.TOP);
editText.setBackgroundDrawable(null);
editText.setMaxLines(Integer.MAX_VALUE);
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
editText.setTextSize(14);
textInputLayout.addView(editText);
}
private int dpToPx(int dp) {
return (int) (dp * getResources().getDisplayMetrics().density);
}
}
There has to be some way to make the bottom of a View align with the Software Keyboard, because TextInputLayout does exactly that without setting any flags in the <activity>...
If You have any other idea, please help.


