android soft keyboard backspace button not working - android

I want to take control of soft keyboard appering. So I have override EditText class and method onCheckIsTextEditor.
#Override
public boolean onCheckIsTextEditor() {
return MenuActivity.expanded;
}
And after it everything works fine except delete/backspace button. When I press it nothing change. Also when I set onClickListener to my editText i can see that every click except delete fire this listener.
public void setOnBackSpaceListener(){
this.setOnKeyListener((view, i, keyEvent) -> {
Log.d("AAA -> ", String.valueOf(keyEvent.getKeyCode()));
return false;
});
}
So it's look like android think there was no click and I don't know why?

I'm going to make the assumption that you are implementing View.OnKeyListener in your Activity or Fragment. Are you passing your listener to the view you would like to capture the event from?
In this case, it will be:
mEditText.setOnKeyListener(this);

Related

Testing AutoCompleteTextView with Espresso

I am using Espresso to test an app with several AutoCompleteTextViews. For one particular test, the autocomplete popup appears, but I want to just ignore it and move to the next TextView. How can I do this? My ideas are to either simulate a "Back" press or simulate a tap in the popup.
Update:
I am attempting the following to click on the autocomplete popup:
onView(withId(R.id.brand_text))
.perform(scrollTo(), typeText(card.getBrand()));
onData(allOf(is(instanceOf(String.class)), is(card.getBrand())))
.inRoot(isPlatformPopup())
.perform(click());
onView(withId(R.id.brand_text))
.check(matches(withText(card.getBrand())));
Now the problem is that in some cases, the text entered doesn't have any autocomplete matches so no popup appears. How do I conditionally perform the click depending on whether or not a view is matched?
My solution was to add a UiDevice object from UI Automator to my test:
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
Then I call UiDevice.pressEnter() to advance to the next TextView.
device.pressEnter();
The problem encountered is that this doesn't work as expected out of the box. I also added a View.OnKeyListener to each AutoCompleteTextView to handle the Enter key event. For now I add a listener to each view and explicitly state which view requests focus on an Enter key event.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// ...
brandText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_ENTER) {
yearText.requestFocus();
return true;
}
return false;
}
});
//...
}
This isn't ideal. Eventually I plan to generalize this, possibly with a custom component which inherits from AutoCompleteTextView which handles the Enter key as I expect. It will send focus to the "next" view.

How to prevent a key to be inserted in an EditText if it comes from a specific keyboard

i'm working with a bluetooth barcode scanner acting like a bluetooth keyboard, i want to handle the keys when my user scan something but i know he can focus any EditText in the activity so i want to detect which keyboard he uses in my KeyEvent like this :
if KeyEvent.getDevice().getName().equals("Datalogic Scanner") //do stuff
else //let the edittext add the letter
and prevent the EditText to be edited and instead store and process the values the scanner send.
I tried returning true in dispatchKeyEvent in my activity,
I tried returning true in onKeyDown in my activity,
I tried returning true in a OnKeyListener on my EditText and overriding OnKeyDown on my EditText class, but nothing works, the text still get inserted in my EditText.
Any idea ?
Try to use the dispatchKeyEvent like that :
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
return scanManager.handleDispatchKeyEvent(event);
}
And in the scanManager class, you should do something like that :
public boolean handleDispatchKeyEvent(KeyEvent event) {
// handle the event
return true;
}

Android: show white background when search button pressed

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.

Android: When the on-screen keyboard appears and disappears, are there any listeners that are automatically called?

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?

android start user defined activity on search button pressed # handset

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.

Categories

Resources