Custom dialog implementation.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="@android:color/holo_blue_light"
android:layout_height="100dp">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
// Implement the listener (interface)
CustomDialog questionDialog = new CustomDialog();
questionDialog.show(getFragmentManager(), "questions_dialog");
public class CustomDialog extends AppCompatDialogFragment {
LayoutInflater inflater;
View view;
SampleListener listener;
@Override
public void onAttach(Context context) {
super.onAttach(context);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
listener = (SampleListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement SampleListener");
}
}
public interface SampleListener{
public void listener(String dataToPass);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
inflater = getActivity().getLayoutInflater();
view = inflater.inflate(R.layout.custom_dialog_layout, null);
// Extract
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view).setIcon(R.drawable.ic_question_black_24dp).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Toast.makeText(getActivity(), "Ask", Toast.LENGTH_SHORT).show();
handler.questionAdded(editTextQuestion.getText().toString());
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Handle negative click
}
});
return builder.create();
}
}