Keyboard should show always and should not minimize until i press back - android

I have a chat feature in my application. everything is working fine. The problem i am facing is that i have an edittext and a button for sending the text. Now when i press the send button the keyboard comes down which i don't want.
Because it is very annoying for the user to open the keyboard after sending every message. Does anyone have any solution for this. it is a very silly issue but it is quite important for me. and is there any change in xml or manifest which we can make which will help solve this problem

Try the below code:
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// User has pressed Back key. So hide the keyboard
InputMethodManager mgr = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(this.getWindowToken(), 0);
// TODO: Hide your view as you do it in your activity
} else if (keyCode == KeyEvent.KEYCODE_MENU) {
// Eat the event
return true;
}
return false;
}

I was faced with the same thing in one of my project's. Try doing this to show keyboard
private void showKeyboard(){
InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
yourEditText.requestFocus();
}
This makes the keyboard go down only when back is pressed.

According to this answer.
EditText txtEdit = (EditText) findViewById(R.id.txtEdit);
txtEdit.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// your additional processing...
return true;
} else {
return false;
}
}
});
Let me know if it solves your problem.

if you return true from your onEditorAction method, action is not going to be handled again. In this case you can return true to not hide keyboard when action is EditorInfo.IME_ACTION_DONE.
Hope it will help !

Related

Listening for Done doesn't close soft keyboard

I have added a setOnEditorActionListener for my EditText so I can catch press of "Done" button. While it works like you can see in the code below and enters the if() section, keyboard stays open and doesn't close.
What do I have to change so I can still catch the press of "Done" button and close the keyboard ?
etCompany.setOnEditorActionListener(new BackEventEditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
((GetStartedActivity) getActivity()).isKeyboardOpen = false;
setVisibleContent();
return true;
}
return false;
}
});
You can force close it with
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(textView.getWindowToken(), 0);

Contextual ActionBar hides when I click on hardware backbutton and the keyboard is out

I am using the Contextual ActionBar for editing and when I have the keyboard shown and I want to hide it by pressing the hardware back button, it hides the keyboard but it also cancels the contextual actionbar and I really canĀ“t find a way how to keep it on.
Anyone?
You should try to override the Back Key hardware, and handle the expected behaviour with a boolean as follows:
// boolean isVisible to retrieve the state of the SoftKeyboard
private boolean isVisible = false;
// isVisible becomes 'true' if the user clicks on EditText
// then, if the user press the back key hardware, handle it:
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// check isVisible
if(isVisible) {
// hide the keyboard
InputMethodManager mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
mImm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
isVisible = false;
} else {
// remove the CAB
mActionMode.finish();
}
}
return false;
}
Another solution might be to call dispatchKeyEvent method which is still called when the CAB is displayed:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
// check CAB active and isVisible softkeyboard
if(mActionModeIsActive && isVisible) {
InputMethodManager mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
mImm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
isVisible = false;
return true;
// Maybe you might do not call the 'else' condition, anyway..
} else {
mActionMode.finish();
return true;
}
}
return super.dispatchKeyEvent(event);
}
This should do the trick, but I have not tested it. Hope this helps.
Sources: How to Override android's back key when softkeyboard is open - Prevent to cancel Action Mode by press back button

How to interact with android keyboard enter and rest of the button

I have an edit box which popup an android keyboard. What i want is on click of done button in the keyboard i want to display a message in the and if the user click rest of the buttons in-spite of done button the message should be clear. I have used this code but it doesn't work. Please help me to solve this out.
Code i have used
m_passwrdEditText.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_ENTER)
{
m_passwrdErrorText.setText("Test Project");
}
else
{
m_passwrdErrorText.setText("");
}
m_passwrdEditText.setTypeface(face);
return false;
}
});
return true from onKey() method.

How to handle hardware search button android?

I am new to android, I have a small doubt regarding how to handle the hardware Keyboard and if I click the search button in any part of my application it should be handled means I need to pass the intent of search activity?
How I can reach this goal.
Try this,
#Override
public boolean onSearchRequested() {
// your stuff here
return false;
}
It will also trigger onKeyDown with a keyCode of KeyEvent.KEYCODE_SEARCH before calling onSearchRequested as stated above
Add a listener for your EditText to listen on the Search button as following:
myEditText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH || keyCode == KeyEvent.KEYCODE_ENTER) {
//Do some stuff
}
//Leave the return value false to hide the SoftKeyboard if it is shown
return false;
}
});
For me, it happened before that the softkey search button is detected as Enter button not search. That's why I'm ORing them

EditText with soft keyboard and "Back" button

When I'm using "EditText" I have the virtual keyboard.
Pressing first time "Back" button hides the keyboard. Second press invokes "onBackPressed" callback in my activity. OK, but...
I have no idea how to hook the very first press. I need to process input data as soon as the virtual keyboard dismissed.
Any ideas are welcome.
Thanks.
You can override when the keyboard disappears 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);
}
Taken from my other answer # : Android: Error popup on EditText doesn't move down when keyboard goes away
Custom Back Button:-
final RelativeLayout rrBack = (RelativeLayout) mCustomView.findViewById(R.id.rr_back);
rrBack.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
MyApplication.getInstance().getRequestQueue().cancelAll(FEED_DETAIL_TAG_REQUEST);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(rrBack.getWindowToken(), 0);
}
});

Categories

Resources