1

I have a scroll view that has a Linearlayout child and this linearlayout has a drawingview.

the problem : I tried almost all the available answers on this topic and none of them worked, I cant understand why click is not fired for (scrollview, linear layout and even drawingview).

note : I tried setting on click for each of the three and none worked.

the xml code:

<ScrollView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@id/scrollview"
android:clickable="true"
>

<LinearLayout
android:setorientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@id/linear"
android:clickable="false">


<Customview
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@id/draw"
android:clickable="false"/>

</LinearLayout>
</ScrollView>
Charuක
  • 12,953
  • 5
  • 50
  • 88
Hasan B.T
  • 51
  • 1
  • 7

2 Answers2

0

You have set android:clickable="false" for both make it true it will work for you

<ScrollView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:id="@id/scrollview"
    android:clickable="true"
    >

    <LinearLayout
    android:setorientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@id/linear"
    android:clickable="true">


    <Customview
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@id/draw"
    android:clickable="true"/>

    </LinearLayout>
    </ScrollView>
santosh kumar
  • 2,952
  • 1
  • 15
  • 27
0

First your ScrollView ,layout_height is wrapped! and for children you gave match_parent i doubt your view won't even display. Isn't it?

Then no idea why you use setorientation and android:id="@id/draw" instead of android:orientation and android:id="@+id

If you want to check scrolling works use

   scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                Log.d("TAG", "onScrollChanged: ");
                // DO SOMETHING WITH THE SCROLL COORDINATES
            }
        });

You are using unnecessary attributes check this example :

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

    <ScrollView

        android:id="@+id/scrollIndicatorDown"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#823">

        <LinearLayout
            android:id="@+id/first_child"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="#900"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/linear_layout"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:background="#000"
                android:orientation="vertical"></LinearLayout>

            <LinearLayout
                android:id="@+id/web_view"
                android:layout_width="match_parent"
                android:layout_height="500dp"
                android:background="#987"
                android:orientation="vertical"></LinearLayout>


        </LinearLayout>
    </ScrollView>

</LinearLayout>

If you write an onClickListner to the id linear_layout it just work normally!


Answer :The child Views of your ScrollView are consuming the click events you do on the ScrollView.

The solution to this problem is to set the onClickLIstener to the immediate child of the ScrollView(It's child).

So according to my example if i want to find out the ScrollView onClick i write my listener to its child.

 scrolChild =(LinearLayout) findViewById(R.id.first_child) ;


        scrolChild .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(YourActivityName.this, "You Clicked Me OH yeaaaaa", Toast.LENGTH_SHORT).show();
            }
        });

Read here when you need androidclickable-true android:clickable="true" mean's that it's not clickable?

This is my activity

   public class MainActivity extends AppCompatActivity {


    private ScrollView scrollView;

    private LinearLayout scroolChild;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web);

        scrollView = (ScrollView) findViewById(R.id.scrollIndicatorDown);
        scroolChild =(LinearLayout) findViewById(R.id.first_child) ;


        scroolChild.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "You Clicked Me OH yeaaaaa", Toast.LENGTH_SHORT).show();
            }
        });

        scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                Log.d("TAG", "GotAScrollChanged: ");
                // DO SOMETHING WITH THE SCROLL COORDINATES
            }
        });


    }
}
Community
  • 1
  • 1
Charuක
  • 12,953
  • 5
  • 50
  • 88
  • these are typing mistakes but I have them correctly , I didnt copy paste them I typed them , now how I set on click listener to the scroll view please. – Hasan B.T Jan 07 '17 at 19:07
  • @Hasan B.T use my examaple try it if it works then *compere* then you will learn that's your exercise :)) – Charuක Jan 07 '17 at 19:11
  • where does the custom view go? – Hasan B.T Jan 07 '17 at 19:18
  • @Hasan B.T its just an example that i added cuz your view was wired i added what you want as answer at the bottom – Charuක Jan 07 '17 at 19:20
  • @Hasan B.T if you want to get the click of scroll view , use it's first child write an onClick to that one and you are good to go :)) – Charuක Jan 07 '17 at 19:24
  • I did what you said , I set on click to the very first child and nothing happened. – Hasan B.T Jan 07 '17 at 19:33
  • @Hasan B.T are you sure ? then add a `android:clickable="true"` to that view and see but its unnessarory – Charuක Jan 07 '17 at 19:36
  • @Hasan B.T I posted my activity class as well and i used my xml that i show you in the answer if it does not work tell me – Charuක Jan 07 '17 at 19:40
  • now my scroll view is found in a frame layout not in linear layout like you did I dont know if that changes anything? – Hasan B.T Jan 07 '17 at 19:42
  • @Hasan B.T indeed ! its problamatic,I recommend not to use the FrameLayout at all(for scrol child), since it is just a useless container,use a linear layout in this scenario – Charuක Jan 07 '17 at 19:44
  • @Hasan B.T No wait if its outside i don't think thats a problem, is a problem when it act as a child of ScrollView .. I am saying it again it only problamatic if it comes under ScrollView as a child but as a parent its fine – Charuක Jan 07 '17 at 19:46
  • I appreciate the time you gave me to explain, all your explanation is clear and perfect and I discovered that as you said the first child is the one that can be set to on click, but I also discovered that if the first child of scrollview is drawing view it is not being clciked. – Hasan B.T Jan 07 '17 at 20:07
  • @Hasan B.T I dunno what view you use. you have not given any detail.If it is a problem put a linear layout again as the child and add your whatever the drawing view inside it as a child.problem solved! – Charuක Jan 07 '17 at 20:11
  • If it helps you can consider of accepting the answer XOXO – Charuක Jan 07 '17 at 20:14