Adding positive / negative Button to DialogFragment’s Dialog

This is how I figured it out. I erased the onCreateView and altered the onCreateDialog. This link actually had the answer so all the credit should go there. I’ve just posted it just in case anyone bumps in this question first. @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder b= new AlertDialog.Builder(getActivity()) .setTitle(“Enter Players”) .setPositiveButton(“OK”, new … Read more

Prevent DialogFragment from dismissing when button is clicked

Override the default button handlers in OnStart() to do this. @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(“Test for preventing dialog close”); builder.setPositiveButton(“Test”, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Do nothing here because we override this button later to change the close behaviour. //However, we still … Read more

ActionBar in a DialogFragment

using the idea from a google group post I was able to pull it off styling an activity. you would want to modify the height and width to a “dynamic” size of your choice preferably. Then set whatever ActionBar buttons you would like <style name=”PopupTheme” parent=”android:Theme.Holo.Light.Dialog”> <item name=”android:windowIsFloating”>false</item> <item name=”android:windowContentOverlay”>@null</item> <item name=”android:windowSoftInputMode”>stateAlwaysHidden</item> <item name=”android:windowActionModeOverlay”>true</item> <item … Read more

How to change the background color around a DialogFragment?

I had to set android:windowIsFloating to false and android:windowBackground to my custom color in the dialog style: styles.xml <resources xmlns:android=”http://schemas.android.com/apk/res/android”> <style name=”MyDialog” parent=”@android:style/Theme.Dialog”> <item name=”android:windowFrame”>@null</item> <item name=”android:windowBackground”>@color/orange_transparent</item> <item name=”android:windowIsFloating”>false</item> <item name=”android:windowContentOverlay”>@null</item> <item name=”android:windowTitleStyle”>@null</item> <item name=”android:colorBackgroundCacheHint”>@null</item> <item name=”android:windowAnimationStyle”>@android:style/Animation.Dialog</item> <item name=”android:windowSoftInputMode”>stateUnspecified|adjustPan</item> <item name=”android:gravity”>center</item> </style> </resources> MyDialogFragment public class MyDialogFragment extends DialogFragment { @Override public void onCreate(Bundle savedInstanceState) … Read more

Android DialogFragment onViewCreated not called

This is how I make sure onViewCreated is called in kotlin: class MyDialog: DialogFragment() { private lateinit var dialogView: View override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { dialogView = LayoutInflater.from(context).inflate(R.layout.dialog, null) val dialog = MaterialAlertDialogBuilder(context!!) .setView(dialogView) .create() return dialog } // Need to return the view here or onViewCreated won’t be called by DialogFragment, sigh override … Read more

BottomSheetDialogFragment – listen to dismissed by user event

Although all similar questions on SO suggest using onDismiss I think following is the correct solution: @Override public void onCancel(DialogInterface dialog) { super.onCancel(dialog); Toast.makeText(MainApp.get(), “CANCEL”, Toast.LENGTH_SHORT).show(); } This fires if: * the user presses back * the user presses outside of the dialog This fires NOT: * on screen rotation and activity recreation Solution Combine … Read more

AlertDialog with custom view: Resize to wrap the view’s content

I have a working solution that to be honest, I think digs way too deep to obtain such a simple result. But here it is: What exactly is happening: By opening the Dialog layout with the Hierarchy Viewer, I was able to examine the entire layout of the AlertDialog and what exactly what was going … Read more