Gannon
Gannon

Reputation: 77

Set focus EditText android

I have a problem with EditText, in my app I need codebars that get it with codebar scanner, when the CCBB was inserted in the ET, the focus go to a ListView, but i tried to return the focus with:

et.requestFocus();

But the focus not move of the listview, also tried with disable focus of the listview.

In the app I have a thread with TTS, that speak every 15s and I put the et.requestFocus() in the thread but it dosn't work.

thanks!


I have achieved it with this code in xml:

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:hint="@string/ccbb"
        android:id="@+id/codigobarra"
        android:nextFocusDown="@id/codigobarra"/>

Upvotes: 3

Views: 5557

Answers (2)

Ankita Shah
Ankita Shah

Reputation: 1904

Try this. It will help you.

listview.post(new Runnable() {
  @Override
  public void run() {
      et.requestFocus();
      et.setFocusable(true);
      et.setFocusableInTouchMode(true);
  }

Upvotes: 2

Chetan Joshi
Chetan Joshi

Reputation: 5721

Add this android:descendantFocusability="blocksDescendants" property to parent view of your listItem and in EditText android:focusableInTouchMode="true" like below XML.

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="blocksDescendants"
        tools:context="com.cj.myapplication.MainActivity">

        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusableInTouchMode="true"
            android:text="Name" />

    </RelativeLayout>

Upvotes: 0

Related Questions