Kindle Fire Keyboard Buttons - android

I have an EditText field in my app:
<EditText
android:id="#+id/task_buy_edit_mass_editText1"
android:minWidth="100dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:imeOptions="actionDone" >
</EditText>
The problem is that on my Kindle Fire, the keyboard that appears when the user wants to edit the EditText has no Done button, just a button that hides the soft keyboard
That wouldn't be a problem if I could intercept the 'hide keyboard' button that I highlighted on the above screenshot. However, it doesn't seem to trigger the OnEditorActionListener I've attached to the EditText, so I'm a bit stuck what to do aside from having my own 'Done' button in my actual layout to take focus away from the EditText, hide the keyboard and make any changes that result from the number being changed
Is there a way to intercept the 'hide keyboard' button, or is there some alternative solution?
Incidentally, I have a regular EditText view elsewhere in my app that has no imeOptions nor specifies inputType and that does have a Return key
Here's the rest of the code for reference:
editText.setOnEditorActionListener(new OnEditorActionListener(){
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE || actionId==EditorInfo.IME_ACTION_NEXT || actionId==EditorInfo.IME_NULL || event.getKeyCode()==KeyEvent.FLAG_EDITOR_ACTION || event.getKeyCode()==KeyEvent.KEYCODE_ENTER){
closeKeyboard(v, done_button);
return true;
}
return false;
}});
// set done button for keyboards without correct IME options
final EditText finalEditText = editText;
editText.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
done_button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
closeKeyboard(finalEditText, v);
}});
done_button.setVisibility(View.VISIBLE);
return false;
}
});
.
private void closeKeyboard(TextView v, View buttonv){
demand.setAmount(Double.parseDouble((v.getText().toString()))); // update the game
// close keyboard
InputMethodManager inputManager = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
v.clearFocus();
buttonv.setVisibility(View.GONE);
}

android:inputType="textMultiLine" and android:singleLine="true" as #James Wald indicated works.
EDIT:
Doing the above will make it the EditText multi line. Instead android:imeOptions="actionGo" worked for my purposes on all devices (AFAIK).

Related

Custom keyboard set visibility gone when touch event dispatch from editText

I want to mimic android keyboard behavior when handling touch event. The android keyboard always hide every time I click somewhere else other the edit text and the keyboard. So far, I could mimic that's behavior using dispatchTouchEvent
Since it will detect for any dispatch touch even, whenever I click my keyboard button then it will close the keyboard it self. I wanna check if I click somewhere else other than my keyboard then hide the keyboard, else keep the keyboard on. I think I just missed piece of code and I can't find it what i missed here. It just need one single step to accomplish this. Please help.
Here my keyboard looks like
and here what i did so far
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (getCurrentFocus() != null) {
View view = getCurrentFocus();
//im not sure this condition would help
if (view == null) {
keyboard.setVisibility(View.GONE);
}
}
return super.dispatchTouchEvent(ev);
}
I can get the view object but i cant determine which view that i need to keep my keyboard on.
The other code (variable declaration)
PhoneKeyboard pk;
InputConnection ic;
LinearLayout keyboard;
pk = findViewById(R.id.phone_keyboard);
ic = phone_no.onCreateInputConnection(new EditorInfo());
pk.setInputConnection(ic);
Handling back pressed (mimic the android keyboard behavior)
#Override
public void onBackPressed(){
if(keyboard.getVisibility() == View.GONE)
super.onBackPressed();
else
keyboard.setVisibility(View.GONE);
}
How I handle request on the editText to make my keyboard visible
phone_no.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(
android.content.Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
if(view.equals(phone_no) && keyboard.getVisibility() == View.GONE) {
phone_no.requestFocus();
keyboard.setVisibility(View.VISIBLE);
}
return false;
}
});
How I handle submit / enter event at my custom keyboard (for who need the solution for custom keyboard, maybe this post can help other:) )
phone_no.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(
android.content.Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
if(view.equals(phone_no) && keyboard.getVisibility() == View.GONE) {
phone_no.requestFocus();
keyboard.setVisibility(View.VISIBLE);
}
return false;
}
});
Here is my snippet code for the keyboard layout view
<include
layout="#layout/layout_keyboard_phone_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="false"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Ps. I also have base activity and it extends to all my activity (if your solution required the base activity)
Update 1
I have try this method
phone_no.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
keyboard.setVisibility(View.GONE);
}
}
});
Is doing the same thing with the onDispatch event, whenever I click number on my custom keyboard, it also hide the keyboard. I need to check if the view others than the edit text and my custom keyboard then hide the keyboard else keep the keyboard on. Thank you

Android: How do I stop my edit text from going to next one?

so i have a login form. When i press the back button or the enter button, it seems to go the next edit text (or previous) even though i set these following options for my edit text.
What is weird, once i cycle through them all, everything works fine as it should i.e. back button / done button dismiss the edit text and key board.
Please note i am only showing 1 edit text field to not flood the post. The rest are pretty much the same
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<com.test.views.widgets.myEditText
android:id="#+id/userFirstName"
android:imeOptions="actionDone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/generic.first.name"
android:inputType="textPersonName"
android:textColor="#android:color/white"
android:theme="#style/EditText.Base.Style"
app:font="regular" />
</android.support.design.widget.TextInputLayout>
So the above, i have set actionDone which was one of the suggestions to this problem. This does not work.
In addition, I am extending the AppCompatEditText to have my own implementation.
Here is some code
When done button is pressed
#Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if(actionId == EditorInfo.IME_ACTION_DONE){
clearFocus();
}
return false;
}
When the back button is pressed
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
clearFocus();
}
return false;
}
Also note that i followed this tutorial as a means to hide keyboard when focus changes
http://toastdroid.com/2015/06/04/on-screen-keyboard-tracking-part-2/
Add android:imeOptions="actionDone". It will close keyboard rather than going on next focusable view.
add this to the EditText attribute
android:maxLines="1"
Try adding android:imeOptions="actionDone" to your EditText
OR
youredittext.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int keyCode,
KeyEvent event) {
if ( (event.getAction() == KeyEvent.ACTION_DOWN ) &&
(keyCode == KeyEvent.KEYCODE_ENTER) )
{
// hide virtual keyboard
InputMethodManager imm =
(InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
return true;
}
return false;
}
});
I suppose that problem is in your clearFocus(). Documentation says:
Note: When a View clears focus the framework is trying to give focus
to the first focusable View from the top. Hence, if this View is the
first from the top that can take focus, then all callbacks related to
clearing focus will be invoked after which the framework will give
focus to this view.
try to replace clearFocus() with hideKeyboard() like this
void hideKeyboard() {
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(yourView.getWindowToken(), 0);
}
I managed to fix this.
I added this to my root layout (which contained the edit text) and the problem no longer seems present
android:focusableInTouchMode="true"

Edittext Keyboard Not Showing After First Input

So I need an input box for text in my android app.
I have this in my xml:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/inputCodePA"
android:textSize="18sp"
android:inputType="text"
android:hint="#string/inputHint"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/arrowRightPA"
android:layout_toEndOf="#+id/arrowRightPA">
<requestFocus/>
</EditText>
and this:
public void setOnClickListners(){
final EditText inputBox = (EditText)findViewById(R.id.inputCodePA);
inputBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
inputBox.clearFocus();
inputBox.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(inputBox, InputMethodManager.SHOW_FORCED);
}
});
inputBox.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
inputBox.setText("");
inputBox.clearFocus();
inputBox.requestFocus();
return false;
}
});
}
For my code in my activity's class (setOnClickListner() is called in my onCreate()).
But, whenever after I type something into the EditText box and press Enter, I can not open the keyboard to enter text again.
I know I'm making a really basic error, but I can't seem to figure out what.
After all, this is the first time an EditText failed me.
HA!
I found out the problem!
It was because I had a ScrollView below the EditText in the code (which made the ScrollView become 'in front'), which did not allow the user to click on the view at all!
Hope this helps other people who makes these stupid mistakes!

Android keyboard enter button to keep keyboard visible

I'd like for my enter key to complete an action without hiding the keyboard afterwards. Currently every time I enter something and press the enter button the EditText input is cleared to accept the next input, but as the keyboard disappears I have to click the EditText again so that the keyboard would appear again...
Right now this is on my layout:
<EditText
android:id="#+id/etCommand"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/commandHint"
android:inputType="textNoSuggestions"
android:imeOptions="actionSend" >
</EditText>
And the code:
etCommand = (EditText) findViewById(R.id.etCommand);
etCommand.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendCommand();
}
return false;
}
});
EDIT
As swayam suggested I had to return true inside onEditorAction so that the OS would think the action was handled and leave it alone.
Have a go at this code snippet :
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
Add the KeyListener for EditText for Enter key press and after the task completed requestfocus on edittext again would be solution

How to add a Button on top of an Android keyboard

Hi guys i was assigned a task in my project,there i need to add a Done button on the top of Android keyboard.In my screen i have an EditText,whenever i click on the EditText it should open a keyboard along with the "Done" Button on the top of an Android keyboard.So that i can attach a Listener to that button to perform my task.Any Suggestions.
Thanks&Regards,
E.N.Krishna.
If you need the keyboard to display the Done button, you need to define this in your EditText
<EditText android:text="EditText" android:layout_width="fill_parent"
android:id="#+id/editText1" android:layout_height="wrap_content"
android:imeOptions="actionDone"/>
You can then catch when the user presses the Done button by using OnEditorActionListener
class DoneOnEditorActionListener implements OnEditorActionListener {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
return true;
}
return false;
}
}
Your answer could be found if you read this.
Namely the concept of IME action.

Categories

Resources