Popup Keyboard issue in android, when column limits are specified - android

Im working on an input method for android and one of the tasks is to implement a custom popup keyboard for hard keyboard key presses. Usually input methods let the editor handle that but the thing is I need to add more symbols than android supports.
So I implemented the popup keyboard and even have it displaying well when a hard key (character key) is long pressed.
The steps I've followed are:
Create popup window.
Inflate a linear layout which contains a keyboardview and a close button and save it to a view object
bind the keyboardview and close buttons to the relevant objects
Create a keyboard for the popup characters and set it as the keyboard for the keyboard view. The keyboard has a column limit of 5.
set the linear layout as the content view for the popup window
Display the popup window
THE ISSUE: If there are multiple rows in the popup keyboard I am only able to select key in the last row for the column. Even if I click on the key in the first row of that column, the key in the last row gets selected.
If anyone could explain why this is happening and how do I fix it, Id appreciate it.
THE CODE:
PopupWindow mPopupKeyboard = new PopupWindow(this.getBaseContext());
mPopupKeyboard.setBackgroundDrawable(null);
if(mPopupKeyboard != null)
{
this.dismissPopupKeyboard();
View mMiniKeyboardContainer = null;
KeyboardView mMiniKeyboard = null;
View closeButton = null;
mMiniKeyboardContainer = getLayoutInflater().inflate(R.layout.keyboard_popup_keyboard, null);
mMiniKeyboard = (KeyboardView) mMiniKeyboardContainer.findViewById(R.id.popup_keyboardView);
closeButton = mMiniKeyboardContainer.findViewById(R.id.closeButton);
if (closeButton != null)
{
closeButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
mPopupKeyboard.dismiss();
});
}
mMiniKeyboard.setOnKeyboardActionListener(this);
String resourcestring = "abcdefghi";
mMiniKeyboard.setKeyboard(new Keyboard(this.getBaseContext(), R.xml.kbd_popup_template, alternates, 3, 0));
mMiniKeyboard.setPopupParent(mCandidateView);
mPopupKeyboard.setContentView(mMiniKeyboardContainer);
mPopupKeyboard.setWidth(LayoutParams.WRAP_CONTENT);
mPopupKeyboard.setHeight(LayoutParams.WRAP_CONTENT);
mPopupKeyboard.showAtLocation(mCandidateView, Gravity.TOP, 0, 0);
}

I had a similar problem with popup keyboards. I found that it was a problem only with Android 2.3. My only workaround was to avoid popup keyboards with more than one row.

The reason this occurs is because KeyboardView sends off the MotionEvent. MotionEvent.getRawX() and getRawY() only return coordinates within the bounds of KeyboardView. If the MotionEvent happens above KeyboardView, it returns the closest absolute coordinates in KeyboardView.
One solution is to create an invisible View above KeyboardView. It will have to detect the MotionEvent and then pass the MotionEvent back to KeyboardView and then your multirow popup keyboard will work
For the starting code look at CandidateViews above the KeyboardView. For example look at this project:
https://github.com/blackcj/AndroidCustomKeyboard
In the
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
(https://github.com/blackcj/AndroidCustomKeyboard/blob/master/app/src/main/java/com/blackcj/customkeyboard/CandidateView.java)
method add 200 to desiredHeight in this statement:
setMeasuredDimension(measuredWidth, resolveSize(desiredHeight, heightMeasureSpec));
Notice how this will cause the motionEvent.getRawY() to work for an extra 200p in height

Related

Keyboard placement obscures view below EditText would like to keep visible

I have an activity that is basically a long form of entry fields.
On each row, I want to show a TextView to serve as hint text just below each EditText and I want the TextView to remain visible at all times when the user is entering data. Unfortunately, the soft keyboard obscures the hint text and always positions itself immediately below the EditText. Is there any technique that will allow the TextView below the EditText to also be visible when the soft keyboard appears and the contents are adjusted (via windowSoftInputMode=adjustResize|adjustPan), without having the user scroll ?
Vishavjeet got me on the right track in suggesting I scrolldown to reveal the view that may be overlapped by the keyboard. Below is a function similar to what I used to solve the problem. It can be called when the EditText above the TextView receives focus:
// View targetView; // View that may be hidden by keyboard
// ScrollView scrollContainerView; // Scrollview containing hiddenView
//
void assureViewVisible (View targetView, ScrollView, scrollContainerView) {
Window rootWindow = activity.getWindow();
Rect rMyView = new Rect();
View rootview = rootWindow.getDecorView();
rootview.getWindowVisibleDisplayFrame(rMyView); // Area not taken up by keyboard
int subTextPos[] = new int[2];
targetView.getLocationInWindow(subTextPos); // Get position of targetView
int subTextHt = targetView.getHeight(); // Get bottom of target view
if ((subTextPos[1]+subTextHt) > rMyView.bottom) { // Is targetView at all obscured?
int scrollBy = (subTextPos[1]+subTextHt) - rMyView.bottom + 10; // add a small bottom margin
mMeasurementViewScrollView.smoothScrollBy(0, scrollBy); // Scroll to subtext
}
}
EDIT:
By understanding the problem more deeply, I think that you should add scroll programatically when user clicks on the Edittext. Here is the code to do that:
private final void focusOnView()
{
new Handler().post(new Runnable()
{
#Override
public void run()
{
your_scrollview.scrollTo(0, your_EditBox.getBottom());
}});
}
From my personal experience I think there is not such way to do that. The thing you can do is place the hint textview toRightOf the editext. Or Use modern Approach by using a Hint Placeholder on Edittext:
In XML, it's simply android:hint="someText"
Programatically you can use edittext.setHint(int);
pass R.string.somestring in above method.

How to disable all input controls in an activity in android?

I want to Disable all input controls (eg: TextEdit, Spinners) on click of a button.
Eg: When user enters a value in text field and clicks on Submit button, I would want to disable all input controls and hide keyboard.
An overlay view on top of activity can be added to prevent user from touching screen, but this is not an option since I want to disable all input components and hide input controls.
Iterate the container layout view and treat the views depending what widget they're instances of. For example if you wanted to hide all Button and disable all EditText:
for(int i=0; i < layout.getChildCount(); i++) {
View v = layout.childAt(i);
if (v instanceof Button) {
v.setVisibility(View.GONE); //Or View.INVISIBLE to keep its bounds
}else
if (v instanceof EditText) {
((EditText)v).setEnabled(false);
}
}
Of course if you wan to add other properties such as making it not clickable or whatever you'd just add them in the correspondent if from the previous code.
Then to hide the keyboard:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
A cleaner way of doing this (if you know the ids of the views) is to store them in int[] and the loop over that instead of getting al children views from the layout, but as far as the result goes, they're pretty much the same.
Let us take the case of TextView. Then do as follows :
textView.setClickable(false);
textView.setFocusable(false);
textView.setFocusableInTouchMode(false);
This would disable the TextView. Similarly for the others as per requirement.
Try using editText.setEnabled(false);
Just set setEnabled(false); for all controls inside button click.
buttonOK.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
editText.setEnabled(false);
spinner.setEnabled(false);
.......
// here you can disable all InputControls.
}
});

Can't edit an edittext in a popup window without dismissing and recalling it first

I am using a popup window to display some information and have an edit button that allows that information to be changed. I am loading all textviews, buttons and edittext fields and hiding/showing them as needed. The edittext fields are not letting me edit them. I have tried the below suggestion about setting focusable to true, but that isn't working. Any other suggestions?
Tried this: EditText On A Popup Window
EDIT: Below is part of my code. I've just included the parts for initializing the edittext and showing the popup contents since everything else is working, just not the edittext. I am using a tablelayout, and when the user clicks a row, the popup window displays. Keep in mind I'm still pretty new to Java and Android!
EDIT #2: The softkeyboard was not showing when the edittext was selected, but now it will show if I dismiss the popup once and then call it again. I then tried forcing the softkeyboard to display, and it showed behind the popup (another problem). I was able to select the number 1 since it was barely showing behind the popup window, but it didn't seem to work either. The fix was the same here: dismiss the popup window and recall it. So my remaining problem is being able to type into the edittext without having to dismiss the popup once and recall it. As such, I am changing the title of this question.
row1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final EditText pwETBrand = (EditText) vPopUp.findViewById(R.id.et_editbrand);
if (!infoClick) {
infoClick = true;
// Popup elements initialized earlier
pwInfo.showAtLocation(llInfo, Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
pwInfo.update(0, 0, llMain.getWidth()-50, llMain.getHeight()-100);
pwInfo.setFocusable(true);
....
// Hide some elements initially until "EDIT" button is pressed
pwETBrand.setVisibility(View.INVISIBLE);
....
// When EDIT button is pressed, hide default elements, and show edit elements
pwEdit.setOnClickListener(new OnClickListener() {
public void onClick(View v2) {
pwETBrand.setVisivility(View.VISIBLE);
// Other element settings go here
pwInfo.setFocusable(true);
}
)};
....
}
}
I have implemented a solution, though it may not be the best solution. I have set a boolean field at the class initialization that checks if the popupwindow has ever been called. If it has not yet been called, then it will immediately dismiss the popupwindow and re-open it since my problem is in the popupwindow initialization.
pwInfo.showAtLocation(llInfo, Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
pwInfo.update(0, 0, llMain.getWidth()-50, llMain.getHeight()-100);
pwInfo.setFocusable(true);
if (pwFirst) {
pwFirst = false;
pwInfo.dismiss();
pwInfo.showAtLocation(llInfo, Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
pwInfo.update(0, 0, llMain.getWidth()-50, llMain.getHeight()-100);
pwInfo.setFocusable(true);
}
Not the best solution, but it works.
Try this,
final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,true);
works for me.
Use Below Link's Code for that, it may help you.
Popup With Edittext
Whenever you create a PopupWindow you must put true in focusable like that :
PopupWindow(View contentView, int width, int height, boolean focusable)
final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,true);
good luck

Make a custom keyboard in Android having the same behavior as the soft keyboard

So, today I decided to try out Android, so please understand that I am a beginner in it.
What I want to achieve right now is to have a EditText, and a set of buttons to be used to enter data into the EditText.
What I've done currently is stick a set of button widgets in the XML layout, and I use this code to make the buttons insert stuff into the EditText:
final EditText inputline = (EditText) findViewById(R.id.textentry);
final Button my_button = (Button) findViewById(R.id.my_btn);
my_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
inputline.append("a");
}
});
This kind of works, but I need help with a few issues:
it always appends the character at the end of the string, not at the current cursor position
similarly, when I call inputline.selectAll() and press my button, it inserts the text at the end of the string again; whereas I want it to delete the text first (as it's selected) and then insert the character
it seems tedious to write all that code for each of the buttons I have. Is there a better way to do this altogether?
Thanks for your help!
I have now pretty much solved by replacing inputline.append("a"); etc. with my custom function, lineInsert(), which you can see below.
public void lineInsert(CharSequence text) {
final EditText inputline = (EditText) findViewById(R.id.textentry);
int start = inputline.getSelectionStart();
int end = inputline.getSelectionEnd();
inputline.getText().replace(Math.min(start,end), Math.max(start,end), text, 0, text.length());
inputline.setSelection(inputline.getSelectionEnd());
}
This has the same behavior as the soft keyboard.

Defining EditText imeOptions when using InputMethodManager.showSoftInput(View, int, ResultReceiver)

In my application I have a custom view which requires some text input. As the view in itself doesn't contain any actual views (it's a Surface with custom drawing being done), I have a FrameLayout which contains the custom view and underneath it an EditText -view. When the user does a specific action, the custom view is hidden and the EditText takes over for user input. This works fine, but android:imeOptions seem to be ignored for this view. I'm currently doing this:
InputMethodManager inputMethodManager = (InputMethodManager)parent.getSystemService(Context.INPUT_METHOD_SERVICE);
EditText t = (EditText)parent.findViewById(R.id.DummyEditor);
t.setImeOptions(EditorInfo.IME_ACTION_DONE);
inputMethodManager.showSoftInput(t, 0, new ResultReceiver(mHandler) {
#Override
protected void onReceiveResult(
int resultCode, Bundle resultData) {
// We're done
System.out.println("Editing done : " +
((EditText)parent.findViewById(R.id.DummyEditor)).getText());
}
}
);
It seems that the setImeOptions(EditorInfo.IME_ACTION_DONE) has no effect. I've also tried adding the option to the layout XML with android:imeOptions="actionDone". No help.
Any ideas?
this post looks like will answer your question:
How do I handle ImeOptions' done button click?

Categories

Resources