Android custom EditText and back button override - android

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.

Related

Edittext cursor still blinks after closing the soft keyboard

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.

how to disable phone keyboard while app is running?

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;
}
}

Skipping disabled EditText's when pressing Next IME button on soft keyboard

I have a LinearLayout with several EditText's, all of them created programmatically (not with an XML layout), and in particular without IDs.
When I'm typing in one of the EditText's, and the next one (respective to focus) is disabled, and I press the Next IME button on the keyboard, the focus advances to the disabled EditText, but I can't type anything in it.
What I was expecting was focus to advance to the next enabled EditText. I also tried, in addition to making the EditText disabled via edittext.setEnabled(false), to disable its focusability via edittext.setFocusable(false) and edittext.setFocusableInTouchMode(false), and to set a TYPE_NULL input type, but to no avail.
Any hints?
Thanks ;)
Solved by examining how the next focusable is found by the keyboard from this blog post and by subclassing EditText:
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
public class MyEditText extends EditText {
public MyEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyEditText(Context context) {
super(context);
}
#Override
public View focusSearch(int direction) {
View v = super.focusSearch(direction);
if (v != null) {
if (v.isEnabled()) {
return v;
} else {
// keep searching
return v.focusSearch(direction);
}
}
return v;
}
}
More details:
ViewGroup implementation of focusSearch() uses a FocusFinder, which invokes addFocusables(). The ViewGroup's implementation tests for visibility, while the View implementation tests for focusability. Neither test for the enabled state, which is why I added this test to MyEditText above.
I solved it setting the focusable property to false, not only the enabled property:
editText.setEnabled(false);
editText.setFocusable(false);
See
EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendMessage();
handled = true;
}
return handled;
}
});
This was grabbed from http://developer.android.com/training/keyboard-input/style.html#Action .
If you can figure out how to focus on the next TextView, you can add an OnEditorActionListener to every TextView and have it pass the focus to the next one if it is disabled.

Keep the numeric keypad open through out activity life

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;
}
}

deactivate sliding SeekBar (or creating a progessbar with thumb...)

I have a problem using a SeekBar in my code...
I'd like to have a SeekBar which the values are set by me, not by the user. The only solution I've founded and I don't like, is setting enabled to false, but the colors of the seek bar become grey... (so I can't do anything when the user slides the thumb...)
I would get something like that, but without touchable events!
SOLVED:
I solved the problem, as #CommonsWare says:
public class TaskListProgressBar extends SeekBar {
public TaskListProgressBar(Context context) {
super(context);
}
public TaskListProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TaskListProgressBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
return true;
}
#Override
public boolean onTrackballEvent(MotionEvent ev) {
return false;
}
You could subclass it and override onTouchEvent() and onTrackballEvent() (to eat all attempts to silde the thumb) and also use android:focusable="false" (to prevent it from getting the focus, so arrow keys cannot modify the thumb position).
That being said:
I have never tried this, so YMMV.
Please only do this if you are changing the default thumb image, per your screenshot above. Otherwise, users will expect the thumb to be movable, and they will get frustrated when they cannot move it.

Categories

Resources