Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8093107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:25:58+00:00 2026-06-05T20:25:58+00:00

I have a LinearLayout and I want to see when a user has touched

  • 0

I have a LinearLayout and I want to see when a user has touched it, and has released their finger. I thought I could do this by adding a onTouch listener and checking the event. This linearLayout holds a bunch of buttons.

My problem isn’t the listener not working, it just isn’t doing what I thought it would do. The listener only gets activated if ONLY the layout has been touched, which makes sense, but isn’t what I want. If the user touches a button inside of the layout the layout listener isn’t called, and I want it to be called.

Here is my code:

java code:

    ll = (LinearLayout)findViewById(R.id.CategorySelect_lLayout_noID);
    ll.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {

            if(event.getAction() == MotionEvent.ACTION_DOWN){
                Toast.makeText(CategorySelect.this, "action DOWN called", Toast.LENGTH_SHORT).show();
                return true;
            } 
            if(event.getAction() == MotionEvent.ACTION_UP){
                Toast.makeText(CategorySelect.this, "action Up called", Toast.LENGTH_SHORT).show();
                return true;
            }
            return false;
        }
    });

my xml:

  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="400dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:scrollbars="none"
 >

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

        <Button
            android:id="@+id/CategorySelect_b_1"
            style="@style/CustomBLUEButtonTextStyle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/CategorySelect_b_1"
        />

        <Button
            android:id="@+id/CategorySelect_b_2"
            style="@style/CustomBLUEButtonTextStyle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/CategorySelect_b_2" />

        <Button
            android:id="@+id/CategorySelect_b_3"
            style="@style/CustomBLUEButtonTextStyle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/CategorySelect_b_3" />

        <Button
            android:id="@+id/CategorySelect_b_4"
            style="@style/CustomBLUEButtonTextStyle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/CategorySelect_b_4" />

        <Button
            android:id="@+id/CategorySelect_b_5"
            style="@style/CustomBLUEButtonTextStyle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/CategorySelect_b_5" />

        <Button
            android:id="@+id/CategorySelect_b_6"
            style="@style/CustomBLUEButtonTextStyle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/CategorySelect_b_6" />

        <Button
            android:id="@+id/CategorySelect_b_7"
            style="@style/CustomBLUEButtonTextStyle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/CategorySelect_b_7" />
    </LinearLayout>

</ScrollView>

Thanks for reading.

UPDATED CODE FOR COMMENTS:

My main xml file that is inflated from my activity:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/categoryselect_background"
android:scaleType="fitXY"
 >

<TextView
    android:id="@+id/CategorySelect_tv_chooseacat"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="70dp"
    android:gravity="center"
    android:text="@string/CategorySelect_tv_chooseacat"
    android:textColor="#FF5500"
    android:textSize="34dp"
    android:textStyle="bold" 
    />

<Button
    android:id="@+id/CategorySelect_b_settings"
    android:background="@drawable/menu_settings"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
     />

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="400dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:scrollbars="none"
>

    <com.tdubstudios.soundboard.hungergames.ScrollAbleLinearLayout
        android:id="@+id/CategorySelect_lLayout_noID"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <Button
            android:id="@+id/CategorySelect_b_cato"
            style="@style/CustomBLUEButtonTextStyle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/CategorySelect_b_1"
        />

        <Button
            android:id="@+id/CategorySelect_b_cinna"
            style="@style/CustomBLUEButtonTextStyle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/CategorySelect_b_2" />

        <Button
            android:id="@+id/CategorySelect_b_effie"
            style="@style/CustomBLUEButtonTextStyle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/CategorySelect_b_3" />

        <Button
            android:id="@+id/CategorySelect_b_gale"
            style="@style/CustomBLUEButtonTextStyle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/CategorySelect_b_4" />

        <Button
            android:id="@+id/CategorySelect_b_haymitch"
            style="@style/CustomBLUEButtonTextStyle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/CategorySelect_b_5" />

        <Button
            android:id="@+id/CategorySelect_b_katniss"
            style="@style/CustomBLUEButtonTextStyle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/CategorySelect_b_6" />

        <Button
            android:id="@+id/CategorySelect_b_peeta"
            style="@style/CustomBLUEButtonTextStyle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/CategorySelect_b_7" />
    </com.tdubstudios.soundboard.hungergames.ScrollAbleLinearLayout>

</ScrollView>
</RelativeLayout>

This is the ScrollAbleLinearLayout class:

package com.tdubstudios.soundboard.hungergames;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

public class ScrollAbleLinearLayout extends LinearLayout {


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

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    requestDisallowInterceptTouchEvent(true); // Tell parents not to use this event
    return false; // Tell children not to use this event
}
}

Yes I know the naming on some things have changed from my previous code, that is because I changed it to hide what type of app I was making (from search results). However for whatever reason I no long care, so that’s the reasoning behind that.

Thanks.

THIRD EDIT:

So I’m currently using

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    //requestDisallowInterceptTouchEvent(true); // Tell parents not to use this event
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        return false;
    } 
    if(event.getAction() == MotionEvent.ACTION_UP){
        return false;
    }


    return true; // Tell children not to use this event
}

And it works the way I want it to, however it isn’t practical. For it to actually register the button clicks you have to make sure you don’t move your finger at all left/right/up/down. It has to be like exactly down and exactly up. I was trying to add a onFling method, and then tracking the distance the finger as moved. And if it has moved less then x amount of pixels then make the main method return false which will allow the buttons to work, but if it was actually a swipe/move then the button doesn’t need to get activated. Here is what I mean:

@Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        if (Math.abs(e1.getY() - e2.getY()) > MORE_THAN_X_INT) {
            booleanVar = false;
            return false;
        }else{
          booleanVar = true;
         }
      return false;
   }

Then doing:

Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    //requestDisallowInterceptTouchEvent(true); // Tell parents not to use this event
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        return false;
    } 
    if(event.getAction() == MotionEvent.ACTION_UP){
        return false;
    }


    return booleanVar; // Tell children not to use this event
}

I think that would work, but I can’t test it because there isn’t a onFling method for extends LinearLayout

Any suggestions?

Please note my returning true/false might be backwards, I’m not sure it’s kind of confusing me. But if someone could lead me in a way to making that onFling method I can mess around with it to get it the correct values.


I have

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    //requestDisallowInterceptTouchEvent(true); // Tell parents not to use this event
    while(event.getAction() == MotionEvent.ACTION_DOWN){
        return true;
    } 
    while(event.getAction() == MotionEvent.ACTION_UP){
        return true;
    }


    return false; // Tell children not to use this event
}

and my buttons are never click able. But the ontouch listener works perfectly haha 😐

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:25:59+00:00Added an answer on June 5, 2026 at 8:25 pm

    I would recommend extending LinearLayout and using onInterceptTouchEvent to catch the touch event, then return true to force its children to not receive the events.

    Something like this:

    public class MyLinearLayout extends LinearLayout {
        public MyLinearLayout(Context context) {
            super(context);
        }
        public MyLinearLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        public MyLinearLayout(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
        @Override
        public boolean onInterceptTouchEvent(MotionEvent event) {
            return true; // Tell children not to use this event
        }
    }
    

    And, then in your XML, change LinearLayout to com.yourpackagename.MyLinearLayout.

    Addendum: If you want both the Buttons and the LinearLayout to receive the event, you can change the onInterceptTouchEvent to this:

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        // Whatever touch event code you want, using the "event" variable
        return false; // Allow children to use the event
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a LinearLayout, which only contains one button. I want this button to
For a linearLayout, I want to have a gradient along with a tiled (repeated)
I have a LinearLayout which holds a couple of buttons. To give the user
Just a straight forward problem. I have a LinearLayout which has attribute in xml
i have many views (textview) in linearlayout i want each view id when i
I have a SQLite db and I want to see in a ListView all
I have a SQLite db and I want to see in a ListView all
Here is what I want to do: I have this scale xml: <?xml version=1.0
I have a linearlayout with 3 imageView and a listview in vertical orientation.i want
I have a LinearLayout view that already contains several elements. I want to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.