Skip to content

Instantly share code, notes, and snippets.

@mitchtabian
Created December 11, 2017 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitchtabian/5bfb8efebacbc792fb794f10f9172fc3 to your computer and use it in GitHub Desktop.
Save mitchtabian/5bfb8efebacbc792fb794f10f9172fc3 to your computer and use it in GitHub Desktop.
package codingwithmitch.com.dialogfragmentfragment;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
/**
* Created by User on 12/10/2017.
*/
public class MyCustomDialog extends DialogFragment {
private static final String TAG = "MyCustomDialog";
public interface OnInputSelected{
void sendInput(String input);
}
public OnInputSelected mOnInputSelected;
//widgets
private EditText mInput;
private TextView mActionOk, mActionCancel;
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_my_custom, container, false);
mActionOk = view.findViewById(R.id.action_ok);
mActionCancel = view.findViewById(R.id.action_cancel);
mInput = view.findViewById(R.id.input);
mActionCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: closing dialog");
getDialog().dismiss();
}
});
mActionOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: capturing input.");
String input = mInput.getText().toString();
if(!input.equals("")){
//
// //Easiest way: just set the value.
// MainFragment fragment = (MainFragment) getActivity().getFragmentManager().findFragmentByTag("MainFragment");
// fragment.mInputDisplay.setText(input);
mOnInputSelected.sendInput(input);
}
getDialog().dismiss();
}
});
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
mOnInputSelected = (OnInputSelected) getTargetFragment();
}catch (ClassCastException e){
Log.e(TAG, "onAttach: ClassCastException : " + e.getMessage() );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment