Max
Max

Reputation: 572

Fragment using ScrollView inside RelativeLayout > ontouch doesn't work

I have been trying to find a solution for this for hours and it feels like I searched every question on stackoverflow and tried everything in the layout. Before I tried the layout displayed below, the edittext and the two imagebuttons were inside the scrollview. In this old layout the ontouch worked perfectly in my fragment, however once I pulled out the relative layout with the edittext and the two image buttons the ontouch was not fired. Can someone help me with this problem? Any help is appreciated. Thanks

View fragmentView = inflater.inflate(R.layout.request, container, false);

fragmentView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

            }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"
        android:background="#BDCCE0" >

    <EditText
        android:id="@+id/etsearch"
        android:layout_width="210dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6dp"
        android:layout_marginTop="10dp"
        android:hint="search..."
        android:singleLine="true"
        android:imeOptions="actionDone" />

    <ImageButton
        android:id="@+id/bsearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/etsearch"
        android:layout_marginTop="9dp"
        android:src="@drawable/search" />

    <ImageButton
        android:id="@+id/bupdaterequests"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_toRightOf="@id/bsearch"
        android:layout_marginTop="9dp"
        android:src="@drawable/refresh" />

  <ScrollView
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:fillViewport="true"
        android:background="#BDCCE0"
        android:layout_below="@id/etsearch" >

   <TableLayout
       android:id="@+id/myTableLayout"  
       android:layout_width="fill_parent"  
       android:layout_height="wrap_content" >    

   </TableLayout> 
 </ScrollView>
</RelativeLayout>

Upvotes: 1

Views: 3887

Answers (2)

Harsh
Harsh

Reputation: 599

android:clickable="true" resolved the issue..

Upvotes: 1

Sam
Sam

Reputation: 86958

A TouchEvent starts at the "highest" element and trickles "down" the View hierarchy until it is consumed by some event. In this case the ScrollView, Buttons, or EditText all consume the event before it reaches the RelativeLayout...


basically I only want to that when someone touches outside the edit text the keyboard disappears.

I addressed this by simply making the root layout clickable and focusable.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true" >

Upvotes: 3

Related Questions