how to disable phone keyboard while app is running? - android

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

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.

Android - Dialog fragment: always hide virtual keyboard

I have a custom dialog which is a DialogFragment. This dialog have a EditText and my own keyboard view so I don't want to use the default virtual keyboard.
I hide the virtual keyboard everytime user touch the EditText:
edtAmount.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
View view = this.getDialog().getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(),0);
}
return true;
}
});
But because the system still call the virtual keyboard to show (Before is force to hide it), then system move my dialog up and down very quickly. This is not good.
Can someone help me to avoid the dialog pushed up like this, just keep it stay still?
PS: I tried in Manifest:
android:windowSoftInputMode="adjustNothing"
But seem like not work.
Thank you very much.
EDIT
I want to keep the cursor so I find the solution in this thread:
https://stackoverflow.com/a/14184958/2961402
Hope this help some one.
This can only done when you extends the EditText from your custom EditText, please use the below code for custom EditText which never open Soft Keyboard ever...!
public class DisableSoftKeyBoardEditText extends EditText {
public DisableSoftKeyBoardEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onCheckIsTextEditor() {
return false;
}
}
Try this code. In my app it work perfectly
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Showing the Soft Keyboard manually with InputMethodManager does not adjust the window

So I have an Activity that is using windowSoftInputMode="adjustPan", and I have a OnPreDrawListener for an EditText that calls:
editText.requestFocus();
inputManager.showSoftInput(editText, 0);
Which works as expected and pushes the Activity up to make room for the EditText. However, if I dismiss the keyboard with the back button (which pans the window back to the original location), then touch the EditText again to show the keyboard, the keyboard shows, but the window does not adjust.
I've even tried adding an OnClickListener to the EditText and calling the same two calls again:
editText.requestFocus();
inputManager.showSoftInput(editText, 0);
But the window does not pan until I dismiss the window and show it again. Any suggestions?
I think this three steps will solve your problem.
1)in manifest file change windowSoftInputMode="adjustPan"
to windowSoftInputMode="adjustResize"
2) The layout you are using for EditText , tha change the parent layout to ScroolView.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="#+id/edittextview"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:inputType="textFilter"
android:padding="10dp"
android:imeOptions="actionDone"
android:scrollbars="vertical"
android:textSize="14sp" />
</ScrollView>
3) To explicitly showing keyboard to avoid "dismiss the keyboard with the back button and window is not Adjust"
in Edittext onclick write
edittext.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
InputMethodManager m = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (m != null) {
m.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
edittext.requestFocus();
}
}
});
Hope this will solve your problem.
So it's not exactly a solution to the problem, but it's the workaround I ended up going with. I created a subclass of LinearLayout to intercept the back button press before the IME receives it:
public class IMEInterceptLinearLayout extends LinearLayout {
//For some reason, the event seems to occur twice for every back press
//so track state to avoid firing multiple times
private boolean notifiedListener = false;
private OnBackPressedPreIMEListener listener;
public IMEInterceptLinearLayout(Context context) {
super(context);
}
public IMEInterceptLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public IMEInterceptLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setOnBackPressedPreIMEListener(OnBackPressedPreIMEListener listener) {
this.listener = listener;
}
private void fireOnBackPressedPreIME() {
if(listener != null && !notifiedListener) {
listener.onBackPressedPreIME();
notifiedListener = true;
}
}
#Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
fireOnBackPressedPreIME();
return true;
} else return super.dispatchKeyEventPreIme(event);
}
public interface OnBackPressedPreIMEListener {
public void onBackPressedPreIME();
}
}
Then from there I just registered my window as a listener for the layout, and dismiss the window and the keyboard when I receive the event, disallowing the keyboard to be dismissed while the window is visible.

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

Android custom EditText and back button override

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.

Categories

Resources