Search code examples
androidandroid-edittextandroid-alertdialogandroid-dialogfragment

How to display, on MainActivity, the number inserted on a custom alertDialogFragment with EditText?


I'm using a custom alertdialog with an editText to insert a number, the alertdialog is working, but nothing happens when I click positive button, it should display the value inserted, in main_activity.xml.

It seems that my interface is not working. I don't know what I'm doing wrong, here is my code:

TestAlertDialogFragment.java

public class TestAlertDialogFragment extends DialogFragment {

// Use this instance of the interface to deliver action events

TestAlertDialogListener mListener;

public interface TestAlertDialogListener {
    public void onDialogPositiveClick(DialogFragment dialog, int i, String string);

    public void onDialogNegativeClick(DialogFragment dialog);
}


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction


    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();


    builder.setView(inflater.inflate(R.layout.fragment_test_dialog, null))

            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {

                    final EditText editText = (EditText) getDialog().findViewById(R.id.number);
                    int i = 0;
                    String string;
                    if (editText != null) {
                        string = editText.getText().toString();
                        i = Integer.parseInt(string);

                    }
                    if (mListener != null) {
                        mListener.onDialogPositiveClick(TestAlertDialogFragment.this, i, string);
                    }
                }
            })

            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                    if (mListener != null) {
                        mListener.onDialogNegativeClick(TestAlertDialogFragment.this);
                    }
                }
            });
    // Create the AlertDialog object and return it
    AlertDialog dialog = builder.create();

    // makes the soft-keyboard shows when edittext is focused
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

    return dialog;
}
}

MainActivity.java

public class MainActivity extends FragmentActivity
        implements TestAlertDialogFragment.TestAlertDialogListener {
int i=0;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);


public void showTestAlertDialog(View v) {
    // Create an instance of the dialog fragment and show it

    DialogFragment d = new TestAlertDialogFragment();
    d.show(getSupportFragmentManager(), "testAlertDialog");
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

}

public void setSelectedNumber(String string, int i) {
    TextView textView = (TextView) findViewById(R.id.test_number);
    textView.setText(string);
    TextView textView1 = (TextView) findViewById(R.id.test_string);
    String string2 = i;
    textView1.setText(string2);
}


@Override
    public void onDialogPositiveClick(DialogFragment dialog, int i, String string) {
        // User touched the dialog's positive button
        setSelectedNumber(string, i);
    }

    @Override
    public void onDialogNegativeClick(DialogFragment dialog) {
        // User touched the dialog's negative button

    }
}

activity_main.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Input Number"
    android:layout_marginTop="20dp"
    android:id="@+id/button"
    android:onClick="showTestAlertDialog"
    android:layout_gravity="center_horizontal" />

<TextView
    android:id="@+id/test_number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="20dp"
    android:textAppearance="?android:attr/textAppearanceMedium"/>

<TextView
    android:id="@+id/test_string"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="20dp"
    android:textAppearance="?android:attr/textAppearanceMedium"/>

</LinearLayout>

fragment_alert_dialog.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.example.android.datepickertest.InserirPrazoDialogFragment">

<!-- TODO: Update blank fragment layout -->

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="INSERT NUMBER:"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:paddingTop="5dp"
    android:textColor="#40C4FF"
    android:textStyle="bold" />

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginLeft="3dp"
    android:layout_marginRight="3dp"
    android:background="#40C4FF"/>


<EditText
    android:id="@+id/number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:ellipsize="middle"
    android:focusable="true"
    android:padding="10dp"
    android:layout_marginBottom="5dp"
    android:textSize="25dp"
    android:ems="2"
    android:inputType="number" />

</LinearLayout>

Solution

  • You are not setting listener TestAlertDialogListener in your Activity. Just make below changes and you will get it working.

    Create a constructor in your TestAlertDialogFragment which will set the listener for us. I am not pasting the whole code of the dialog, only essential fragments. The onCreateDialog method will be as it was.

    public class TestAlertDialogFragment extends DialogFragment {
    
    // Use this instance of the interface to deliver action events
    
    TestAlertDialogListener mListener;
    
    public TestAlertDialogFragment(TestAlertDialogListener mListener)
    {
    
        this.mListener=mListener;
    
    }
    
    public interface TestAlertDialogListener {
        public void onDialogPositiveClick(DialogFragment dialog, int i, String string);
    
        public void onDialogNegativeClick(DialogFragment dialog);
    }
    

    Change your activity code like this:

    public void showTestAlertDialog(View v) {
    // Create an instance of the dialog fragment and show it
    
    DialogFragment d = new TestAlertDialogFragment(MainActivity.this);
    d.show(getSupportFragmentManager(), "testAlertDialog");
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    
    }