Skip to content
Advertisement

How to take text input with DialogFragment in Android?

I am trying to get a value that user enters into a Dialog, using the recommended DialogFragment class for it, the Dialog constructs and runs fine, but I cannot return the value of the EditText parameter to the parent class, without get a NullPointerException.

My DialogHost class, this constructs, returns and links the parent to its buttons.

package jo.app.co;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
 
public class DialogHost extends DialogFragment {


public interface NoticeDialogListener {
    public void onDialogPositiveClick(DialogFragment dialog);
    public void onDialogNegativeClick(DialogFragment dialog);
}

NoticeDialogListener mListener;

@override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    
    try {
        
        mListener = (NoticeDialogListener) activity;
        
    } catch (ClassCastException e) {      
        
        throw new ClassCastException(activity.toString());
    }
}

@override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    
    LayoutInflater inflater = getActivity().getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.dialog_add, null))
   
           .setPositiveButton("Save", new DialogInterface.OnClickListener() {
               
               @override
               public void onClick(DialogInterface dialog, int id) {        
                   mListener.onDialogPositiveClick(DialogHost.this);                       
                   
               }
           })
           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   DialogHost.this.getDialog().cancel();                       
               }
           });      
    return builder.create();
}

}

This is my MainActivity class showing the dialog

package jo.app.co;

import android.app.DialogFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.EditText;

public class MainActivity extends FragmentActivity implements      DialogHost.NoticeDialogListener {


 @override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    showNoticeDialog();
    
}

public void showNoticeDialog() {
    
    DialogFragment dialog = new DialogHost();
    dialog.show(getFragmentManager(), "DialogHost");
}


@override
public void onDialogPositiveClick(DialogFragment dialog) {
            
    EditText myText = (EditText) findViewById(R.id.item_added);
    try {
        Log.d ("IN TRY", myText.getText().toString());          
    }
    catch (Exception e) {
        Log.e ("IN CATCH", e.toString());
    }           
    
}

@override
public void onDialogNegativeClick(DialogFragment dialog) {        
    Log.d ("INMAIN", "REACHED NEG");
}


 }

Thisthe layout of my Dialog for reference.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">
   
    <EditText
       android:id="@+id/item_added"
       android:inputType="text"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginTop="16dp"
       android:layout_marginLeft="4dp"
       android:layout_marginRight="4dp"
       android:layout_marginBottom="4dp"
       android:hint="@string/hint_add_item" />
   
</LinearLayout>

Advertisement

Answer

There seems to be problem with the way you are referring to the edit text. You need to get it from the view you inflated Please try the below code which adds all functionality in your main activity itself. If you want you can adapt to your case with a separate class:

public void showNoticeDialog() {

public String inputvalue;

LayoutInflater inflater = LayoutInflater.from(this);
final View textenter = inflater.inflate(R.layout.dialog_add, null)
final EditText userinput = (EditText) textenter.findViewById(R.id.item_added); 
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(textenter)
          .setTitle("Your title");
builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {

               @override
               public void onClick(DialogInterface dialog, int id) {        
                   inputvalue =  userinput.getText().toString();                  
                  // Do something with value in inputvalue
               }
           })
           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                 dialog.cancel();

               }
           });      
    AlertDialog dialog = builder.create();
          builder.show();
         }

For alert dialog , you need to inflate an xml, only if you are planning to have multiple views like more than 1 edittext in that dialog. If you plan to have only 1 edittext as in your example, you dont need an xml and you can directly define an edittext and set it as the view of the alert dialog.

final EditText userinput = new EditText(context);
            builder.setView(userinput);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement