AlertDialog.Builder: setView; custom view is not displayed

I have a list of videos recorded by a user in my application. When the user long clicks on the video name in the ListView, a dialog box opens to provide the user with the following options: Play, Rename, Delete. Playback brings up the selection of the video player for video playback. Works as it should. Delete brings up another dialog box to confirm that the user wants to delete the video. Also works as it should. When clicked on rename, it should show another AlertDialog containing the EditText from the custom view to allow the user to rename the video.

Here's the XML for the custom view set to rename the AlertDialog:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/flRename"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <EditText
        android:id="@+id/etRename"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_rename" />
</FrameLayout>

      

In onCreate, I set up a custom view and AlertDialog:

vRename   = getLayoutInflater().inflate(R.layout.rename, null);
etRename  = (EditText)vRename.findViewById(R.id.etRename);

adRename = new AlertDialog.Builder(context)
    .setIcon(R.drawable.ic_launcher)
    .setMessage("Rename video:")
    .setPositiveButton("Rename", dioclRename)
    .setNegativeButton("Cancel", null)
    .setTitle(getString(R.string.app_name))
    .setView(vRename)
    .create();

      

When an AlertDialog message appears, it has an icon, title, message, and buttons, but not a custom view.

+3


source to share


2 answers


From AlertDialog documentation:

If you want to display a more complex view, find a FrameLayout named "custom" and add your view to it:

FrameLayout fl = (FrameLayout) findViewById(android.R.id.custom);
fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));

      

so maybe call:



FrameLayout fl = (FrameLayout) adRename.findViewById(android.R.id.custom);
fl.addView(vRename, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));

      

or check if switching from create()

to helps show()

.

+1


source


I can't search for sources right now, but please try removing this call setMessage()

from the chain (and probably the title and icon related as well).



First, it doesn't make sense as you provide your own layout. Second, this call can actually block the use of the custom view.

0


source







All Articles