Reputation: 41
I've created a dialog using a custom DialogFragment and interface as well as a custom layout that passes the click events back to the dialog host (MainActivity). However my problem is that when I attempt to get the text entered in the EditText in the dialog, it comes back as an empty string. Any ideas? I am able to get the text from the TextView's within the dialog, just not the EditText.
add_class_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/viewClassName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Class: "
android:textSize="25sp"
android:textColor="@color/textColor"/>
<EditText
android:id="@+id/editClassName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter class"
android:textSize="25sp"
android:textColor="@color/textColor"
android:layout_gravity="center"/>
</LinearLayout>
</LinearLayout>
MainActivity.java:
public class MainActivity extends FragmentActivity implements AddClassDialogFragment.AddClassDialogListener {
TextView nameView;
EditText editName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View vi = getLayoutInflater().inflate(R.layout.add_class_dialog, null);
nameView = vi.findViewById(R.id.viewClassName);
editName = vi.findViewById(R.id.editClassCredits);
}
public void addClass(View view) {
AddClassDialogFragment acdf = new AddClassDialogFragment();
acdf.show(getFragmentManager(), "ac");
}
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
Toast.makeText(this, nameView.getText().toString(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, editName.getText().toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onDialogNegativeClick(DialogFragment dialog) {
Toast.makeText(this, "Cancel", Toast.LENGTH_SHORT).show();
}
}
AddClassDialogFragment.java:
public class AddClassDialogFragment extends DialogFragment {
public interface AddClassDialogListener {
void onDialogPositiveClick(DialogFragment dialog);
void onDialogNegativeClick(DialogFragment dialog);
}
AddClassDialogListener mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (AddClassDialogListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.add_class_dialog, null));
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onDialogPositiveClick(AddClassDialogFragment.this);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onDialogNegativeClick(AddClassDialogFragment.this);
}
});
return builder.create();
}
}
Upvotes: 0
Views: 80
Reputation: 739
The reason you are getting the value of TextView is because you have hardcoded it in your layout. android:text="Class: "
There are a couple of things wrong with your code. If the layout is for the dialog then you should not be inflating it in the Activity. Here's how you'd want to implement it.
You don't need to inflate the layout in the Activity. The layout should be inflated in the Dialog and the Activity should have no knowledge of what that layout has or does. Whatever the Activity needs to know should come through the callback.
package com.soulpatch.stackoverflow;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends FragmentActivity implements AddClassDialogFragment.AddClassDialogListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final AddClassDialogFragment acdf = new AddClassDialogFragment();
acdf.show(getSupportFragmentManager(), "abc");
}
@Override
public void onDialogPositiveClick(TextView textView, EditText editText) {
Toast.makeText(this, textView.getText(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, editText.getText() == null ? "" : editText.getText().toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onDialogNegativeClick(TextView textView, EditText editText) {
Toast.makeText(this, textView.getText(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, editText.getText() == null ? "" : editText.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
And the DialogFragment will define what needs to go back to the activity through the interface you declare here. I have changed it to take a TextView and an EditText. You may change it as per your requirement.
package com.soulpatch.stackoverflow;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class AddClassDialogFragment extends DialogFragment {
public interface AddClassDialogListener {
void onDialogPositiveClick(TextView textView, EditText editText);
void onDialogNegativeClick(TextView textView, EditText editText);
}
AddClassDialogListener mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (AddClassDialogListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final LayoutInflater inflater = getActivity().getLayoutInflater();
final LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.add_class_dialog, null);
final TextView textView = linearLayout.findViewById(R.id.viewClassName);
final EditText editText = linearLayout.findViewById(R.id.editClassName);
builder.setView(linearLayout);
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onDialogPositiveClick(textView, editText);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onDialogNegativeClick(textView, editText);
}
});
return builder.create();
}
}
Upvotes: 1
Reputation: 116
You just referred wrong id for your Edittext .
public class MainActivity extends FragmentActivity implements AddClassDialogFragment.AddClassDialogListener {
TextView nameView;
EditText editName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View vi = getLayoutInflater().inflate(R.layout.add_class_dialog, null);
nameView = vi.findViewById(R.id.viewClassName);
editName = vi.findViewById(R.id.editClassName);
}
public void addClass(View view) {
AddClassDialogFragment acdf = new AddClassDialogFragment();
acdf.show(getFragmentManager(), "ac");
}
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
Toast.makeText(this, nameView.getText().toString(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, editName.getText().toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onDialogNegativeClick(DialogFragment dialog) {
Toast.makeText(this, "Cancel", Toast.LENGTH_SHORT).show();
}
}
Upvotes: 0