Set Spinner focus to the Spinner itself after a user selection - android

When I turn TalkBack on, once a selection is made in my Spinner, the focus immediately goes back to the top of the screen (toolbar). I would like the focus to be on the spinner immediately after the user's selection.
I have tried setting the spinner to be focusable:
spinner.setFocusable(true);
spinner.setFocusableInTouchMode(true);
I have checked out the following StackOverflow questions and answers which do not directly address the issue I have:
How can I use onItemSelected in Android?
Adding Focus to a spinner
I am noticing that the spinner is briefly disabled as the selection is set. I am guessing this is why the focus gets delegated to a different view. How can I circumvent this disabling or work around it?
Any help would be appreciated.

I was able to fix this by performing the following (hopefully this helps someone else with the same issue):
private boolean isUserSelection; // to detect user selection
spinner.setChangeListener(this);
spinner.setFocusable(true);
spinner.setFocusableInTouchMode(true);
if (((AccessibilityManager)getActivity().getSystemService(Context.ACCESSIBILITY_SERVICE)).isEnabled())
{
spinner.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus && !isUserSelection)
v.performClick(); //bypasses the caret inside the spinner so user doesn't have to tap twice to open the drop down
else if (!hasFocus && isUserSelection)
v.requestFocus();
}
});
}
In the itemSelectedListener for the spinner, use this code:
if (isUserSelection && (AccessibilityManager)getActivity().getSystemService(Context.ACCESSIBILITY_SERVICE)).isEnabled()))
durationSpinner.requestFocusFromTouch();

Where does the focus go to? Try requesting focus from there like this.
newlyFocusedView.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
newlyFocusedView.clearFocus();
spinner.requestFocus();
spinner.performClick();
}
}
});
Also try spinner.setFocusable(true);

Related

Disabling edittext searchview after user input

How to disable EditText searchview after completion of input on fragment?
You can set View.OnFocusChangeListener to your editText and after focus change call setEnabled(false). for eg.
mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
//let the user input
} else {
//disable you search view
mEditText.setEnabled(false);
}
}
});
P.S. -If you are still facing a problem, please update your code snippet.
If user have to click button after enter text into searchview, you can handle this in event button click method. In your button click method you can write:
search.setEnabled(false);

How Do I Use setOnFocusChangeListener with RecyclerView?

I have the following on onBindViewHolder() in Adapter Class for RecyclerView:
holder.answerEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
String answer = holder.answerEditText.getText().toString();
mDatasetAnswers.add(answer);
}
}
});
The above only returns input from the first editText in the recyclerview. What could I be doing wrong?
I would like it to return text from all EditTexts in the recyclerview.
This happens because of the keyboard which pops up once you click on any Edit box in a recyclerview because onbindview is called and the focus changes to the first box in the recyclerview as all rows are reinflated again.
Hence, monitor for on focus gain and do ur stuff first before keyboard pops up. Hope this helps.
Try to add these lines:
holder.answerEditText.setFocusable(true);
holder.answerEditText.setFocusableInTouchMode(true);
With these lines you make sure that the component can capture the focus.

Remove Focus change Listener of a view in android

I have added FocusChangeListener to EditText. like
etZip.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus)
{
callAreaDetailService(zipCode,false);
}
}
});
Now If I have focus on etZip and I click on Button to go to anotherActivity. etZip loses focus and show Response alert in Next Activity.
I know I can add a boolean flag for not calling service if button is clicked. But I am sure there must be an android build in method to stop focus change.
There is no function like etZip.removeFocusChangeListener . So is there any way to do this using Android SDK.

How to clear selection of ListView when the ListView lose focus and reset the old selection when the ListView has focus?

Recently, I am working in a TV platform, I find that when I move from one ListView to some other view, the ListView will keep the selection which makes the ListView background doesn't change(I have asked the background problem at Why the background color does not change when I move focus away from a list view?).
I think if I clear the selection when I move away from the ListView, the background will set to normal, so I add these code:
mMenuView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
mMenuView.clearChoices();
mMenuView.setItemChecked(mMenuView.getSelectedItemPosition(), false);
}
}
});
But, it seems that I do not clear the selection successfully.
And also, I need to reset the old selection, do I have any choice?

How to make a View(spinner) visible on keyBoard hide event in Android?

I have EditView a button and a Spinner in my layout. A picture is given below:
When I focus on the search field (EditText) I'm making the spinner (BrowseBy) disappear. Code is here:
edTxt_SearchField.setOnFocusChangeListener
(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (edTxt_SearchField.hasFocus()) {
spnrBrowseBy.setVisibility(View.GONE);
}
}
});
what happens here is that the softkeyBoard appears. also notice that spinner is gone.
When I press the backKey here, the softkeyboard is hiding. I want the spinner should reappear here also as in 1st image. Or is there any method which can detect the keyBoardHide event?
I have tried onConfiguration change method, has no effect. Note: I can't override the onBackPresskey() method also because my activity is extending the ActivityGroup class.
Did you put the configChages in your manifest so that onConfigurationChage is called?
<activity android:name="TestActivity"
android:configChanges="keyboardHidden">
otherwise it should be just
public void onConfigurationChanged(Configuration newConfig){
spnrBrowseBy.setVisibility(View.VISIBLE);
spnrBrowseBy.requestLayout();
super.onConfigurationChanged(newConfig);
}
By following way you can take actions on Got Focus and Lost Focus of your EditText.
yourEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
Toast.makeText(getApplicationContext(), "Got Focus", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Lost Focus", Toast.LENGTH_LONG).show();
}
});
Hope this helps you.
Thanks.

Categories

Resources