Pop-Up display in Android using alertdialog.builder - android

I am new to Android Programming. I need to display a Pop-Up message in my application which I have no clue about. I got to hear from somewhere that it can be done using alertdialog.builder, but I have no idea how to do that?
I just want to display a simple Text Message.
Can anybody please help me out regarding this with an example if possible.

If you want to display a simple message you're probably better off using a Toast.
Toast.makeText(this, "Some Text", Toast.LENGTH_LONG).show();
Or, if you're doing it properly, and using a String resource:
Toast.makeText(this, getString(R.string.message_toast), Toast.LENGTH_LONG).show();

If you really need a pop up that the user has to acknowledge by pressing a button, you can do something like this:
new AlertDialog.Builder(context).setMessage("message text").setPositiveButton("OK", null).setTitle("Title").show();
you can leave out setTitle if you don't need it

Related

Android show newest toast

I'm working on my first Android application and I have a situation that really annoys me.
Lets say that user has to enter some information in few Edit Text fields and can't proceed if there is at least one empty field.
If user clicks a button to proceed and there is at least one empty field, a certain Toast is displayed.
The problem is, if user clicks that button like 10 times, and then enters those missing information and proceeds, those Toast messages keep showing in the new Activity along with Toast for successful registration.
Is there a way to interrupt a currently displayed Toast and display newest or something similar that will stop this kind of behaviour from happening?
edit: I have accepted #Vucko's answer because it suits better for my problem. #Augusto Carmo wrote the answer that indeed works, but I prefer Vucko's answer, it is just more elegant.
The more elegant and common way to do this would be to use:
editText.setError("Your error message");
This will disappear itself once you write something in your editText or you can manually remove it by calling setError(null);.
Have a single Toast object, and when you want to show another toast message, you cancel the last one and show the new one. Example:
Toast toast = new Toast();
toast.makeText(CONTEXT_HERE, MESSAGE_HERE, TIME_HERE).show();
// displaying another
toast.cancel();
toast.makeText(CONTEXT_HERE, ANOTHER_MESSAGE_HERE, TIME_HERE).show();

How to give validation to registration page in phonegap Android like we see in websites?

Hi I have created an registration page I want to give validation to it like the one we normally see in the websites Means if any field is blank it should show it in red with message not like we normally use dialog box or alert can any one tell how to do this.I am not using form to create pages i used div.
Thanks in advance.
you can use javascript to validate your textbox registration like
$('#btnReg').click(function(){
if($('#textField').val().length==0){
// show you massage in div here
// like this
$('#MsgError').html("error msg");
// or use alert
alert("error msg");
}
})

Blocking dialog like construct in android

Looking for a blocking,value returning dialog like custom construct in android.
Something like this:
Output outValue = MyPopupBuildingClass.showWindow(inValue, R.layout.my_layout);
Yes that's the all too familiar MessageBox for Windows programmers :) and I think there is JOptionPane in Swing too.
The complexity here is that I don't want a small default android dialog, but more of a popup fragment with some detailed layout and functionality. It accepts an object, pops up a window, lets user do something to it and return it back when user accepts or dismiss.
Any thoughts?
Like #Luksprog said in comments, Blocking dialogs in android are hard to implement and should be avoided.

Is there a limitation for Dialog's title?

I've created a DialogFragment and used getDialog().setTitle("Application failed to decrypt the file") and the thing I see in the title is "Application failed to". Is there a limitation for a title? How can I put the whole string in it? Thanks.
use an AlertDialog to show a message instead for something this long.

Android SDK keep Toast from fading away

Quick question: Is there a way to display a toast message that doesn't fade away until I call cancel() on it?
I have tried setting the duration to something like 9999 but that doesn't work.
Is there a way to display a toast message that doesn't fade away until I call cancel() on it?
No, not directly from the SDK, but you can "tweak" your Toast to make it live longer by calling show() on it as many times you wish using threads. See this article for more information.
A Toast that doesn't go away until you cancel it is called a Dialog (or AlertDialog). The integer you pass to Toast.setDuration() is a flag - not a value - it will only recognize the values Toast.LENGTH_SHORT and Toast.LENGTH_LONG.
The Toast calss description says:
"A toast is a view containing a quick little message for the user. The
toast class helps you create and show those..."
"...The idea is to be as unobtrusive as possible, while still showing the
user the information you want them to see. Two examples are the volume
control, and the brief message saying that your settings have been
saved..."
As for the duration parameter it should be one of LENGTH_LONG or LENGTH_SHORT - 1 or 0 respectively.
Use a dialogue that looks like a Toast if you really have to, but I don't recommend doing this because this wont be what a user expects from a Toast.
Toast Message works with time.there is no way to control it with the cancel.You have to use Dialog for the kind of purpose
The official doc says (http://developer.android.com/reference/android/widget/Toast.html#makeText(android.content.Context, int, int) ):
public static Toast makeText (Context context, int resId, int duration)
Since: API Level 1
Make a standard toast that just contains a text view with the text from a resource.
Parameters
context The context to use. Usually your Application or Activity object.
resId The resource id of the string resource to use. Can be formatted text.
duration How long to display the message. Either LENGTH_SHORT or LENGTH_LONG
Throws Resources.NotFoundException if the resource can't be found.
This means there is no direct way to do that. You will have to build your custom code for this. As Toasts exacly overlap each other you can call the same Toast every second with a thread as an example, and use a cancel() custom method to terminate that thread.
I know this post is old but for others that come across it you are more than welcome to use a little library I put together called SuperToasts.
You can find the library here.
There is an indeterminate option for Toasts that are added to an Activity namely the SuperActivityToast. I purposely did not add this feature to standard SuperToasts, a class that mimics standard Toasts, because the SuperToast can linger until your application is killed as it is added to the WindowManager and not an Activity. SuperActivityToasts are added to the Activity's content and will be destroyed along with your Activity hence the ability to make them indeterminate.

Categories

Resources