How to hide the soft keyboard after hitting the search dialog box - android

I'm trying to use Android's built-in search dialog, and it's working fine except when I try to use it with a ProgressDialog. The basic chain of events it this:
User press the search button, enters a query and submits it.
The SearchManager calls the search Activity's onCreate method.
In the onCreate method, I call an AsyncTask that runs a query and shows a ProgressDialog in the onPreExecute method and hides it in the onPostExecute method.
This all happens fine except as soon as I submit the query the screen looks like this:
... which is pretty ugly. How can I prevent this from happening?

I dont see the image, but i think it must be the softkeyboard sitting there. It can be removed from the code by calling:
((InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(searchField.getWindowToken(), 0);
This should be called before the dialog is made and shown.

Related

Android onWindowFocusChanged called to early

In my activity needs to download an image from a webservice, therefore I wrote an updateUi method which get called in onCreate() to hide the ui elements (because of missing data at this time) and to show a simple TextView with a message like "data loading...".
Then I use the onWindowFocusChanged with a loaded boolean to call it only one time. In the onWindowFocusChanged I download all data I need from the webservice and call updateUi again to show the original ui and hide the "data loading" TextView.
The goal is to immediately open the new activity after the user pressed a button instead of hidden processing while the user don't know what's happening.
I have like 10 other activities where this method works very well without any trouble, but in this particular activity it seems like onWindowFocusChanged get called before the new activity shows up. If I click on the button to open the activity nothing happens on the display for some secs and then the activity starts with the original ui with all data filled in.
I don't understand why the activity doesn't show up with the "data loading.." TextView instead of doing the tasks in onWindowFocusChanged while the activity didn't show up already?
The code is the same like in my other activities, does someone have any experience with this issue?
I have noticed, that the problem doesnt exists in the first creation of the activity. if i click on a button which start the activity the first time the textview with the loading message is showing and the other ui elements are hidden.
if i leave the activity an start it again the problem occurs...

Halting an activity until I get result from an AlertDialog

Here is my problem - I want to create an activity and a dialog (with text field and OK button). I want to do the following:
Show the AlertDialog (on creation of the activity, on clicking a button, or some other actions);
Fill the text in the AlertDialog, and click the OK button;
Continue doing the main thread of the activity;
Something like this:
public String getText() {
String result = null;
// Showing the new window with the text box and the button, and after
// the button is clicked to move to the return statement below;
return result;
}
I tried using "runOnUiThread" and "AsyncTask", but the actions on the field "result" are done only in the "protected void onPostExecute(Void result)" method, and meanwhile the main program is still executing, without waiting for my input.
Is there anyway this to be done (I am sure it has, because saw such apps) to solve this problem? I know that is really impudent to ask for such help - but is is possible to write some code example, just to see how it is really happening, because more than 3 days, I can' do it. If not please give some suggestions, and will continue trying and trying :)
THANKS A LOT!
Whatever the operations u want to perform after click ,write in asynctask .When dialog button clicked execute the asynctask.Thats it
Well that's pretty much how dialogs work anyway.
http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
At the bottom it shows how to create a custom dialog.
However, a lot of the time the text you need to take in is also displayed on the activity anyway, could you not embed an EditText on your activity and simplify this task?

Android - entries in dialog should be deleted when dialog is closed

I have a customDialog with input fields. I want possible entries to be removed once the dialog is closed (either via back or when a certain button is pressed), i.e. the state should not be saved.
How can I do that?
If the back button is pressed means that dialog is canceled. Implement DialogInterface.OnCancelListener for your dialog and empty/delete/null the entries you want.
I think you might be getting another problem.
Say that you have shown a dialog which was dismissed. If the same dialog is going to be shown a second time, it won't be rebuilt. It will just be shown again.
This means that if you setup your dialog in the onCreateDialog method, the second time that the dialog is shown, this method isn't invoked! Instead, onPrepareDialog is invoked.
Alternatives? You can call Activity.removeDialog or take care of the setup process in the onPrepareDialog hook.

Android Activity.startActivity() and Dialog.show() order

I need to show a EULA like dialog when a user starts up my app, but my app also has a splash screen of sorts that needs to be displayed before the dialog. I coded this up like so:
Activity A.onCreate(...) {
...
1. startActivity(Activity B) [this activity calls .finish() after x seconds]
2. dialog.show()
...
}
Logically, it seems like this should work. However, after I return to activity A from activity B, the entire screen is dimmed as if the dialog is showing, but without the dialog window.
I was able to work around this by reversing the calls as such and the dialog shows after activity B is done and no weird issues occur:
Activity A.onCreate(...) {
...
1. dialog.show()
2. startActivity(Activity B) [this activity calls .finish() after x seconds]
...
}
Anyone know why the order of calls is so important? To me they should work the same.
Both of those functions are asynchronous functions. This means that the code execution does not pause on them, but continues on to call the next lines.
Instead of calling them one right after the other, you need to wait for the one to return before calling the next. If you want to show the Dialog first, add an onDismissListener to listen for when the dialog is closed. Within that listener you can start the next activity.

How to handle feedback from background threads if the user uses the back button?

I have the following problem:
I have an Activity where a user can start a web search showing a new activity to show a progress bar until the results are shown. Now the user can either wait for the results or maybe think about the search parameters, hit the back button and triggering a new search. The search is running in an Async Task and therefor still running if the user hits back.
At the moment the thread finishes it calls some methods on the old activity causing the activity to show a dialog.
This causes the system to crash because the dialog tries to show itself with a reference to an activity that is not longer present on the screen.
How can I achieve a dialog that is only shown if the activity is still active?
Call isFinishing() on your activity.

Categories

Resources