I have a screen which asks user to enter PIN.
I have 4 separate boxes & each box will have only one digit.
So I want to keep the Numeric keypad open through out the life of an activity.
I am able to force keypad open up on activity starts. But On presses back button it gets hidden.
Can you set this as part of your activity section on your manifest file :
android:windowSoftInputMode="stateAlwaysVisible"
you could try something like this :
public class EditView extends EditText {
public EditView (Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
return true;
}
}
Related
Is an edittext cursor supposed to continue blinking after the soft keyboard is closed or is this a result of testing on an emulator and wouldn't happen on an actual device? -- as pointed out by the second post in this discussion
Update:
I know that the edittexts still have the cursor blinking because they're still in focus -- logged a message whenever edittext lost focus, but message was never logged when soft keyboard closed.
Update:
I've tried doing:
#Override
public void onBackPressed() {
super.onBackPressed();
getCurrentFocus().clearFocus();
}
So that every time the keyboard is closed, the EditText currently in focus loses that focus and onFocusChanged() is called. The problem is that onBackPressed() isn't called when the back button is pressed when the keyboard is up. I know this because I put a toast in onBackPressed(), and no toast shows when the back button is pressed whilst the keyboard is up.
First create a custom Edit text. Below is the example which has a call back when keyboard back is pressed to dismiss the keyboard
public class EdittextListner extends EditText {
private KeyImeChange keyImeChangeListener;
public EdittextListner(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setKeyImeChangeListener(KeyImeChange listener) {
keyImeChangeListener = listener;
}
public interface KeyImeChange {
public boolean onKeyIme(int keyCode, KeyEvent event);
}
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyImeChangeListener != null) {
return keyImeChangeListener.onKeyIme(keyCode, event);
}
return false;
}
}
Secondly change your EditText to EdittextListner in you layout file.
Finally do the following
mLastNameEditText.setKeyImeChangeListener(new EdittextListner.KeyImeChange() {
#Override
public boolean onKeyIme(int keyCode, KeyEvent event) {
mLastNameEditText.clearFocus();
return true;
}
});
This worked for me. Hope this helps
Edittext is a View which accept input from user, so it is not related with keyborad open or close, when user will click on edittext, that edittext will get focus and cursor will start to blink for taking input,
So you can do one thing as when you are closing keyboard at the same time you can also set visibility of cursor for that edittext so it will stop to blink,
For that you need to write below line when you hide keyboard.
editTextObject.setCursorVisible(false);
This will stope cursor to blink.
As you said, the blinking cursor in the EditText is related to the EditText having focus, but showing or hiding the soft keyboard has no correlation to a View gaining or losing focus. Any View (EditText or otherwise) can be focused independent of whether or not a soft keyboard is showing and there is nothing intrinsic to EditText that would make it behave any differently.
If you want an EditText to lose focus whenever the soft keyboard is hidden, you will need to implement this functionality yourself by listening for changes in the soft keyboard visibility and updating the EditText as a result.
The only way to know keyboard is disappeared is to override
OnglobalLayout and check the height.
Based on that event you can "setCursorVisible(false)" on your edit text
For more information, check this Link.
RelativeLayout mainLayout = findViewById(R.layout.main_layout); // You must use the layout root
InputMethodManager im = (InputMethodManager) getSystemService(Service.INPUT_METHOD_SERVICE);
/*
Instantiate and pass a callback
*/
SoftKeyboard softKeyboard;
softKeyboard = new SoftKeyboard(mainLayout, im);
softKeyboard.setSoftKeyboardCallback(new SoftKeyboard.SoftKeyboardChanged()
{
#Override
public void onSoftKeyboardHide()
{
// Code here
EditText.clearFocus();
}
#Override
public void onSoftKeyboardShow()
{
// Code here
}
});
/*
Open or close the soft keyboard easily
*/
softKeyboard.openSoftKeyboard();
softKeyboard.closeSoftKeyboard();
/* Prevent memory leaks:
*/
#Override
public void onDestroy()
{
super.onDestroy();
softKeyboard.unRegisterSoftKeyboardCallback();
}
try this:
public class EditTextBackEvent extends EditText {
private EditTextImeBackListener mOnImeBack;
public EditTextBackEvent(Context context) {
super(context);
}
public EditTextBackEvent(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EditTextBackEvent(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
if (mOnImeBack != null) mOnImeBack.onImeBack(this, this.getText().toString());
}
return super.dispatchKeyEvent(event);
}
public void setOnEditTextImeBackListener(EditTextImeBackListener listener) {
mOnImeBack = listener;
}
public interface EditTextImeBackListener {
void onImeBack(EditTextBackEvent ctrl, String text);
}
}
in your layout:
<yourpackagename.EditTextBackEvent
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
and in your fragment:
edittext.setOnEditTextImeBackListener(new EditTextBackEvent.EditTextImeBackListener()
{
#Override
public void onImeBack(EditTextBackEvent ctrl, String text)
{
edittext.clearfocus();
}
});
Try keeping a view in your layout which is focusable above your editText.
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:focusable="true"
android:focusableInTouchMode="true" />
This should work as the blank focusable view should catch focus and not your edittext.
1) make 2 Edit Text fields close to the bottom of the screen, also a submit button (on submit have a setError called on the first field)
2) run the app, enter some text in both fields, hit submit
3) you should see the error popup in the correct position (as expected), click into the 2nd text field, then click back into the first text field, the error popup is off-position (it's either not aligned correctly or the arrow is flipped in the wrong direction). While the soft keyboard is up in this case you can also hit back to close the soft keyboard but the error popup is still off-positioned.
Add this class to your source folder.
public class ListenerEditText extends EditText {
private KeyImeChange keyImeChangeListener;
public ListenerEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setKeyImeChangeListener(KeyImeChange listener){
keyImeChangeListener = listener;
}
public interface KeyImeChange {
public void onKeyIme(int keyCode, KeyEvent event);
}
#Override
public boolean onKeyPreIme (int keyCode, KeyEvent event){
if(keyImeChangeListener != null){
keyImeChangeListener.onKeyIme(keyCode, event);
}
return false;
}
}
In your XML file.
<com.src.ListenerEditText
android:id="#+id/password"
style="#style/EditBoxStyle"
android:ems="30"
android:hint="*API Key"
android:inputType="text|textNoSuggestions"
android:maxLength="250"
android:singleLine="true"
</com.src.ListenerEditText>
Add action for edittext.
password.setKeyImeChangeListener(new KeyImeChange() {
#Override
public void onKeyIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
password.clearFocus();
}
}
});
When using AutoCompleteTextView, the dropdown suggestion list appears with the software keyboard still visible. This makes sense, as it is often a lot more efficient to type ensuing characters to narrow the list.
But if the user wants to navigate the suggestion list, it becomes extremely tedious with the software keyboard still up (this is even more of a problem when the device is in landscape orientation). Navigating the list is a lot easier without the keyboard hogging the screen space. Unfortunately, the default behaviour removes the list first when you press the back key (even though in the software versions of the back key it is showing the image that says 'pressing this will hide the keyboard').
Here's a barebones example that demonstrates what I'm talking about:
public class Main2 extends Activity {
private static final String[] items = {
"One",
"Two",
"Three",
"Four",
"Five"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AutoCompleteTextView actv = new AutoCompleteTextView(this);
actv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
actv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
actv.setThreshold(1);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(actv);
setContentView(ll);
}
}
Besides the fact that this is unintuitive (the back key hint is suggesting that the back press will be sent to the keyboard), it makes navigating AutoCompleteTextView suggestions extremely tiresome.
What is the least intrusive way (e.g. catching the back in on onBackPressed() in every activity and routing it accordingly would definitely not be ideal) to make the first back press hide the keyboard, and the second remove the suggestion list?
You can achieve that by override-ing onKeyPreIme in your custom AutoCompleteTextView.
public class CustomAutoCompleteTextView extends AutoCompleteTextView {
public CustomAutoCompleteTextView(Context context) {
super(context);
}
public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && isPopupShowing()) {
InputMethodManager inputManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputManager.hideSoftInputFromWindow(findFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS)) {
return true;
}
}
return super.onKeyPreIme(keyCode, event);
}
}
set DismissClickListener like this
autoCompleteTextView.setOnDismissListener(new AutoCompleteTextView.OnDismissListener() {
#Override
public void onDismiss() {
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), 0);
}
});
hello i am making an app calculator for android when the user click's on the edit-box the keyboard pops (Phone) up how can i disable keyboard when app is running??
Reason is I have already made the number button's so the keyboard make it really hard to navigate.
Thank you
You need to disable the soft keyboard, add attribute in xml of that edit text.
<EditText android:id=".."
..
android:focusable="false" />
It will stop the execution of soft keyboard.
or
Create your own class that extends EditText and override the onCheckIsTextEditor():
public class NoImeEditText extends EditText {
public EditTextEx(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onCheckIsTextEditor() {
return false;
}
}
I want to override the back button when the soft keyboard is shown. Basically when the back button is hit, I want the keyboard to dismiss, and I want to append some text onto whatever the user has typed in that edit text field. So basically I need to know when the keyboard is dismissed. After searching around, I realized there is no API for this, and that the only real way to do this would be to make your EditText class.
So I created my own EditText class and extended EditText like this
public class CustomEditText extends EditText
{
public CustomEditText(Context context)
{
super(context);
init();
}
public CustomEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
public CustomEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init();
}
private void init()
{
}
}
I have also added this method
#Override
public boolean dispatchKeyEventPreIme(KeyEvent event)
{
if (KeyEvent.KEYCODE_BACK == event.getKeyCode())
{
Log.v("", "Back Pressed");
//Want to call this method which will append text
//init();
}
return super.dispatchKeyEventPreIme(event);
}
Now this method does override the back button, it closes the keyboard, but I dont know how I would pass text into the EditText field. Does anyone know how I would do this?
Also another quick question, does anyone know why this method is called twice? As you can see for the time being, I have added a quick logcat message to test it works, but when I hit the back button, it prints it twice, any reason why it would be doing this?
Any help would be much appreciated!!
This is due to the dispatchKeyEventPreIme being called on both ACTION_DOWN and ACTION_UP.
You will have to process only when KEY down is pressed. So use
if(event.getAction () == KeyEvent.ACTION_DOWN)
Edit:
for the first question You could do
setText(getText().toString() + " whatever you want to append");
in dispatchKeyEventPreIme
Why twice? Probably the method is called on press down and up event.