Alert dialog và popup window android
Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue. Android AlertDialog is composed of three regions: title, content area and action buttons. Android AlertDialog is the subclass of Dialog class. Methods of AlertDialog classMethodDescriptionpublic AlertDialog.Builder setTitle(CharSequence)This method is used to set the title of AlertDialog.public AlertDialog.Builder setMessage(CharSequence)This method is used to set the message for AlertDialog.public AlertDialog.Builder setIcon(int)This method is used to set the icon over AlertDialog.Let's see a simple example of android alert dialog. activity_main.xmlYou can have multiple components, here we are having only a textview. strings.xmlOptionally, you can store the dialog message and title in the strings.xml file. Activity classLet's write the code to create and show the AlertDialog. Output:Popupwindow is a floating view that is displayed on top of an activity. Android provides a PopupWindow class for creating a popup window with the custom design. It is present from the first versions of the API, but not as popular as other dialog boxes due to the need for additional configuration, which is not always necessary. Android PopupWindow very useful when you want to show the dialog just over the view items. For Example you can able to show the dialog near to the more option of your cardview / button view. Check out my popup window android example demo. Now we are going to create popup window showed in the popupwindow example video. 1. Adding DependenciesIn this example, we are going to show list of items in the recyclerview. So, we need recyclerview and cardview dependencies. dependencies { I have explained about using recyclerview and cardview in separate post. Please check it in the links. Recyclerview Android Example [Beginners] Cardview Android Example [beginners] 2. Create PopupWindow Layouts
We are using recyclerview in the popup window. so, we need model class to hold the data. data class FilterItem(val icon:Int,val name: String) Also, we need to create adpater to set popup items. class AlertFilterAdapter(val context: Context) : RecyclerView.Adapter In the Adapter, set the click listener interface for the popup window click callbacks. interface RecyclerviewCallbacks Already, we have created layouts and adapter for the PopupWindows. Lets create the PopupWindow. private fun showAlertFilter(): PopupWindow { In the above code, I am using adapter.setOnClick to pass the implementation of the RecyclerviewCallbacks interface to receive the callbacks of the popup window items. 4. Show / Hide the PopupWindowNow, our popup window is ready to display. So, whenever we need to show the popup window. call the below code. |