Android: Tapping editable TextView doesn't give it focus - android

I have several EditViews in my activity's layout with editable enabled. When the activity's view is brought up, the first EditView gains focus: there is a blinking cursor and soft keyboard. However I can't get focus on any of the other EditViews in order to type into them. I have them enabled and editable. What other things could cause this?
If I simply load the layout in the activity without running any of my code, this problem does not occur, so it must be caused in the code at runtime--this is a ton of ported legacy code that is too much to share, but I wonder what types of properties or method side effects could cause enabled EditViews to not receive focus when tapped.

Another cause of this is having a Touch listener that does not set the event's TouchEventArgs.Handled property to true (I'm using Xamarin/monodroid vernacular here).
This was my code's problem. You must be sure to set TouchEventArgs.Handled to false. It was defaulting to true, which caused Android's handler which sets focus to not be run in the chain of responsibility!
_view.Touch += (sender, args) =>
{
Click(sender, new EventArgs());
args.Handled = false;
};

Related

xamarin forms entry selection length not changing value

In my app I have the StackLayout with 2 entries. On initialize stacklayout visibility is set to false.
Later in code I change it to true together with setting focus on 1st entry. Despite focus working correctly selection length doesn't change its value.
protected void Txt_Focussed(object sender, FocusEventArgs e)
{
txt.CursorPosition = 0;
txt.SelectionLength = txt.Text.Length;
}
When I debug the code I see that txt.Text.Length is 1 because it has default value set as 1 letter text. But when I go through the line that is going to change SelectionLength to 1, it stays at 0.
Could it be that this doesn't change because when I trigger the focus event, the stacklayout containing that entry is not visible?
I've used events a couple of times to change the focus in my code, and it always worked like a charm. However, now the stacklayout visibility parameter is what makes this different from the others
And once I changed the visibility from false to true and back to false, the length of the selection will work fine until I restart the application.

How to prevent Spinner announcement when initialized

Background
The fragment creates it's views and then starts a network operation. When the network operation is completed, various types of subviews are created and added to the fragment's view, based on the results of the network operation.
Problem
If a Spinner is added by the fragment, it's initial value is announced by TalkBack. This is very undesirable because the Spinner is usually buried deep within the form.
Failed Solutions
I've tried:
wrapping addView with setImportantForAccessibility
not setting the initial Spinner value (not an acceptable solution in any case) but the initial value is still announced when added
setting the Spinner's contentDescription to non-breaking space before adding and restoring it in onAttachedToWindow *
Question
Spinner is created and dynamically added, after the fragment is initially created. How can I prevent TalkBack from announcing the initial value of the Spinner?
Here's a workaround that I did in my project which worked as expected i.e. Spinner's content is only read out by TalkBack when it's in focus.
I subclassed the AppCompatSpinner class and overrode its onInitializeAccessibilityEvent function as follows:
#Override
public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED) {
super.onInitializeAccessibilityEvent(event);
}
}
1) set important for accessibility to false to all spinners in your form in onCreate().
spinner.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
2) setOnHoverListener to each spinner and inside it enable the accessibility again and send accessibility events to announce them properly.
spinner.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
spinner.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
spinner.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
spinner.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3) setOnTouchListener to each spinner to perform click after double tap.
spinner.performClick();

Android set input type in view. NOT on TextView or any derived class

I is there way to set input type on a View object? Note it is not TextView or any derived class like EditText, Button etc.
I is there way to set input type on a View object?
No.
In my case, I have constraints that require me to extend View and not TextView, yet I need to show the soft keyboard of differing types based on focus changes to objects that accept text (the objects accepting text are part of a custom, cross-platform framework and are therefore not EditText objects, etc.). Obviously, this is fighting the framework, but that's the situation.
In this case, you would have already needed to override onCreateInputConnection, which gives you an opportunity to set up the input type. It's kind of a hack, but you can get onCreateInputConnection called again by doing the following:
InputMethodManager mgr = (InputMethodManager)context.
getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.restartInput(this); // 'this' is the View
I would receive a notification from my custom objects that the keyboard needed to be opened with a certain intent, cached that intent, then performed the above couple lines. That would in turn result in a call to onCreateInputConnection, which allows you to set the input type to the cached value.

Why performEditorAction() always do an IME_ACTION_NEXT even with bad parameter?

I'm working on a accessibility service that include an IME for Android. It's important to know that is not part of an application (activity, view, etc).
With a certain command, I want to stop writing on a editText when I type letters.
I use the inputConnection to send action to the editor for this behavior with the action: IME_ACTION_DONE
//Inside public class myIME extends InputMethodService
boolean res = false;
InputConnection inputCon = getCurrentInputConnection();
if (null != inputCon)
{
res = inputCon.performEditorAction(EditorInfo.IME_ACTION_DONE);
//res = true, action executed but wrong behavior
}
For a unknow reason, the behavior is that the focus will always go to the next element in the view like the behavior of IME_ACTION_NEXT.
Even if I do this, this will do the same:
inputCon.performEditorAction(987654321); //No existing action ID
The question is, why?
Did you try to add the following to your EditText inside the xml-File?
android:imeOptions="actionDone"
I have a Layout with multiple EditTexts and am using a custom keyboard. Calling "inputCon.performEditorAction(EditorInfo.IME_ACTION_DONE);" was my only way to hide the keyboard after entering text. The focus stays on the last edited EditText (blinking cursor, but no keyboard shown).

Is there anyway to get my edittext highlighted?

I disabled softkeypad in my application because I have my own custom keypad. But the problem is when I clicked on the edittexts in order to enter data through my custom keypad ,that edittexts are not getting highlighted at all. Even cursor is not visible inside that respective clicked edittext. Why there are always side effects while disabling soft keypad? I tried all the suggestions that are there in the sources including stackoverflow, but nothing worked. Can I get the perfect solution to get the edittext highlighted when clicked?
you need to call textView.requestFocus() when clicked this way your editText can be highlight
dont forget also to add in your XML File
this attribute android:focusableInTouchMode="true" to your EditText
I don't know why those side effects occur, but in this post there is a workaround how disable the keyboard and still have the cursor. That worked for me except that I also needed to request focus, so it's:
//disable keypad
et.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
int inType = et.getInputType(); // backup the input type
et.setInputType(InputType.TYPE_NULL); // disable soft input
et.onTouchEvent(event); // call native handler
et.setInputType(inType); // restore input type
et.requestFocus(); // request focus
return true; // consume touch even
}
});

Categories

Resources