detect keyboard search button - java

I change android soft keyboard to show search icon rather than Enter icon. My question is: how i can detect that user click on that search button?

In the edittext you might have used your input method options to search.
<EditText
android:imeOptions="actionSearch"
android:inputType="text"/>
Now use the Editor listener on the EditText to handle the search event as shown below:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// Your piece of code on keyboard search click
return true;
}
return false;
}
});

You can use OnQuerySubmit listener to get keyboard Search button click event.
Here's how,
searchView.setOnQueryTextListener(queryTextListener);
SearchView.OnQueryTextListener queryTextListener
= new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
showLog("searchClickListener onQueryTextSubmit");
getPhotos(searchView.getQuery().toString());
return true;
}
#Override
public boolean onQueryTextChange(String s) {
return false;
}
};

Related

Searchview clear focus on hide keyboard

The user presses the hide keyboard button or the back button.
So I need to clear focus on the SearchView when the user is hiding the keyboard.
I tried this but it's not working. focus remains when the user hides the keyboard.
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
searchView.clearFocus();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
app.requests.getApi().search(newText).enqueue(SearchFragment.this);
return false;
}
});
and this:
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
app.functions.logWrite("has focus to searchview");
} else {
//code
}
}
});
Okay so try this it needs the use of a library unfortunately but it makes it easier.
In your build.gradle: add this:
dependencies {
implementation 'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:3.0.0-RC2'
}
Register for the keyboard events using KeyboardVisibilityEvent library like this in the fragment/class where SearchView is declared:
KeyboardVisibilityEvent.setEventListener(
getActivity(),
new KeyboardVisibilityEventListener() {
#Override
public void onVisibilityChanged(boolean isOpen) {
if (!isOpen) {
View focusedView = getWindow().getCurrentFocus();
if (focusedView != null && focusedView instanceof SearchView) { // does SearchView have focus?
searchView.clearFocus();
}
}
}
});
searchView.clearFocus(); works on the assumption you have another focusable view in the hierarchy, if not add this to your fragments layout:
android:focusableInTouchMode="true"
Alternatively simply call focus(); on any other view element you want to receive focus.
This is what I use for handling back button clicks for SearchView, by Overriding onBackPressed()
#Override
public void onBackPressed() {
if (!searchView.isIconified()) {
searchView.setIconified(true);
ANY_VIEW_IN_YOUR_LAYOUT.requestFocus();
} else
super.onBackPressed();
}
Hope this helps. Feel free to ask for clarifications...

Submit an AlertDialog.Builder containing an EditText by pressing enter on the keyboard in android studio

This is my code for an AlertDialog.Builder that has a custom view with an EditText. After entering the value inside the EditText, I want the press of Enter on the keyboard to act the same way as the PositiveButton of the AlertDialog.Builder. I have included the necessary 'imeOptions' part in the XML file. I manage to execute the code when pressing Enter, but the AlertDialog.Builder is still on screen and does not dismiss like when the PositiveButton on the AlertDialog.Builder is clicked.
//AlertDialog to set weekly income
incomeAlert = new AlertDialog.Builder(this);
incomeInflater = this.getLayoutInflater();
incomeDialogView = incomeInflater.inflate(R.layout.activity_weekincome, null);
incomeAlert.setView(incomeDialogView);
et_WeekIncome = incomeDialogView.findViewById(R.id.ls_WeekIncome);
et_WeekIncome.setOnEditorActionListener(new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
submitIncome();
return true;
}
return false;
}
});
incomeAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
submitIncome();
}
});
Thanks in advance for any help.
UPDATE: I managed to dismiss the AlertDialog.Builder by adding another piece of code as shown below
AlertDialog incomeDialog = incomeAlert.create();
incomeDialog.show();
Then when needing to dismiss, I use
incomeDialog.dismiss();
Since dismiss() is not available with AlertDialog.Builder, I had to create the Builder through an AlertDialog. Then I call dismiss() on the AlertDialog.
Thank you all for your input.
You can use OnKeyListener with your edit text to handle a specific keypress.
mEditTV.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
// do action
return true;
}
return false;
}
});
You can use the above setOnKeyListener this way.
et_WeekIncome.setOnKeyListener(new OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_ENTER:
submitIncome();
return true;
default:
break;
}
}return false;
}
});
Since dismiss() is not available with AlertDialog.Builder, I had to create the Builder through an AlertDialog. Then I call dismiss() on the AlertDialog.
There is another way to deal with this problem: use the setOnShowListener callback to set the key listener. This gives you access to the dialog's dismiss() method.
incomeAlert.setOnShowListener((DialogInterface d) -> {
et_WeekIncome.setOnKeyListener((v, keyCode, event) -> {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_ENTER) {
onClick(dialog, BUTTON_POSITIVE);
d.dismiss();
return true;
}
return false;
});

background button press detection

I am trying to build an android app which detects button pressed on a background and plays a sound or something. For demo, I am trying to add this method
public boolean onKeyDown(int keyCode, KeyEvent event) {
final MediaPlayer abc = MediaPlayer.create(this, R.raw.abc);
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
abc.start();
}
return super.onKeyDown(keyCode, event);
}
to this class
public class RSSPullService extends IntentService{
public RSSPullService(){
super("Nevermind me");
}
#Override
protected void onHandleIntent(#Nullable Intent intent) {
}
}
Is there something that can be helpful? I am new to android development and Java.
In touch android mobile phones there is not any keyboard hardware. So please specify which button you want to get pressed?
Edited:
Add the following permission in Manifest fie:
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
and override following methods:
//To capture any short click of button
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
// this is method which detect press even of button
event.startTracking(); // Needed to track long presses
return true;
}
return super.onKeyDown(keyCode, event);
}
//To capture long press of power button
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
// Here we can detect long press of power button
return true;
}
return super.onKeyLongPress(keyCode, event);
}

Set search widget back button functionality

I have implemented the Android search widget in my navigation drawer based app. I have set it to open the keyboard and focus the editText when clicking on the search icon. I want to set the back button (up button) to hide the keyboard. I have searched the web for the R.id of the up button, and found this android.R.id.home. So I have set it to be:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
case android.R.id.home:
hideKeyboard();
break;
...
}
I debugged the code and noticed that clicking on the navigation bar icon fires up the android.R.id.home, but hitting the up button of the search widget doesn't even enter the onOptionsItemSelected(MenuItem item) function.
I have also tried this:
searchView.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard();
}
}
});
But didn't work.
How can I hide the keyboard when pressing the back (up) button?
Setting the search view:
private void setSearchView(Menu menu) {
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search).getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
public boolean onQueryTextChange(String newText) {
Home.getFilter().filter(newText);
return true;
}
public boolean onQueryTextSubmit(String query) {
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
}
The following code should work:
searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setOnCloseListener(new OnCloseListener() {
#Override
public bool OnClose() {
searchView.clearFocus();
return true;
});
However this didn't work for me for some reason. :-/
I found the workaround below here:
searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.addOnAttachStateChangeListener(new OnAttachStateChangeListener() {
#Override
public void onViewDetachedFromWindow(View v) {
searchView.clearFocus();
}
#Override
public void onViewAttachedToWindow(View v) {
}
});
I don't think that using android.R.id.home will work since I think that onOptionsItemSelected(android.R.id.home) will only be called once the SearchView has been closed.

Stop keyboard from opening up when enter button is pressed?

I have an activity with a numeric EditText sitting above a button.
I have noticed that if you click in the edit text and use the hard keyboard to type some numbers then press the hard enter, that it shifts the focus to the button below the edit text and a QWERTY keyboard appears. How do I stop this from happening?
I have tried to control the enter key with the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activityEnterAmt);
final EditText editText = (EditText) findViewById(R.id.editTextEnterAmt);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
Log.i("Tag", "Enter pressed or IME Action Submit pressed");
saveButtonClick(editText);
}
return false;
}
});
}
But I really want to stop it from opening up the keyboard when you press enter. I don't really understand why it is doing this?
Thanks
I have fixed this issue by closing the soft keyboard when the EditText loses focus:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (v == editText) {
if (hasFocus) {
((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(editText,
InputMethodManager.SHOW_FORCED); // open keyboard
} else {
((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
editText.getWindowToken(), 0); // close keyboard
}
}
}
});

Categories