3

I am using scroll view inside RelativeLayout and I am assigning onClickLIstener to RelativeLayout.

I am using following code. Layoutfile

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:id="@+id/row2_lnr_layout_ver">


                     <ScrollView
                           android:id="@+id/row2_scrollview"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:fadeScrollbars="false"
                        android:scrollbarAlwaysDrawVerticalTrack="true"
                        android:scrollbars="vertical" 
                        android:fillViewport="true">

                        <TextView
                            android:id="@+id/sa_sub_txt_view_2"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:layout_marginLeft="10dp"
                            android:layout_weight="0.4"
                            android:paddingLeft="10dp"
                            android:text="hjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj"
                            android:textColor="@android:color/black">
                        </TextView>

                   </ScrollView>
</RelativeLayout>

Java File in OnCreate()

((RelativeLayout)findViewById(R.id.row2_lnr_layout_ver)).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.i("MainActivity", "onSmartActionClicked");
            Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();  
        }
    });

So when I click on TextView or scrollview, it does not show Toast. But when I remove scroll view and click on textview, then it works fine. Can anybody tell me why this strange behavior while using ScrollView??

Rohit
  • 2,646
  • 6
  • 27
  • 52

2 Answers2

0

Can you please try adding the following touch listener for the TextView and after that, set the onclick Listener for the Textview rather than giving it to the Relative Layout.

private OnTouchListener textViewTouchListener= new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            v.getParent().requestDisallowInterceptTouchEvent(true);
            break;

        case MotionEvent.ACTION_MOVE:
            v.getParent().requestDisallowInterceptTouchEvent(false);
            break;

        case MotionEvent.ACTION_UP:
            v.getParent().requestDisallowInterceptTouchEvent(false);
            break;
        }

        v.onTouchEvent(event);
        return true;
    }
};
Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44
  • I wanted to add click listener to RelativeLayout demanded by my app. So I cant use it to listener to TextView.. – Rohit Nov 18 '14 at 10:55
0

Try requestDisallowInterceptTouchEvent from ViewParent: http://developer.android.com/reference/android/view/ViewParent.html#requestDisallowInterceptTouchEvent(boolean)

public boolean onTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
      this.getParent().requestDisallowInterceptTouchEvent(true);
    }
    if (ev.getAction() == MotionEvent.ACTION_UP) {
      this.getParent().requestDisallowInterceptTouchEvent(false);
    }

    return super.onTouchEvent(ev);
  }

android scrollable textview inside scrollview

Community
  • 1
  • 1
SubinM
  • 394
  • 3
  • 7