triston
triston

Reputation: 493

clearFocus in android dialog doesn't work

I have a EditText(searcField) in a android dialog, after I finished the editing, and dismiss the dialog.

then i go to other EditText, the focus is still in searchField.

Any idea?

thanks

EDIT: there are too much to post in here, hope this can help a bit to understand my issue.

Customer dialog view

           <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingTop="5dip"
                android:orientation="horizontal" >

                <EditText 
                     android:id="@+id/search_field"
                     android:layout_width="0dp"
                     android:layout_height="wrap_content"
                     android:gravity="left"
                     android:inputType="textNoSuggestions|textVisiblePassword"
                     android:layout_weight="0.70"
                     android:hint="type item name to search"/>


           </LinearLayout>


          <ListView    
             android:id="@+id/itemList"    
             android:layout_width="fill_parent"    
             android:layout_height="wrap_content"
             android:divider="#00000000"
             android:descendantFocusability="beforeDescendants"    
             android:scrollbars="none"/>

      </LinearLayout>

Java code

    View searchView = getActivity().getLayoutInflater().inflate(R.layout.search, null, false);
    EditText searchField = (EditText)productEditView.findViewById(R.id.search_field);
    searchField.addTextChangedListener(this);
    ...

    public void afterTextChanged(Editable text) {
        if (searchField.hasFocus()) {
            Log.i("MyApp", "searchField is still focused");
            return;
        }
        if (otherField.hasFocus()){
           Log.i("MyApp", "other field");
           return;
        }
    }

Upvotes: 4

Views: 3523

Answers (3)

Pankaj Arora
Pankaj Arora

Reputation: 10274

You have to add the following code after your editing code event :

seachview.clearfocus();
edittext.requestfocus();

Upvotes: 1

type-a1pha
type-a1pha

Reputation: 1893

I don't know if it can be achieved through xml but you can do it programmatically when your dialog is dismissed by doing something like:

mView.setFocusableInTouchMode(true);
mView.requestFocus();

To know when your dialog is dismissed simply override the onDismiss() callback method.

Upvotes: 2

invertigo
invertigo

Reputation: 6438

Another, albeit somewhat hacky, solution I regularly use is setting android:focusable="true" and android:focusableInTouchMode="true" to the parent view (LinearLayout, RelativeLayout, etc) of a layout if I want to be able to clearFocus() of an EditText, or open the Activity/Fragment without the first EditText from being focused automatically.

Upvotes: 3

Related Questions