Android DialogFragment Example
activity_main.xml
MainActivity.java
import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements PopupModelFragment.DialogListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void openDialog(View v) { FragmentManager fm = getSupportFragmentManager(); PopupModelFragment popupModelFragment = PopupModelFragment.newInstance("Hello!", "How are you!"); popupModelFragment.show(fm, "popup_model_fragment"); popupModelFragment.setDialogListener(this); } @Override public void onConfirm() { Toast.makeText(this, "Message Read!", Toast.LENGTH_SHORT).show(); } }
fragment_popup_dialog.xml
PopupModelFragment.java
import android.app.Dialog; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.TextView; public class PopupModelFragment extends DialogFragment { public static final String TITLE = "dialog_title"; public static final String MESSAGE = "dialog_message"; private DialogListener dialogListener; private TextView tvTitle, tvMessage, tvConfirm; public PopupModelFragment() { } public static PopupModelFragment newInstance(String title, String message) { PopupModelFragment frag = new PopupModelFragment(); Bundle args = new Bundle(); args.putString(TITLE, title); args.putString(MESSAGE, message); frag.setArguments(args); return frag; } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_popup_dialog, container); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); tvTitle = (TextView) view.findViewById(R.id.tv_title); tvMessage = (TextView) view.findViewById(R.id.tv_message); tvConfirm = (TextView) view.findViewById(R.id.tv_confirm); String title = getArguments().getString(TITLE, ""); String message = getArguments().getString(MESSAGE, ""); if (title.isEmpty()) { tvTitle.setVisibility(View.GONE); } else { tvTitle.setText(title); } if (message.isEmpty()) { dismiss(); return; } tvMessage.setText(message); tvConfirm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dismiss(); if (dialogListener != null) { dialogListener.onConfirm(); } } }); } @Override public void onStart() { super.onStart(); } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Dialog dialog = super.onCreateDialog(savedInstanceState); dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); return dialog; } public void setDialogListener(DialogListener listener) { this.dialogListener = listener; } public interface DialogListener { void onConfirm(); } }
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts