I have the following custom view DialogFragment
.
When I tap on the EditText
, soft keyboard is shown. Currently, the observations are
- The position of the dialog is "push up" a bit.
- Dialog is not resized and Dialog content is blocked.
This is how it looks like.
I don't want the dialog content covered by keyboard. I had made modification according to https://stackoverflow.com/a/36295587/72437
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
Still, it doesn't help at all.
I had also tried
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
It makes no difference. The Dialog is not resized and the Dialog content is blocked. Except the soft keyboard will be shown immediately during the first time, without requiring user tapping on the EditText
immediately.
May I know, how can I make my Dialog
resized, when soft keyboard is shown?
Update
I did an experiment by applying the same XML layout file on an Activity. The Activity
is resized without problem.
Seem like the XML layout file itself is OK.
The soft input mode I am using is
<activity android:name="org.yccheok.jstock.gui.trading.sign_in.SignInFragmentActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize|stateAlwaysVisible" />
The is the complete code and layout file.
Source code
package org.yccheok.jstock.gui.trading.sign_in;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.Button;
import org.yccheok.jstock.gui.R;
/**
* Created by yccheok on 8/1/2018.
*/
public class SignInDialogFragment extends DialogFragment {
public static SignInDialogFragment newInstance() {
SignInDialogFragment signInDialogFragment = new SignInDialogFragment();
return signInDialogFragment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Activity activity = getActivity();
// Get the layout inflater
LayoutInflater inflater = LayoutInflater.from(activity);
final View view = createView(this, inflater, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
final AlertDialog dialog = builder.setView(view).create();
dialog.setCanceledOnTouchOutside(true);
final ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
makeDialogShorter(dialog);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
});
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
return dialog;
}
private static View createView(final Fragment fragment, final LayoutInflater inflater, final ViewGroup container) {
View v = inflater.inflate(R.layout.trading_sign_in_fragment, container, false);
Button forgotPasswordButton = (Button)v.findViewById(R.id.forgot_password_button);
Button signInButton = (Button)v.findViewById(R.id.sign_in_button);
forgotPasswordButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
signInButton.setEnabled(false);
return v;
}
private static void makeDialogShorter(Dialog dialog) {
// http://stackoverflow.com/questions/19326142/why-listview-expand-collapse-animation-appears-much-slower-in-dialogfragment-tha
int width = dialog.getWindow().getDecorView().getWidth();
DisplayMetrics displayMetrics = new DisplayMetrics();
dialog.getOwnerActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
height = Math.min(
(int)(height * 4.0 / 5.0),
dialog.getWindow().getDecorView().getHeight()
);
if (height > width) {
dialog.getWindow().setLayout(width, height);
}
}
}
Layout XML
<?xml version="1.0" encoding="utf-8"?>
<ViewAnimator xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/view_animator"
android:animateFirstView="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="@+id/sign_in_relative_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/trading_activity_vertical_margin"
android:paddingBottom="0dp"
android:layout_above="@+id/sign_in_bottom_nav_bar">
<android.support.design.widget.TextInputLayout
android:layout_marginLeft="@dimen/trading_activity_horizontal_margin"
android:layout_marginRight="@dimen/trading_activity_horizontal_margin"
app:hintTextAppearance="@style/TradingWizardTextInputLayout"
android:id="@+id/username_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/wizard_username"
android:id="@+id/username_edit_text"
android:inputType="textVisiblePassword|textNoSuggestions"
android:imeOptions="actionNext|flagNoExtractUi" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_marginLeft="@dimen/trading_activity_horizontal_margin"
android:layout_marginRight="@dimen/trading_activity_horizontal_margin"
app:hintTextAppearance="@style/TradingWizardTextInputLayout"
app:passwordToggleEnabled="true"
android:id="@+id/password_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/wizard_password"
android:id="@+id/password_edit_text"
android:inputType="textPassword"
android:imeOptions="actionNext|flagNoExtractUi" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<Button
style="?android:attr/buttonBarButtonStyle"
android:id="@+id/forgot_password_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:enabled="true"
android:textAllCaps="false"
android:text="@string/forgot_password"
android:textSize="16sp"
android:layout_above="@+id/sign_in_bottom_nav_bar"
android:layout_centerHorizontal="true"
android:layout_marginBottom="8dp"
android:paddingLeft="32dp"
android:paddingRight="32dp" />
<LinearLayout
android:background="?attr/welcomeBottomNavBarBackground"
android:orientation="horizontal"
android:id="@+id/sign_in_bottom_nav_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
style="?android:attr/buttonBarButtonStyle"
android:background="?attr/selectableItemBackground"
android:id="@+id/sign_in_button"
android:layout_width="0dp"
android:width="0dp"
android:layout_weight="1.0"
android:layout_height="48dp"
android:gravity="center"
android:layout_gravity="center"
android:enabled="true"
android:textAllCaps="true"
android:text="@string/log_in" />
</LinearLayout>
</RelativeLayout>
</ViewAnimator>