i am trying to open keyboard when i move to first activity to second activity.
There is two button in main activity
1) if click on "NotShowKeyboard" button it will open the second.java activity with out keyboard
2) if s"ShowKeyboard" button is clicked then it will open the second.java activity with keyboard and EitdText with focus
But problem is that i don't know how to do that. I put some examples top show keyboard but on "ShowKeyboard" button click keyboard open and immediately disappear.
Main.java:
NotShowKeyboard.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
}
if (event.getAction() == MotionEvent.ACTION_UP) {
bundle.putBoolean("show", false);
Intent start = new Intent(Main.this, Start.class);
start.putExtras(bundle);
startActivity(start);
}
return false;
}
});
ShowKeyboard.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
}
if (event.getAction() == MotionEvent.ACTION_UP) {
bundle.putBoolean("show", true);
Intent start = new Intent(Main.this, Start.class);
start.putExtras(bundle);
startActivity(start);
}
return false;
}
});
Second.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
i = getIntent();
extras = i.getExtras();
search = (EditText) findViewById(R.id.start_edit);
search.addTextChangedListener(myTextWatcher);
if((extras.getBoolean("show"))==true) {
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
//getting all stuff like buttons imageViews etc..
}
When NotShowButton Clicked then this should open:
When ShowButton Clicked then this should open:
For hiding keyboard:
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
For Showing keyboard:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
Have you tried this?
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Try and put this code in onResume()
Sometimes it depends on the device, if the softkeyboard comes up or not. On my Vodafone Smart II phone there didnt came it (3,5" display) on my Intexo TAB814 (8" display) there it was. Same App, but different results. It also semms to depend on the android version u use. (Vodafone smart II -> 2.1.3, Intenso tablet 4.1) try on another device
Related
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 !
The picture shows a part of my app, an AutoCompleteTextView with an attached adapter.
When the user enters something into that view, autocomplete suggestions are shown.
The problem I have is: when the suggestions are shown and the device's down arrow is pressed, only the suggestions from the AutoCompleteTextView are closed, the keyboard stays open and needs a second tap on the down arrow to disappear.
I do want the suggestions and the keyboard to disappear on the first tap on the down arrow.
I tried overriding onBackPressed but it is not called when the down arrow is tapped, presumably because it's not considered 'back'.
How could I do this?
EDIT: I know how to programmatically hide the keyboard, I guess my problem is to detect the 'down arrow' tap.
Try to override onKeyPreIme() method in your AutoCompleteTextView as follows:
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == 1) {
super.onKeyPreIme(keyCode, event);
hideKeyboard()
return true;
}
return super.onKeyPreIme(keyCode, event);
}
You can try something like this:
private boolean mIsKeyboardShown;
private EditText mSearchTextView;
#Override
protected void onCreate(Bundle bundle)
...
mSearchTextView = (EditText) findViewById(R.id.search);
View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
// if more than 100 pixels, its probably a keyboard...
mIsKeyboardShown = (heightDiff > 100);
}
});
}
public void onBackPressed() {
if(mIsKeyboardShown) {
// close the keyboard
InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(mSearchTextView.getWindowToken(), 0);
} else {
super.onBackPressed();
}
}
I haven't tried the code but I think this is the right approach.
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
you need to import android.view.inputmethod.InputMethodManager;
I want to create 1 edit text with the below condition:
- User can not focus on this control in normal.
- When user click on this control, soft-keyborad is displayed and user can input into this control
- When user press enter on this soft-keyborad or back on device, it is closed and back to normal view with control is not focus.
I tried the below code but not work :(
When starting, control is not focus: ok
When click on control, at the first click, control is focus but not display soft-keyborad
In the second click, display soft-keyborad
When press enter button in soft-keyborad: back to screen with control is not focus: OK
When press back button device, back to screen with control is still focus : not ok
public void onCreate(Bundle savedInstanceState)
{
final EditText txtSearch = (EditText)this.findViewById(R.id.p60004_txt_search_str);
txtSearch.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
txtSearch.setFocusable(true);//(false);
txtSearch.setFocusableInTouchMode(true);
txtSearch.requestFocus();
}
});
txtSearch.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
String strSearch = txtSearch.getText().toString();
if (strSearch != null && strSearch != ""){
searchFriend(UserAPIConstants.FRIEND_SEARCH_TYPE_SC, strSearch);
}
hideSoftKeyboard(v);
txtSearch.setFocusable(false);
txtSearch.setFocusableInTouchMode(false);
}
return false;
}
});
public void hideSoftKeyboard (View view) {
InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
In your xml which has edittext put these values for the layout node
android:focusable="true"
android:focusableInTouchMode="true"
and dont put any focusable or focusable in touch mode attributes for your edittext..
Then in your code in onKey method remove thse lines..
txtSearch.setFocusable(false);
txtSearch.setFocusableInTouchMode(false);
and put
txtSearch.clearFocus();
And You should override this method
onBackPressed()
like this..
#Override
public void onBackPressed() {
txtSearch.clearFocus();
//hide the soft keyboard..
}
try commenting
hidekeyboard(v);
and the changes that Alex Lockwood suggested.
use this in onClick()
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
to close the keypad use
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);
I am trying to achieve the following: my layout has a number of EditTexts. When the user touches one of the EditTexts for the first time, the keyboard should not open but the EditText should just gain focus. This is because I am doing a calculation in the EditText when it gains focus and I want to display the result to the user. Then when the user has seen the result he can touch the EditText again which will selectAll and open the keyboard to enter a new number.
This is the code I am using right now:
myEditText.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(iFocus == R.id.eTmyEditText) {
if(iCount == 3) {
myEditText.selectAll();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(myEditText, InputMethodManager.SHOW_IMPLICIT);
}
if(iCount > 3) return false;
iCount++;
}
else iCount=0;
myEditText.requestFocus();
return true;
}
});
I am using the iCount variable to distinguish the first touch from the second and the ones which will follow. iFocus is set in the focusChangeListener and holds the last EditText which had focus.
The code is working fine sometimes but not always. From time to time the keyboard is not opening or already on the first touch, sometimes the text is not selected, etc.
Is it possible that the TouchEvent bounces somehow? So that the TouchListener is executed more often than I would expect it to be? I also tried to separate the down event and the up event but this does not help.
Has anyone a good idea how what I am trying to do could be implemented in a better way? Thanks
EDIT: this is my final solution:
myEditText.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
myEditText.requestFocus();
if(iCount == 1){
myEditText.postDelayed(new Runnable() {
public void run() {
InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
manager.showSoftInput(myEditText, 0);
myEditText.selectAll();
}
}, 200);
}
iCount++;
break;
default:
break;
}
if(iCount >= 2) return false;
else return true;
}
});
Try this, its working for me when I touch the EditText two Times.
mEditText.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
iCount++;
if(iCount == 2){
mEditText.requestFocus();
InputMethodManager manager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
manager.showSoftInput(mEditText, 0);
iCount = 0;
}
break;
default:
break;
}
return true;
}
});
Try to replace
myEditText.setOnTouchListener(...);
with
myEditText.setOnClickListener(...);
Have a TextView that has the same background and size as the EditText, and switch between them on first tap, and switch them back again when the EditText loses focus.
Or store the current getKeyListener(), setKeyListener(null) and then restore after first click.
Or subclass EditText and override getDefaultEditable() to return false unless it already has focus.
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);
}
});