0

The image below is essentially what I want to achieve, this comes straight out of eclipse as my dialog_layout.xml

I want to inflate a custom dialog and have it on the right of screen to serve as a menu. Everytime, I try to show this DialogFragment, the layout below centres itself to the centre of the screen. (The code for the onCreateDialog() of the `DialogFragment1 is below the image.

Is there any way to achieve this? Do I have to create an Activity using a dialog theme?

Desired dialog

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    // Create a new Dialog using the AlertDialog Builder
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    View background = getActivity().getLayoutInflater().inflate(R.layout.dialog_layout, null);

    builder.setView(background);        

    return builder.create();
}

Any help would be appreciated!

Cheers

mgibson
  • 6,103
  • 4
  • 34
  • 49

1 Answers1

2

Success!

I used a DialogFragment to produce a custom menu dialog in the top right of the screen.

enter image description here

Reasoning:

  • Needed big buttons therefore standard menu was too small.
  • Standard menu is a hassle to inflate from a custom menu button.
  • I wanted a fairly slim dialog in the top right, same position as the users thumb when pressing the menu button. This is the same logic as most menus.

Problems faced:

  • Unwanted black border around my dialog || Solution: dialog.setView(dialogLayout, 0, 0, 0, 0);
  • Dialog was always centred || Solution: Changed the WindowParams.
  • Dialog occupied the majority of the screen even though the XML design can be seen at the very start || Solution: Apply a custom transparent windowBackground theme

Code:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    // Create a new Dialog and apply a transparent window background style
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.dialog_theme);                 
    AlertDialog dialog = builder.create();

    // Create the custom dialog layout and set view with initial spacing parameters to prevent black background
    View dialogLayout = getActivity().getLayoutInflater().inflate(R.layout.dialog_fragment_menu, null);
    dialog.setView(dialogLayout, 0, 0, 0, 0);

    // Change the standard gravity of the dialog to Top | Right.
    WindowManager.LayoutParams wlmp = dialog.getWindow().getAttributes();       
    wlmp.gravity = Gravity.TOP | Gravity.RIGHT;

    return dialog;
}

The code for my style is simply:

<style name="dialog_theme" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
</style> 
mgibson
  • 6,103
  • 4
  • 34
  • 49