-1

I have a custom DialogFragment whose layout holds just a EditText element.

Layout:

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/reminder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/reminder_message"
        android:inputType="text" />
</LinearLayout>

Code:

public class ReminderDialog extends DialogFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_reminder, container, false);
        return rootView;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (onDismissListener != null) {
        String text = (String) ((TextView) (rootView.findViewById(R.id.reminder))).getText();
        onDismissListener.onDismiss(text);
        }
    }

It is working fine in app module. But I want to take into library module. But then I debugged the error, it is because R.id.reminder is 0.

Is there anyway to access the element.

and_dev
  • 3,723
  • 1
  • 20
  • 28
Kuldeep Yadav
  • 1,664
  • 5
  • 23
  • 41
  • What you want to do? Why you accessing it in ondismiss – Sohail Zahid Jun 18 '16 at 14:00
  • I am long pressing a map marker's InfoWindow and that opens a DialogFragment having an EditText element. I don't want to have any submit button. As soon as user closes the dialog, text is set in InfoWindow. – Kuldeep Yadav Jun 18 '16 at 14:09

4 Answers4

0

Try to use getDialog().findViewById() instead of rootView.findViewById(R.id.reminder)

**

EDIT

**

try to use

Resources.getIdentifier(...)

You can do

getResources().getIdentifier("res_name", "res_type", "com.library.package");

In your case:

R.id.reminder would be:

getResources().getIdentifier("reminder", "id", "com.library.package");

Ognev Zair
  • 1,626
  • 1
  • 13
  • 17
  • getDialog() is returning null – Kuldeep Yadav Jun 18 '16 at 14:15
  • It won't work because R.id.remainder is coming to be 0 – Kuldeep Yadav Jun 18 '16 at 17:52
  • try to use [Resources.getIdentifier(...)](http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29) You can do getResources().getIdentifier("res_name", "res_type", "com.library.package"); in your case: R.id.reminder would be: getResources().getIdentifier("reminder", "id", "com.library.package"); – Ognev Zair Jun 18 '16 at 18:01
  • check imports of your R.class, maybe your R imported from out of your library – Ognev Zair Jun 18 '16 at 18:25
  • Hi Ognev, I checked. But that is not the problem. Can you please have a look at the code. I can send you that on mail if you don't mind. Please send me empty msg on kuldeep5534@gmail.com Thanks, – Kuldeep Yadav Jun 18 '16 at 18:37
  • I made my repo public, have a look. https://bitbucket.org/kuldeepiitg/location-alert – Kuldeep Yadav Jun 18 '16 at 18:45
  • i've found a problem, your problem in cast of types :) replace 47 line in ReminderDialog with this line String text = ((TextView) (rootView.findViewById(R.id.reminder))).getText().toString(); – Ognev Zair Jun 18 '16 at 19:13
  • and make build->clean and rebuild :) – Ognev Zair Jun 18 '16 at 19:14
  • No brother, That is not the problem. Try debugging the code. You will get the issue is with id not typecast. – Kuldeep Yadav Jun 18 '16 at 19:25
  • i debugged and found just string cast exception, after fix it worked.... strange but it working on my phone – Ognev Zair Jun 18 '16 at 19:28
  • my logs with test message in dialog is 06-19 00:10:19.191 7986-7986/com.palup.locationalert I/Place Chooser﹕ test – Ognev Zair Jun 18 '16 at 19:29
  • This is strange, certainly. I am planning to move the fragment to app module where it works. – Kuldeep Yadav Jun 19 '16 at 01:40
  • Wait! what did you do to reproduce the bug? The steps are as following : 1. Select a place in Place Autocomplete Text box. 2. Now, click the map marker, it will show InfoWindow. 3. Long press the InfoWindow, it will open a text dialog. Write some text there. 4. Dismiss the dialog by pressing outside the the text box. Now it will execute the OnDismiss and throws error. – Kuldeep Yadav Jun 19 '16 at 02:00
  • I checked in one more environment, no difference. – Kuldeep Yadav Jun 19 '16 at 02:14
  • I moved DialogFragment from library to app module. So it is working fine now. But thanks a lot for all your efforts. – Kuldeep Yadav Jun 19 '16 at 12:46
0

Try to cast the Dialog instance supplied and then find the view:

((Dialog) dialog).findViewById(R.id.reminder); 

Don't forget to cast to the appropriate type of Dialog you are using but in general Dialog should work out.

and_dev
  • 3,723
  • 1
  • 20
  • 28
0

The method onCreateView is useless for DialogFragments.
You should override:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = new Dialog(getActivity(), null);
    dialog.setContentView(R.layout.fragment_reminder);
    return dialog;
}

Then, as Ognev Zair pointed out, you can use getDialog():

String text = (TextView) (getDialog().findViewById(R.id.reminder))).getText().toString();
Juan Cruz Soler
  • 8,172
  • 5
  • 41
  • 44
0

I also checked out your solution. There isn't any problem with the view id! If it's there it is related to your local setup. So please run:

gradlew clean 

and then build the project again. The only error I got is related to a wrong cast you are doing when retrieving the text input:

06-19 10:38:29.602 7974-7974/com.palup.locationalert E/AndroidRuntime: FATAL EXCEPTION: main Process: com.palup.locationalert, PID: 7974 java.lang.ClassCastException: android.text.SpannableStringBuilder cannot be cast to java.lang.String at com.palup.library.fragment.ReminderDialog.onDismiss(ReminderDialog.java:47) at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1323) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

So just update your code within the ReminderDialog:

@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    if (onDismissListener != null) {
        String text = ((EditText) (rootView.findViewById(R.id.reminder))).getText().toString();
        onDismissListener.onDismiss(text);
    }
}
and_dev
  • 3,723
  • 1
  • 20
  • 28