how to implement popup window without any actions performed in android? - android

I want to show up pop up window after an activity has been launched. It is just like after a few seconds delay pop up should come. How can I implement that?Any ideas or examples?? If so I will be helpful ..Thanks in advance.

Many ways to do so.
If you have a list of Activities where you want to show the POP-up, you can uses two ways:
Create a service which actively checks for the foreground [Top most activities] in the stack and if the activity on which you want to show the pop-up, just send the broadcast to show the pop-up.
Create a Class which extends a Asynctask class, which waits for Xsecs in doinbackground and shows a pop-up over onpostexecute, Just call the execute of the Asynctask class while oncreate ends if you want to show only one time.
Asynctask class will be the best to use in such scenario. By this you can re-use this asynctask class every where in the project.

new Handler().postDelayed(new Runnable(){ #Override
public void run() {
// This method will be executed once the timer is over
//call your toast here.
//TIME_OUT is no of milliseconds you want to wait before Toast is popped.
} },TIME_OUT);
Hope this helps.

Related

Several simultaneous AsyncTasks from different Activities - how to catch proper response?

I have several Activities and each of them has its own AsyncTask which sends requests to a server and catches its responses. These days if AsyncTask is in execution I have a ProgressDialog which blocks User from navigation to another Activities. I want to get rid of the ProgressDialog and substitute it with ProgressBar view so user can switch between Activities. My concern is about the following: what if AsyncTaskFirst started in ActivityFirst and user navigates to ActivitySecond where AsyncTaskSecond is starting also, couldn't it happen the response from the first request will take ground in ActivitySecond so I'll miss it in ActivityFirst? If it is impossible, that's fine. If it is - how to handle such a case? Thank you very much in advance.
If the AsyncTask begun execution in the first Activity then it's execution of the postExecute method should be expected to affect the first Activity and not the second unless you've explicitly set it up otherwise.
I assume your AsyncTask has some kind of reference to the Activity, so it can update the progress bar.
If this is so, then all you have to do is add a method in the AsyncTask:
public void setHandler(MyTaskHandler handler) {
this.handler = handler;
}
When you start a new activity, call this method and pass on the new activity.
Then when the AsyncTask calls the Activity's update progress method, it will use the current Activity regardless of where the task was started from.
BTW: It might be better to use fragments and not activities, if it's the same component only different parts of the display. Then you do not have this problem at all, because the activity remains the same. See these guides about fragments:
http://developer.android.com/training/basics/fragments/index.html
http://developer.android.com/guide/components/fragments.html

Show Dialog everywhere if I don't know the current context?

I have to show a Dialog in the foreground activity as a result of a background http operation.
When the dialog has to come up it could be everywhere, the context can be changed, for example I have started a new Activity.
If I use the applicationContext to show the dialog I get:
05-04 17:32:32.560: E/AndroidRuntime(3663):
android.view.WindowManager$BadTokenException: Unable to add window --
token null is not for an application
so... How can I achieve my aim?
any suggestions?
Whenever/wherever you create the dialog you would be in an activity right? Why not just use that activity as the context?
In my own code, I create a helper class that creates a dialog for me. Into that helper class, I pass in the current activity, title and message. It constructs the dialog and returns an AlertDialog object which I can manage.
You could try that, but you would still need to know the context/activity where you want your dialog to display.
You need a way to notify your foreground Activity that the operation is complete, you can do this by registering a listener, since you have not posted any code, I will make assumptions.
There are two ways you can notify a foreground Activity that I know of, the first way is using broadcast intents, there is a question here relating to them Android BroadcastReceiver within Activity. You can fire of a broadcast intent from your background operation and register your activity as a receiver.
See here http://developer.android.com/reference/android/content/Context.html#sendOrderedBroadcast%28android.content.Intent,%20java.lang.String,%20android.content.BroadcastReceiver,%20android.os.Handler,%20int,%20java.lang.String,%20android.os.Bundle%29 and here
http://developer.android.com/reference/android/content/BroadcastReceiver.html
The second way is to register a listener with your class that performs the background operation, for instance (pseudo code)
#Override
protected void onResume() {
BackgroundOperator.registerListener(this);
}
#Override
protected void onPause() {
BackgroundOperator.unregisterListener(this);
}
public void onOperationComplete(...) {
// TODO: Show your dialog here
}
Where your listener could be something like this (which your Activity could implement):
interface BackgroundOperatorListener {
void onOperationComplete(...);
}
The idea here is your foreground activity will be the current registered listener, so it will be the recipient of the onOperationComplete(...) callback, and you can then show your dialog, the ... could be any number of arguments to pass to your activity when the operation is complete.
I think what you need is get the top activity of the task(current showing activity), then use it to show dialog.
so see this thread :How to get any identifier of the topmost activity?
Edit: Showing a dialog from background is not a good user experience, you can send notification or just make a long time toast.
In my opinion the best way is creating one new activity and using it like a dialog.
The steps:
Create new activity , for example (to being original) MainActivy.
Fill the activity_main.xml asociated as you need.
In the your AndroidManifest.xml re-write this lines.
<activity
android:theme="#style/AppTheme.Dialog"
android:name="com.myapp.original.example"
android:label="#string/timy_titlle" >
Call your MainActivity, now converted to Dialog, from others activies using the Intent class.
If you are using Action Bar Compact you can follow this others steps.

Android : How to show custom dialog from thread

I stack with this, I'm trying to create application using TabLayout. I have 3 tabs (3 different activities). I also got service which reads my gps position - that works fine. I got thread which post to the web server user position - that works to.
Now I'm trying to create thread which ask web server if there are any messages for user. If there are - thread try to show custom dialog (2 text views and gallery - it works if i start action from activity by clicking button), and there problem starts I know i can't update UI form threads different from main thread but i don't know how to work around this. I tried to use AsyncTask - failed - i don't want to assign AsyncTask to specific Activity as inner private class.
I wish I could show my custom dialog regardless of the tab on which user is currently using.
Thread works fine, but how to show that dialog.
I read about runOnUIThread but don't know how to use it.
Any ideas, any similar examples ??
Using runOnUiThread is easy.
YourActivity.this.runOnUiThread(new Runnable() {
public void run() {
YourActivity.this.showDialog(CUSTOM_DIALOG_ID);
}
}
You need to use a Handler. http://developer.android.com/reference/android/os/Handler.html

Android: alert box after 3000 ms

In my program I want this alert dialog to show after 3000ms. how can I do this? I tried a lot but I couldnt. any Idea?
Help is always appreciated...!
AlertDialog.Builder successfullyLogin = new Builder(Register.this);
successfullyLogin.setCancelable(false);
successfullyLogin.setMessage("Successfully Login !");
// successfullyLogin.wait(3000);// this line is nt working
successfullyLogin.setPositiveButton("Ok",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
}
});
I think the wait function you are calling should be specifically used for multithreading...
try this...
new Thread()
{
public void run()
{
sleep(3000);
AlertDialog.Builder successfullyLogin = new Builder(LWM.this);
successfullyLogin.setCancelable(false);
successfullyLogin.setMessage("Successfully Login !").show();
}
};
The wait method is part of java.lang.Object and causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the specified timeout expires. It's not used to implement "sleep" like functionality.
You could start an AsyncTask (that will start a background thread). In the doBackGround, you could sleep the thread for 3 seconds (not blocking the UI), and in your doPostExecute you can pop the dialog.
Instead of showing an alert box why dont u put an suucess message as a toast for 3 seconds....
or else if u want to show the alert box for 3 seconds first remove the ok button then use handlers for close the alert box...
You could use AsyncTask or Timer to accomplish that. If you use AsyncTask, sleep in the background and show the dialog in onPostExecute
The accepted answer here should give you a good head start. Just substitute toast for dialog and you're done.
How to display toast inside timer?
i.e. Use a timer to create a new thread to count down your 3 seconds, and use a handler to display your dialog or toast message on the main UI thread.
Create a Handler in your Activity's class (can be assigned local variable). Then set it up to send a sendEmptyMessageDelayed() inOnStart(). Then, in your handler, create the alert dialog. Note that, since an Activity can be terminated at any time by Android, you need to also override OnStop()in your Activity and call removeMessages() on your handler. If you don't do this, the message is left in the queue but your Activitiy will have already been terminated when the event fires. The result is a rather confusing Exception.
This approach also has the benefit of being able to terminate the message from firing in the first place. For instance, if you finish doing whatever needs to be done before then, you can simply remove the message from the queue and it won't fire.

Threads and ProgressDialog

I am developing my first Androïd application and I'm facing a problem when I want to display a ProgressDialog to indicate that a process is being run.
In my application, the user triggers a time consuming task by pressing a Button. The "OnClick" function of my "OnClickListener" is called when the user presses the Button. In this function, here is what I'm currently doing :
- creation and configuration of an instance of the ProgressDialog class,
- creation of a thread dedicated to the time consuming task,
- attempt to display the ProgressDialog using the "show" method,
- start of the thread,
- main Activity suspended (call of the "wait" function)
- wake up of the main Activity by the thread when it is finished
- removal of the ProgressDialog by calling the "dismiss" function.
Everything works fine (the result of the long task is correct) but the ProgressDialog nevers appears. What am I doing wrong?
Thanks in advance for the time you will spend trying to help me.
You should not call wait() to the Main Activity/UI thread, because this is actually freezing the UI including your ProgressDialog, so it has no time to fade in and will never be shown.
Try to use multithreading correctly: http://developer.android.com/resources/articles/painless-threading.html
final Handler transThreadHandler = new Handler();
public void onClick(View v) {
// show ProgressDialog...
new Thread(){
public void run(){
// your second thread
doLargeStuffHere();
transThreadHandler.post(new Runnable(){public void run(){
// back in UI thread
// close ProgressDialog...
}});
}
}.start();
}
I would suggest using AsyncTask, as it's purpose is exactly to handle this kind of problem. See here for instructions how to use it. Note that the linked page in Floern's answer also recommends the use of AsyncTask.
You would need to do the following:
subclass AsyncTask
override it's onPreExecute() method to create and show a ProgressDialog. (You could hold a reference to it in a member of your subclass)
override it's doInBackground() method to execute your time consuming action.
override it's onPostExecute() method to hide your dialog.
in your activity, create an instance of your subclass and call execute() on it.
If you make your subclass an inner class of your activity, you can even use all of your activity's members.

Categories

Resources