As per the standrad recomendation we should use DialogFragment instead of common dialog for showing message on dialog window.
A fragment display dialog window on top of it's activity window.
For more information read DialogFragment.
Here I am going to create dialog which will be the common for whole application.
A fragment display dialog window on top of it's activity window.
For more information read DialogFragment.
Here I am going to create dialog which will be the common for whole application.
MainActivity.java
import android.app.DialogFragment; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; public class MainActivity extends AppCompatActivity implements CustomDialogFragment.DialogCallBack { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btnOnClick).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDialog(); } }); } void showDialog() { DialogFragment newFragment = CustomDialogFragment.newInstance( R.string.title, R.string.message); newFragment.onAttach(this); newFragment.show(getFragmentManager(), "dialog"); } @Override public void dialogPositiveCallBack() { Log.i("FragmentAlertDialog", "Positive click!"); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="activity.tcp.com.dialogfragment.MainActivity"> <Button android:layout_width="wrap_content" android:id="@+id/btnOnClick" android:text="Click Me" android:onClick="submit" android:layout_height="wrap_content" /> </RelativeLayout>
CustomDialogFragment.java
import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; import android.content.DialogInterface; import android.os.Bundle; public class CustomDialogFragment extends DialogFragment { private DialogCallBack mDialogCallBack; public CustomDialogFragment() { } public static CustomDialogFragment newInstance(int title, int message) { CustomDialogFragment frag = new CustomDialogFragment(); Bundle args = new Bundle(); args.putInt("title", title); args.putInt("message", message); frag.setArguments(args); return frag; } public interface DialogCallBack { void dialogPositiveCallBack(); } @Override public void onAttach(Activity activity) { super.onAttach(activity); mDialogCallBack = (DialogCallBack) activity; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Bundle args = getArguments(); int title = args.getInt("title", 0); int message = args.getInt("message", 0); return new AlertDialog.Builder(getActivity()) .setTitle(title) .setMessage(message) .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (mDialogCallBack != null) { mDialogCallBack.dialogPositiveCallBack(); } } }) .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }) .create(); } }
In the same way you can create negative callback, interface given below.
public interface DialogCallBack { void dialogPositiveCallBack(); void dialogNegativeCallBack(); }
please comment if anything need to support.
No comments:
Post a Comment