1

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>
Sam
  • 86,580
  • 20
  • 181
  • 179
Max
  • 572
  • 2
  • 6
  • 23

2 Answers2

3

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" >
Sam
  • 86,580
  • 20
  • 181
  • 179
  • What do you want the RelativeLayout's OnTouchListener to do? Will it conflict the the other View's basic functions? – Sam Nov 09 '12 at 23:34
  • basically I only want to that when someone touches outside the edit text the keyboard disappears. I have code that usually works with on touch, but since ontouch doesn't fire I am not sure how to resolve this. – Max Nov 09 '12 at 23:37
  • I do inflate something in the tablelayout that contains buttons so I can not override the whole table layout. – Max Nov 09 '12 at 23:38
  • I works but only when I click above the edit text. I will upvote already your answer since we are very close to solving the problem. – Max Nov 09 '12 at 23:54
  • What if you make the ScrollView `focusable` as well? – Sam Nov 09 '12 at 23:56
  • Unfortunately making the scrollview focusable does not work, neither does it work to make the table layout focusable. Sorry to have given such a tough question to your 200+. – Max Nov 10 '12 at 00:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19363/discussion-between-sam-and-max) – Sam Nov 10 '12 at 00:01
1

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

Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
Harsh
  • 599
  • 3
  • 20