I'm facing a problem with the AlertDialog and custom content / view. Simply said the AlertDialog doesn't resize itself when the softkeyboard is opened. The following screenshots show best what my problem is and what I want to achieve:
Current behavior (left) & wanted behavior (right)
I know there a some other threads with a similar issue on SO. Unfortunately none of the provided solutions worked for me. Following my sample code:
XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</EditText>
</LinearLayout>
</ScrollView>
Java - CustomAlertDialog.class
public class CustomAlertDialog extends AlertDialog{
private Context context;
public CustomAlertDialog(Context context) {
super(context);
this.context = context;
}
public void buildDialog(){
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.dialog_content, null);
builder = new AlertDialog.Builder(context);
builder.setTitle("EditText");
builder.setView(layout);
builder.setPositiveButton("Ok", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
dismiss();
}
});
builder.create();
}
public void showDialog(){
builder.show();
}
}
The class / function above is called on a button press in my Main.java class
btnAlertDialog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
CustomAlertDialog dialog = new CustomAlertDialog(Main.this);
dialog.buildDialog();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
dialog.showDialog();
}
});
I've tried the following things:
Adding scrollbars to the LinearLayout like this
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
Result: nothing changed
Setting a flag for the dialog:
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
Result: nothing changed
Applying a custom-style to the AlertDialog:
<style name="AlertDialog" parent="@android:style/Theme.Holo.Light">
<item name="android:windowFullscreen">false</item>
</style>
Java
builder = new AlertDialog.Builder(context, R.style.AlertDialog);
This worked. Unfortunately it produced some problems which you can see the in the following screenshot
I appreciate any help since I've been struggling with this issue for days. Thanks!
I'm now using the following method to create the Dialog in my CustomAlertDialog.class
. See my comment below the answer of nandeesh for further details why it didn't worked before.
public void startDialog(){
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.dialog_content, null);
builder = new AlertDialog.Builder(context);
builder.setTitle("EditText");
builder.setView(layout);
builder.setPositiveButton("Ok", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
dismiss();
}
});
AlertDialog aDialog = builder.create();
aDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
aDialog.show();
}
I am not sure where you did the setSoftInputmode but instead of
builder.show();
use
AlertDialog mDialog = builder.create();
mDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
mDialog.show();
If this does not work, then do post the CustomAlertDialog class
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With