0

I have a linear layout containing an EditText and an ImageView. I've given the EditText a @null background and have given the LinearLayout a background of:

?android:attr/editTextBackground

to make it look like the whole thing is one widget. When the EditText gets focus/is selected I'd like to update the background drawable of the linear layout to show that the whole thing is selected.

My layout XML for the Linear Layout:

<LinearLayout
    android:id="@+id/search_plate"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/editTextBackground">

    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/edit_text"
        android:layout_weight="1"
        android:background="@null"
        android:height="36dp"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/image_view_close"
        android:src="@drawable/ic_clear"
        android:focusable="true"
        android:background="?android:attr/selectableItemBackground"/>
</LinearLayout>

This is the code I'm using to try and change the background state of the LinearLayout when the EditText is focused:

public class IconEditText extends LinearLayout implements View.OnFocusChangeListener {

    private static final String LOG_TAG = "IconEditText";

    private View mSearchPlate;            // Linear Layout
    private EditText mEditTextSearch;

    public IconEditText(Context context) {
        this(context, null);
    }

    public IconEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.icon_edit_text, this, true);

        mSearchPlate = findViewById(R.id.search_plate);
        mEditTextSearch = (EditText) findViewById(R.id.editText);

        mEditTextSearch.setOnFocusChangeListener(this);

    }

    @Override
    public void onFocusChange(View view, boolean focused) {
        mSearchPlate.getBackground().setState(focused ? FOCUSED_STATE_SET : EMPTY_STATE_SET);
    }
}

As well as using FOCUSED_STATE_SET, I've also tried the following:

ENABLED_FOCUSED_SELECTED_STATE_SET
FOCUSED_SELECTED_STATE_SET
ENABLED_SELECTED_STATE_SET
SELECTED_STATE_SET

None of the above seemed to change the background of the LinearLayout to the blue underline. Any help would be appreciated!

Gyroscope
  • 3,121
  • 4
  • 26
  • 33

1 Answers1

0

Seems as though: ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET works... if anyone could explain that, that would be great!

Gyroscope
  • 3,121
  • 4
  • 26
  • 33