When I press the search key on my device, I want it to show a white background. However, when I press the back button, I want the previous activity's background to be restored. ivBackground is a variable I added to my relativelayout which I turn VISIBLE to show the white background.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH) {
ivBackground.setVisibility(View.VISIBLE);//WHITE IMAGEVIEW
return false;
} else if (keyCode == KeyEvent.KEYCODE_BACK) {
ivBackground.setVisibility(View.GONE);
return true;
}
return super.onKeyDown(keyCode, event);
}
While the above code works, the problem is that when I press the back button, the white screen still remains. It only goes away if I press the back button once again. Any solutions?
On the relevant activity, you can get a reference to a SearchManager object. On this, you can set an OnDismissListener, which is called the when search UI is dismissed e.g.
this.searchMgr = (SearchManager)this.getSystemService(Context.SEARCH_SERVICE);
this.searchMgr.setOnDismissListener(new OnDismissListener() {
public void onDismiss() {
ivBackground.setVisibility(View.GONE);
}
});
To make the white background visible, you can override onSearchRequested inside your activity class, which is called when a user signals the desire to start a search
#Override
public boolean onSearchRequested() {
ivBackground.setVisibility(View.VISIBLE);
return super.onSearchRequested();
}
Hope this helps!
How about setting a SearchView.OnCloseListener or a SearchManager.OnDismissListener that also hides your view? This seems like the right solution anyway since the two events are logically connected. If you later dismiss the search view programmatically for example, you don't have to worry about separately dismissing the background view.
Related
I have a basic question about android development. In my app i have a autocomplete box. When user click on a menu button, autocomplete become visible. When user clicks on an entry in autocomplete, the text views show it. I want to use back button such that when user presses it . Textview become invisible , on again click autocomplte disappears and than on third click app exits. But in my app on a single click app exits. I am a new to development so doesn't know much about it, so i ask here.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
//stuff
}
return super.onKeyDown(keyCode, event);
}
Since API 5 :
#Override
public void onBackPressed() {
//stuff
}
I want to get a pressed character in the code without using edittext.
Simply a black screen appear if someone presses "a" or "b" etc.. I want to get this value in my code.
I have done it using editext and textwatcher but the requirement is we don't need to use them now.
Can we make character(alphabet) moving from top to bottom randomly
I have followed some links which have used marquee but it does not work.
In your Activity override the method
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_A) {
//Here your black screen if you press A
return true;
}
return super.onKeyDown(keyCode, event);
}
Check this KeyEvent ;
I was wondering if there is any way to be notified automatically by android when the on-screen keyboard is shown and when it disappears.
For example when we click on a edittext, the ime appears. Will there be any event calls?
And when it disappears when we press back, similarly will there be any even calls?
I found this thread Android: which event fires when on screen keyboard appears? , however no answers have been reached yet.
The purpose is because i need an event to automatically manipulate visibility. I have an activity with an edittext on the top of the screen, below it, a listview and a linearlayout which are sitting on top of each other. To control what the user sees, i manipulate the visibility. By default, the linearlayout is shown initially, however, when the user is entering text, the listview should be shown instead. The listview should disappear when the user has finished typing, which in this case, the on-screen-keyboard will be closed.
I tried acomplishing the change of visibility using onFocusChange, however, even when the on-screen keyboard disappears, the edittext still retains focus and the linearlayout never reappears.
Below is my implementation of the onFocusChange
#Override
public void onFocusChange(View v, boolean hasFocus)
{
if(v.getId()==R.id.search_screen_keyword_textbox)
{
if(hasFocus)
{
filterSection.setVisibility(View.GONE);
autoComSection.setVisibility(View.VISIBLE);
}
else
{
filterSection.setVisibility(View.VISIBLE);
autoComSection.setVisibility(View.GONE);
}
}
else if(v.getId()==R.id.search_screen_location_textbox)
{
if(hasFocus)
{
filterSection.setVisibility(View.GONE);
autoComSection.setVisibility(View.VISIBLE);
}
else
{
filterSection.setVisibility(View.VISIBLE);
autoComSection.setVisibility(View.GONE);
}
}
else
{
filterSection.setVisibility(View.VISIBLE);
autoComSection.setVisibility(View.GONE);
}
}
If anyone has any idea about it do let me know. :D
You can catch the back button when in an edittext, this is what would make the keyboard disappear. Using this method:
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
// Do your thing here
return false;
}
return super.dispatchKeyEvent(event);
}
Search is great: onKeyPreIme or Android API
It seems that this thread has a solution using onConfigurationChanged: How to capture the "virtual keyboard show/hide" event in Android?
How do I have an app return to the 'Home' screen when the user is on a different class and they click the back/return button on their phone?
Right now, when they access a different class and hit the back button, it goes back to the Home screen on the Droid rather than the Home screen on the app - how do I get it to go back to the Home Screen on the app?
Thanks!
Use OnBackPressed() method of Activity class.
Make conditions like :
public void onBackPressed(){
if(conditon)
{
detailInfoList.setVisibility(View.VISIBLE);//or anything that you want to show
}
}
you can't,
the home button is the only you can't prevent form closing your app. Its done so that you can't block the user inside your applications (like a virus).
the other keys like back menu and research are possible
in your activity
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
if(keyCode == 82){
Log.i("jason","released on menu");
//change view
//return so that the other behavior (leaving the activity) is not tirggered
return false;
}
}
Log.i("jason","actual key code "+keyCode);
return super.onKeyDown(keyCode, event);
}
I am using following code to start activity when user pressing search button on the handset
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_SEARCH){
Util.startActivity(ReviewsDetail.this, KeywordSearch.class);
return false;
}else{
return super.onKeyUp(keyCode, event);
}
}
But here are few issues with it please look at the following image.
When press search button it first show google search box at the top of activity then start activity which i want to start
When click on the back button displays empty actiivty
#Override
public boolean onSearchRequested() {
// your logic here
return false; // don't go ahead and show the search box
}
The Search button and system's search request are both working the same when invoked from any activity of your app. If you want to override it you will have to override it for EVERY activity you want it to work from in the same way. Unfortunately, there is no way to override it "globally", neither a way to subclass/style/theme the default search popup. So sad, google.