Ad

Android: The Location Of Title For Alert Dialog Fall Below The Line

- 1 answer

I am showing an alert dialog when one of the item in list view is clicked. Everything is fine except the location of title for the dialog is wrong as shown in the image below. Why the title is located below the line instead of above the line?

enter image description here

Code of alert dialog

ListView listView = (ListView)findViewById(R.id.listView);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this,
                android.R.style.Theme_Holo_Light_Dialog);
        View view2 = getLayoutInflater().inflate(R.layout.dialog,null);
        builder.setView(view2);
        builder.setTitle("Edit")
                .setCancelable(false)
                // Add action buttons
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        //
                        refreshData();
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                    }
                })
                .setIcon(android.R.drawable.ic_menu_edit)
                .show();
    }
});

Layout of alert dialog

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialog_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp">

    <EditText
        android:id="@+id/edittext_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Date"
        android:inputType="text" />

    <EditText
        android:id="@+id/edittext_point"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Point"
        android:inputType="numberSigned" />

    <EditText
        android:id="@+id/edittext_description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Description"
        android:inputType="text" />

</LinearLayout>     
Ad

Answer

You're using a deprecated style for the dialog. Try this:

final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

This is working fine for me.


EDITS

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->  
    ...
    <item name="alertDialogTheme">@style/AlertDialogStyle</item> 
    ...
</style>
<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">#c0ff00</item>
    <item name="android:titleTextStyle">@style/MyTextStyle</item>
</style>
Ad
source: stackoverflow.com
Ad