What I want to do is change the default "Done" label that appears in the virtual keyboard. Here's what I've tried without any luck:
mSearchInput.setImeOptions(EditorInfo.IME_ACTION_DONE);
mSearchInput.setImeActionLabel(getString(R.string.search_action_label), EditorInfo.IME_ACTION_DONE);
I am able, however, to handle a click on that button, with this:
mSearchInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
performSearch();
return true;
}
return false;
}
});
I'm clueless as to how I can change the label on that button at the moment.
The imeActionLabel sets the label for the button that appears on the top right on full screen IME mode (i.e., when your phone is in landscape). If you want to change the button to the bottom right of the keyboard, you can pass certain flags to imeOptions.
As far as I know, for that button you're limited to a certain set of actions (see here for a full list of supported flags), but since you seem to want a search button, all you have to do is to slightly adjust your first line and use IME_ACTION_SEARCH:
mSearchInput.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
Mind you, the exact appearance of that button will depend on the input method. The default Android keyboard shows a magnifier for the search flag, while the Touch Input (HTC's keyboard) seems completely unaware of that flag, still showing a return button.
Related
When a software keyboard is opened on my virtual device, the back button changes function to hide the keyboard when pressed - it does not stay 'KEYCODE_BACK' like it does whilst the keyboard is hidden.
Is this "hide" button still defined as a KeyEvent or do I need to go a different route in order to run an activity whenever it is pressed?
Android Studio 3.0.1
//setup i was hoping to use, but keycode changes whenever keyboard is shown//
public boolean onKeyDown(int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_BACK)
{
checkEmpty();
}
return false;
}
As stated by CommonsWare, "the system does not pass that event [back button while keyboard is displayed] to the activity. It just collapses the input method editor (soft keyboard)."
So no, it does not trigger a KeyEvent.
First off I am kinda new to Android, so with that being said. So I have a spinner, and every time I make a selection the phone will scroll back up to the last edit text who has focus. That is very annoying so I set the spinner as focusable, but for some reason I then have to click the spinner twice to get it to open (the first click gives the spinner focus, the second opens the spinner). So the best I have come up with so far is this:
activitySpinner = (Spinner) findViewById(R.id.acivity_dropdown);
activitySpinner.setFocusable(true);
activitySpinner.setFocusableInTouchMode(true);
activitySpinner.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
activitySpinner.requestFocus();
activitySpinner.performClick();
return true;
}
});
That takes care of the need for two clicks, but that causes problems because it will open the spinner on the slightest touch, even if all I wanted to do was scroll down. Am I looking at this problem the wrong way? Is there a way to make the spinner focusable and also allow it to open on the first click?
Things I've tried:
Setting focusable in the xml,
setting focusable and focusable in touch mode in java,
the code above
Using setFocusable(true) and setFocusableInTouchMode(true) are correct. To fix the two-touch problem created by the latter check for the ACTION_UP event in your touch handler and return false to let the event bubble up which eliminates the extra requestFocus() call:
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP && !v.hasFocus()) {
v.performClick();
}
return false;
}
The hasFocus() check makes this specific to the click-twice problem; if the control already has focus, your actual tap should bring up the list without additional work.
Try this also
spinner.requestFocusFromTouch();
I'm developing an android application and when I press the back button from my device (normal press time for a person, 1 second or less), it skips from my activity, to the previous activity (menu) and then exits the application.
But if I tap the back button quickly, it reacts as expected, it goes to the menu.
I've tried to find a solution but no success.
I've always tried to override the back button default behavior but no success either.
Is there a way to set a reaction time for the back button to react?
Many thanks in advance!
P.S.- I have other activities that maintains the expected behavior in back button when pressed with a normal press time.
"Is there a way to set a reaction time for the back button to react?"
Yes, you can simply record the time when the button is pressed and react differently in onBackPressed by calculating the (currentTime-lastTimePressed)
To allow this to work with previous activities, you can ask activities to startActivityForResult, so that when you finish your activity you can pass on the time as well to let them know if they should exit as well.
I was developing an extra option for an application that already exists, and I found out that I should extend not from the Activity from Android but an already extended Activity called SEActivity. So in this extended version of Activity they override the method onKeyDown like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
else
return super.onKeyDown(keyCode, event);
}
By extending this SEActivity, the Back button works with the expected behavior.
Thanks anyway :)
i have a game, consisting of 7 buttons , arranged such that 1 button is in the center and rest 6 around it.
click on the buttons results in change in textview. using click can really be cumbersome for the user, therefore i would like to add functionality of dragging finger over buttons to perform onclick ..something similar to boggle type of games like -> https://market.android.com/details?id=com.ant.wordfind.client&feature=search_result
i have tried to implement the same ,problem is when i drag of finger only one button gets registered , even if i press other buttons by dragging cursor -> other button touch are just ignored.given the
following code.
public boolean onTouch(View v, MotionEvent arg1)
{
if(arg1.getAction() ==MotionEvent.ACTION_MOVE)
{
Button b;
switch (v.getId())
{
case R.id.b1:
case R.id.b2:
case R.id.b3:
case R.id.b4:
case R.id.b5:
case R.id.b6:
case R.id.b7:
b = (Button)findViewById(v.getId());
b.performClick();
break;
}
return true;
}
am i missing somethng ? wht i want is whn buttons are touched ..they perform onclick functionality.once one button click is registered rest touches are ignored
any help would be appreciated .
i dont want something like piano/soundboard where there is continuous click of button ..but just one click as i drag finger over the button
thanks !
At onTouch(), you shouldn't always use return true. Return false if you want the subsequent actions to be captured.
onTouch() - This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.
Ref: http://developer.android.com/guide/topics/ui/ui-events.html
I have three EditText boxes in an activity, for two of which normal input methods (hard keys, default soft keyboard) are ok. But for one of the EditText boxes I want to send soft input only from a custom keyboard view. So in effect I want the default soft keyboard never to be shown for this EditText. I've tried adding onTouchListeners and onFocusChange listeners for the EditText with partial success like this:
public boolean onTouch(View v, MotionEvent event) {
v.requestFocus();
imm.toggleSoftInput(0, 0);
return true;
}
public void onFocusChange(View v, boolean hasFocus) {
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive(v)) {
imm.toggleSoftInput(0,0);
}
}
But I have not achieved a definitive solution because
1)the default soft keyboard always briefly flashes visible before the listener hides it
2)on some occasions, such as moving focus to the EditText with hard keyboard arrow keys sometimes sets the default soft keyboard visible
and so on.
So I would love to find a simple way to tell Android never to show the default soft keyboard for this specific EditText. I would not like to extend EditText and start to override stuff, since the EditText functionality is perfect for me - I just want the default soft keyboard not to be shown.
I've spent days now trying to figure this out. Some topics (including some here) found via google have half-way attempts at this problem, but so far I haven't found a single totally functional solution.
EDIT:
I'm really starting to get annoyed. I decided I could try not to use EditText but whatever other view that will get the job done. It turns out it is freakin hard to get rid of that soft keyboard. It even shows up when I use the hard keys to move focus from an EditText to a Button! Why on earth should the soft keyboard be shown on every freakin View that happens to have focus? Even when I explicitly say inputType="none"? How do I turn that * soft keyboard OFF? Below is the xml for the Button - let's use that as an example:
<Button
android:id="#+id/OkButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="none"
android:paddingRight="5mm"
android:paddingLeft="5mm"
android:layout_below="#id/Volume"
android:layout_alignParentLeft="true"
android:text="OK"/>
EDIT2:
I have how achieved a solution that seems to work. First I get a hold of the InputMethodManager:
this.imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
and the I set OnClickListener, OnTouchListener and OnFocusChange listener all call the following method when I want the EditText to be focused and my custom KeyboardView visible, while hiding the default soft input:
private boolean makeActive(View v) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
EditText e = (EditText) v;
int iType = e.getInputType();
e.setInputType(InputType.TYPE_NULL);
e.requestFocus();
showKb();
e.setInputType(iType);
return true;
}
Simple
editText.setShowSoftInputOnFocus(false);
Some people suggested that the following might work on older versions of Android, but the behaviour is unexpected.
edittextPlay.setTextIsSelectable(true);
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(searchBox.getWindowToken(), 0);
where SearchBox is your textbox or better yet instead of SearchBox get your current displayed window.
Or try:
InputMethodManager imm = (InputMethodManager)getBaseContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
where context is getApplicationContext();