3

I have a list of videos the user has recorded in my app. When the user long-clicks on a video's name in a ListView, a dialog pops up to give the user the options: Play, Rename, Delete. Play brings up a chooser for a video player to play the video. Works as it should. Delete brings up another dialog for confirmation that user wants to delete the video. Also works as it should. When Rename is clicked, it's supposed to show another AlertDialog containing an EditText from a custom view to let the user rename the video.

Here's the XML for the custom view set for the renaming 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 setup the custom view and the 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 the AlertDialog shows up, it has the icon, title, message and buttons, but not the custom view.

Adam Komar
  • 211
  • 3
  • 9
  • 1
    in your xml id of edittext is etRename, but in your code is R.id.etFilename. did you check it? – MilanNz Apr 27 '15 at 21:36
  • Not the problem, but good eye. Definitely would have caused problems later. I have another view with an EditText using that name so Eclipse didn't throw an error at me. – Adam Komar Apr 27 '15 at 22:20

2 Answers2

1

From the AlertDialog documentation:

If you want to display a more complex view, look up the FrameLayout called "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 show() helps.

questioner
  • 2,283
  • 3
  • 26
  • 35
  • adRename is a null object before the builder chain so trying this results in `NullPointerException` when trying to call `findViewById()` on adRename. Also, I don't want to show the dialog at the point where the chain is happening; just initialize the dialog so it's ready for use so changing `create()` to `show()` wouldn't be good. – Adam Komar Apr 27 '15 at 22:23
0

I cannot look up the sources right now, but please try removing this call setMessage() from the chain (and probably title and icon related as well).

Firstly, it makes no sense since you provide your own layout. Secondly, this call may actually block the custom view usage.

AndroidEx
  • 15,524
  • 9
  • 54
  • 50
  • I've tried it only using `setView()` and `create()` in the chain and it still doesn't work. In that case, the screen darkens slightly and nothing at all shows. – Adam Komar Apr 27 '15 at 22:21
  • 1
    @AdamKomar are you sure there's no small letters of `@string/hint_rename` in the middle of the screen? :) Perhaps, you don't see anything, because both of your views are transparent. The edittext's hint should be visible though anyway. Try adding `android:background` to the frame view. – AndroidEx Apr 27 '15 at 22:24
  • I like the thinking outside the box. Unfortunately, that doesn't seem to be the case. I set `android:background="@android:color/black"` and even set etRename with `android:textColor="@android:color/white"` to make sure they contrast and still nothin'. It's showing up fine in the Graphical Layout preview in Eclipse, but nothing when the app runs. Still gonna leave those settings for now just in case. @Android777 – Adam Komar Apr 27 '15 at 22:33
  • @AdamKomar hmm, that's strange... let me think about it a little. Btw, what context do you use? `Activity`, not `Application`? – AndroidEx Apr 27 '15 at 22:41
  • @AdamKomar you can test one more workaround: use `Dialog` instead of `AlertDialog`, like this: `Dialog adRename = new Dialog(context); adRename.setContentView(vRename);`. In my projects I used to get rid of some issues just by substituting `AlertDialog` with `Dialog`. – AndroidEx Apr 27 '15 at 22:47
  • It's technically `Activity` context. The project itself extends `ActionBarActivity` and I set `context` in `onCreate` as `context = this;`. I'll try switching to `Dialog` and let you know what happens. – Adam Komar Apr 28 '15 at 20:00
  • `new Dialog(context)` is returning null and that's another headache I don't want to deal with. – Adam Komar Apr 28 '15 at 21:26
  • @AdamKomar how come a constructor does not return an object? :) – AndroidEx Apr 28 '15 at 21:27
  • Because it's poorly written that doesn't work the way it's supposed to work? – Adam Komar May 03 '15 at 17:32
  • @AdamKomar it's just hypothetically impossible for [constructor to return null in java](http://stackoverflow.com/a/11103481/4096987). Perhaps, there's another problem. – AndroidEx May 03 '15 at 17:38
  • In `onCreate` I set `context = this;`. When I need to create the dialog, I do `dialogRename = new Dialog(context);` When trying to use any method associated with the dialog, I get a null object reference error. – Adam Komar May 04 '15 at 22:33