7

I have a weird problem that I'm having problems even articulating.

My layout is a relative layout that contains a scrollview that contains chips and an edit text. Below the scroll view, is a listview for auto completion, below which is a linear layout that houses an edit text and a button. (Screen shots at the end of this post).

When the fragment loads and the user types into the edittext inside the scroll view, the listview with auto complete options pops up. When an auto complete option is selected that is added as a selected chip. The library that generates those chips does not clear focus of the edit text, but something is jogging the OS to move focus to the bottom edit text. This results in the weird state that BOTH edit texts are blinking as if they had focus. If the user starts typing on the keyboard again the focus is actually on the the lower edit text, and it's now impossible to focus on the first edit text again to type.

The code for the NonFocusingScrollView is as follows. This properly fixed my issue where the scroll view around the ChipsView was grabbing focus and preventing the edit text from working correctly at all. (The SevenChipsView in the layout is merely an extension of ChipsView).

I've tried, programatically, removing focus from the lower edit text, and explicitly putting it back into the edit text in the scroll view, but that doesn't do anything.

Is there a way that I can prevent the OS from shifting focus around, and stay in the top edit text until the user taps the bottom edit text?

public class NonFocusingScrollView extends ScrollView {
    public NonFocusingScrollView(Context context) {
        super(context);
    }

    public NonFocusingScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NonFocusingScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public NonFocusingScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
        return true;
    }

    @Override
    public ArrayList getFocusables(int direction) {
        return new ArrayList();
    }

    @Override
    public void requestChildFocus(View child, View focused) {
        // avoid scrolling to focused element all together
    }
}

Layout XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <LinearLayout
        android:id="@+id/messaging_announcement_recipient_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="@color/screen_background"
        android:orientation="horizontal"
        android:padding="8dip">

        <TextView
            style="@style/default_textview"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginEnd="8dip"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="8dip"
            android:layout_marginStart="0dp"
            android:gravity="center_vertical"
            android:text="@string/recipient_to"
            android:textColor="@android:color/white" />

        <com.sevenshifts.android.views.NonFocusingScrollView
            android:id="@+id/messaging_announcements_recipients_scroll_view"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:padding="0dp">

            <com.sevenshifts.android.views.SevenChipsView 
                android:id="@+id/messaging_announcements_recipients"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="0dp"
                android:textColor="@android:color/white"
                app:cv_bg_color_clicked="@color/primary"
                app:cv_color_clicked="@color/primary"
                app:cv_icon_placeholder="@drawable/ic_house" />
        </com.sevenshifts.android.views.NonFocusingScrollView>

    </LinearLayout>

    <se.emilsjolander.stickylistheaders.StickyListHeadersListView
        android:id="@+id/messaging_announcements_recipient_autocomplete"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/messaging_announcement_recipient_container"
        android:visibility="gone" />

    <LinearLayout
        android:id="@+id/messaging_announcements_compose_post_container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="8dip"
        android:layout_marginRight="8dip"
        android:minHeight="44dip"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/messaging_announcement_post_text"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="@string/announcement_hint"
            android:inputType="textMultiLine|textCapSentences|textAutoCorrect"
            android:maxLines="5" />

        <Button
            android:id="@+id/messaging_announcement_post_button"
            android:layout_width="80dp"
            android:layout_height="44dip"
            android:background="@null"
            android:text="@string/send"
            android:textColor="@color/primary" />
    </LinearLayout>

    <include layout="@layout/component_loading" />

</RelativeLayout>

Screen shot flow is in this imgur gallery - there's too many to not crowd this post: https://i.stack.imgur.com/T1Ye8.jpg

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.