How to Hide Alert Dialog Never Ask Again in Android

Photo by Gary Bendig on Unsplash

This seemingly simple UI chemical element has a lot of subconscious features. If you accept always developed an application, you probably have used it.

An AlertDialog's master function is to notify users of something that is going to happen that requires their immediate attention and an action to be made on their end. The uses of an AlertDialog are numerous and could range from:

  • Ask the user to grant permission for your application to do something (location)
  • Alert the user to an action that is about to accept place (remove photo)
  • Notify the user of what is going on (characteristic not available)

What this article will non embrace is the basic employ cases and construction of an AlertDialog, just it volition cover some more than advanced uses and caveats of this UI element.

For this article, I will be showing examples written in Kotlin .

Length of AlertDialog's Title

Setting the title on an AlertDialog is easy. Using the following method,

            builder.setTitle(title), //where championship is a CharSequence.                      
setTitle

What is non credible from the code, is that the dialog's title is limited to only 2 lines. Meaning, that if you lot write a long title, it will be truncated and you will see three dots (…) being displayed signifying the sentence is longer than information technology appears.

Truncated Title

But what If we needed more room for our title? We tin utilise the method setCustomTitle which receives a View every bit a parameter.

setCustomTitle

Using the to a higher place, we can easily create a TextView and mode information technology to our liking to get the desired result.

An AlertDialog with a custom title

Our Result

The Neutral Push button

While you may be familiar with the approve or reject buttons associated with the AlertDialog, there is also one other blazon of button bachelor. That push button is the NeutralButton. What is special about this push button is that it signals to the user that if he/she makes this pick, nothing volition be affected.

Neutral Button

If you accept another await at the screenshot in a higher place, you can come across that the button with the Dismiss text is a neutral push.

Items Or Message

After setting the title, nosotros can set the bulletin of our alert dialog. Once again, what if we wanted the user to select something in our warning dialog (out of a list)? For that, we tin use the setItems method:

setItems

Or nosotros can apply the setSingleChoiceItems method:

Here, we come to a fork in the route, because nosotros will have to choose what is more important to us: a list of items or the alert dialog'south bulletin. We tin can't have both. To quote the documentation:

…Because the list appears in the dialog'due south content area, the dialog cannot show both a message and a list…

When using an items list, information technology is a good idea to use an adapter to handle the interaction and management of our list of items. Below is a uncomplicated demonstration of just how to reach that.

MainActivity.kt
AlertDialogListAdapter.kt
list_item.xml

List of items in AlertDialog

If you don't want to deal with an adapter, you tin can employ the setSingleChoiceItems method like and then:

Using SetSingleChoiceItems

And you lot'll go the following:

Image Alert

What if we wanted to accept a bulletin and an prototype in our AlertDialog? Nosotros can achieve that by using the setView method:

setView
MainActivity.kt
custom_view.xml

Our Custom View

🙌 In fact, you may have noticed that even for the previous problem, we can utilise a custom view that has a listview and a textview to handle the scenario of a message and a listing of items.

Those are simply some of the more allusive possibilities you have with the AlertDialog. If you take more than examples like this, feel gratuitous to share them in the comments.

You tin see all the examples shown in this article in an awarding I created at this GitHub repository.

dayinflaing.blogspot.com

Source: https://proandroiddev.com/what-you-might-not-know-about-the-alertdialog-2bdc55f3d907

0 Response to "How to Hide Alert Dialog Never Ask Again in Android"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel