Virthuss
Virthuss

Reputation: 3213

Android layout focus issue: layout clickable, but its element steal focus

Here is my issue with android.

I'm using a xml file as a template of layout I'm inflating several times in order to get a list of displayable and clickable layouts. This template-layout is filled with textviews and buttons that are only visual ( no interaction planned with them at all ).

<ScrollView
        android:id="@+id/scroller"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:fillViewport="true"
        android:layout_above="@+id/bottomcontent">

        <LinearLayout
            android:id="@+id/scrollcontentcontainer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            //Dynamically added layouts go there

        </LinearLayout>
    </ScrollView>

Visually there's no problem. But I'm suppose to be able to touch all of those layouts in order to display an other fragment with more informations in it.

The thing is I have to touch the screen several times, or some specific part of the layout where there is no elements to be able to make the onclick method work.

Given that the layout is fill with button and textviews, all set to clickable and focusable false, is there a way to force a layout to be the receiver of the click and not one of its element? because it seems to be a focus problem linked to the xml file...

Here's the template of layout used to be inflated several times:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/cornerbackground"
android:clickable="true"
android:focusable="true"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">

   //buttons views textviews and any random stuff go there

</RelativeLayout>

Thanks!

Upvotes: 1

Views: 710

Answers (1)

Anitha Manikandan
Anitha Manikandan

Reputation: 1170

I have faced the same issue. What I did was, I have created a custom layout which will take all the touch events, and added in the layout.

public class CustomRelativeLayout extends RelativeLayout {
    public CustomRelativeLayout(Context context) {
        super(context);
    }

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

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

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }
}

You need to use this layout in xml.

Upvotes: 1

Related Questions