Search code examples
androidapidialogcustomdialog

What should I use instead of custom dialog in android api less 20


I wrote an application which have to call dialog to get name. I need the custom view in dialog so I used .setContentView(). But in android with api less 20 I have problems with displaying it. This is calling dialog:

public void setName(View v) {
        nameDialog = new Dialog(this);
        //using our dialog
        nameDialog.setContentView(R.layout.new_name);
        EditText text = (EditText) nameDialog.findViewById(R.id.form2_dialog_name);
        //if we have previous name we show it
        if (haveName) {
            text.setText(((Button) findViewById(R.id.form2_name_button)).getText());
        }
        //request focus and call keyboard for input name
        text.requestFocus();
        text.selectAll();
        nameDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        nameDialog.show();
    }

the xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_blue_dark"
    tools:context=".CreateNoteActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/form2_dialog_name_of_event"
        android:id="@+id/textView11" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text=""
        android:ems="10"
        android:id="@+id/form2_dialog_name"
        android:maxLines="1" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end">

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/close_button"
            android:id="@+id/button8"
            android:onClick="onCancelDialog"/>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/okay_button"
            android:id="@+id/okay_form2_dialog_button"
            android:onClick="onFinishDialog" />
    </LinearLayout>

</LinearLayout>

How it looks when api upper then 20: https://i.sstatic.net/mYwxZ.png

Less then 20: https://i.sstatic.net/LNL2j.png


Solution

  • OK the answer is quite simple: Use the android.support.v7.app.AlertDialog

    Your setName will have to look like this then:

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
    
        LayoutInflater inflater = this.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.new_name, null);
        builder.setView(dialogView);
    
        EditText text = (EditText) dialogView.findViewById(R.id.form2_dialog_name);
        //if we have previous name we show it
        if (haveName) {
            text.setText(((Button) findViewById(R.id.form2_name_button)).getText());
        }
        //request focus and call keyboard for input name
        text.requestFocus();
        text.selectAll();
    
        AlertDialog dialog = builder.create();
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        dialog.show();
    

    I guess you will have to play a little with your layout file because the buttons are quite a bit too far away but I think this will be just a little problem ;)