Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dialogs and Popups in Android

The Android design documentation in http://developer.android.com/design/building-blocks/dialogs.html makes a clear differentiation between Dialogs, Alerts, Popups and Toasts. It also recommends the implementation of Dialogs by means of the DialogFragment class and Toasts by means of the Toast class. However it's not clear to me whether Popups should be implemented with PopupWindow or with DialogFragment.

I know that DialogFragments usually come with Ok/Cancel buttons and that the location of PopupWindows can be defined, but:

  • Are these slight differences the only arguments to use one or the other?
  • Is DialogFragment the successor of PopupWindow that will be deprecated at some point?
  • According to the answer in https://stackoverflow.com/a/15165554/2482894, PopupWindow is "Limited to a few templates", but I can't find any reference to a limited amount of templates in the class documentation.
  • So, finally, how would you implement Popups like these http://developer.android.com/design/media/dialogs_popups_example.png and why?
like image 856
leo9r Avatar asked Aug 29 '13 23:08

leo9r


People also ask

What are dialogs in android?

A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed. Dialog Design.

What is a dialog popup?

Dialogs are Popup controls without owner elements. They allow users to enter or edit information without switching to a new page or view. They can be modal or modeless, and are usually centered on the screen.

What's the difference between dialog and AlertDialog in android?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .


1 Answers

If you want dialog as shown in the link, just make them by making custom dialog as mentioned below:

Make a dialog object:

Dialog dialog = new Dialog(context,android.R.style.Theme_Translucent_NoTitleBar);

Set custom view to this dialog:

show_dialog(){
    dialog.setContentView(R.layout.custom_dialog);//your custom dialog layout.
}

Your custom layout should be like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:id="@+id/custom_dialog_first_rl"
    android:background="@android:color/black">
<!-- write code for rest of your UI here -->
</RelativeLayout>

Now set alpha for your first relative layout in show_dialog() like this:

show_dialog(){
    dialog.setContentView(R.layout.custom_dialog);//your custom dialog layout.
    RelativeLayout custom_dialog_first_rl=(RelativeLayout)dialog.findViewById(R.id.custom_dialog_first_rl);
        custom_dialog_first_rl.getBackground().setAlpha(170);
}

Call show_dialog() where you wanna show this dialog

like image 176
himanshu munjal Avatar answered Sep 21 '22 06:09

himanshu munjal