13
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="20dp" >

    <AutoCompleteTextView
        android:id="@+id/autocomplete_zone"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:dropDownVerticalOffset="0.2dp"
        android:ems="10"
        android:hint="@string/zone_hint"
        android:inputType="text"
        android:lines="1"
        android:maxLines="1"
        android:popupBackground="#00ffffff"
        android:textColor="#ffffff"
        android:textColorHint="#ffffff" >

        <requestFocus />
    </AutoCompleteTextView>

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignBottom="@id/autocomplete_zone"
        android:layout_alignRight="@id/autocomplete_zone"
        android:layout_alignTop="@id/autocomplete_zone"
        android:paddingBottom="5dp"
        android:visibility="invisible" />
</RelativeLayout>

In the above markup

<requestFocus />

does not work. The text view is not focused when the activity is started. Is this because of the progress bar which overlays the text view? Any ideas how to fix this?

Pang
  • 9,564
  • 146
  • 81
  • 122
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
  • I've tested your layout and couldn't see any problem with focusing. Maybe your text color made you think that focus is not working? – Devrim Nov 09 '13 at 12:44
  • @aegean I mean the keyboard wont popup, I have a similar setup for another activity that is working. When the edit text is focused the keyboard does not show up. Any ideas? – DarkLeafyGreen Nov 09 '13 at 12:56

6 Answers6

18

I usually use the following to set the focus: Add following attributs to your xml-layout

<AutoCompleteTextView 
    android:focusable="true" 
    android:focusableInTouchMode="true"> 
</AutoCompleteTextView>

and set focus programmatically like

((AutoCompleteTextView) findViewById(R.autocomplete_zone)).requestFocus();

f.e. in onResume or onWindowChanged

public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    if (hasFocus) {
        ((AutoCompleteTextView) findViewById(R.autocomplete_zone)).requestFocus();
    }
}
alex
  • 5,516
  • 2
  • 36
  • 60
longi
  • 11,104
  • 10
  • 55
  • 89
  • This seems like a good answer, and based upon logic it should work but it does not. Instead i found that Evan Parsons solution was the only one that worked for me... why would that be? – JFreeman Jan 27 '18 at 04:30
8

None of the above worked for me... this is what I used

txtView.getParent().requestChildFocus(txtView,txtView);
Evan Parsons
  • 1,139
  • 20
  • 31
7

Solved it! In manifest I added following to the activity:

android:windowSoftInputMode="stateAlwaysVisible"
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
3

You can try this in the code if you want to forcefully show the keyboard.

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

then you can to use this code to close the keyboard:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(autocomplete_zone.getWindowToken(), 0);
TN888
  • 7,659
  • 9
  • 48
  • 84
nikhil.thakkar
  • 1,093
  • 7
  • 12
  • Sorta confused about the down vote... and the edit... Did the edit make the Answer OK? Maybe ignore the Answer? – DSlomer64 Jul 11 '14 at 22:17
3

To do this in code, in your Activity:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
hidro
  • 12,333
  • 6
  • 53
  • 53
0

I think this is the only good workable solution.

If you want to requestFocus() after !hasFocus then you must do this with a delay because else you call requestFocus(), but after that the next control receives focus. So its important to understand the next control receives focus AFTER your requestFocus().

The solution is that next control receives focus, but after a delay of x milisecs goes back to the previous control. Its a near perfect trick because the next control receives focus for x millisecs. I did set 100 msecs for the delay but you could try a smaller delay, may be that works too.

/**
 * validate data of scan control when focus changed
 * @param view - view to get control from
 * @param subject - subject to validate
 * @param scanId - id of scan data
 * @param fromId - id of from control
 * @param articleId - id of article control
 * @return - return scan edit control
 */
EditText editTextFocusListener(View view, int subject, int scanId, int fromId, int articleId) {

    // select all data when having focus
    EditText editTextScan = view.findViewById(scanId);
    editTextScan.setSelectAllOnFocus(true);

    // when not valid keep focus on control until valid
    editTextScan.setOnFocusChangeListener((vw, hasFocus) -> {
        if (!hasFocus && !validateInput(subject, editTextScan, fromId, articleId))
            new Handler().postDelayed(() -> editTextScan.requestFocus(), 100);
    });

    // return scan from control
    return editTextScan;
}