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 7874491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:48:58+00:00 2026-06-03T02:48:58+00:00

I have a linear layout which holds inner (child) custom image view. My goal

  • 0

I have a linear layout which holds inner (child) custom image view.

My goal is to set a background selector on the image view, and also set a listener
to the linear layout.

however, I can’t make the selector and the listener work together.
I don’t know how to handle the events.

If I make a quick tap it is working, but If I drag my finger on the views, the image view
remains in selected state.

Here’s the activity which has a reference to the linear layout and its listener.

public class EventDispatchingDemoActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.my_layout);

        linearLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v)
            {
                Toast.makeText(getBaseContext(), "Click", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Here’s the custom view (Its a code I found somewhere, credits for the author)

public class ImageSelector extends ImageView
{

    public ImageSelector(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        setBackgroundDrawable(new NavStateListDrawable(getContext(), 0));
        setClickable(true);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        super.onTouchEvent(event);
        return false;
    }

    public class NavStateListDrawable extends StateListDrawable
    {

        private int level;

        public NavStateListDrawable(Context context, int level)
        {

            this.level = level;
            // int stateChecked = android.R.attr.state_checked;
            int stateFocused = android.R.attr.state_focused;
            int statePressed = android.R.attr.state_pressed;
            int stateSelected = android.R.attr.state_selected;

            addState(new int[] { stateSelected }, context.getResources().getDrawable(R.drawable.slide_feedback));
            addState(new int[] { statePressed }, context.getResources().getDrawable(R.drawable.slide_feedback));
            addState(new int[] { stateFocused }, context.getResources().getDrawable(R.drawable.slide_feedback));
            addState(new int[] { -stateFocused, -statePressed, -stateSelected }, context.getResources().getDrawable(android.R.color.transparent));
        }

        @Override
        protected boolean onStateChange(int[] stateSet)
        {

            boolean nowstate = super.onStateChange(stateSet);

            try
            {
                LayerDrawable defaultDrawable = (LayerDrawable) this.getCurrent();

                LevelListDrawable bar2 = (LevelListDrawable) defaultDrawable.findDrawableByLayerId(R.id.element_contact_image_selector);
                bar2.setLevel(level);
            }
            catch (Exception exception)
            {

            }

            return nowstate;
        }
    }
}

And Here’s the xml:

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

    <!-- outer layout -->

    <LinearLayout
        android:id="@+id/my_layout"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#00ff00"
        android:gravity="center" >

        <!-- inner layout -->

        <dor.event.dispathing.ImageSelector
            android:id="@+id/inner_layout"
            android:layout_width="150dp"
            android:layout_height="150dp" />
    </LinearLayout>

</LinearLayout>

I Uploaded the source:
http://www.filefactory.com/file/5ikfwyk9j5bx/n/EventDispatchingDemo.rar

Thanks in advance for your help!

  • 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-03T02:48:59+00:00Added an answer on June 3, 2026 at 2:48 am

    I remember having a similar issue when I was writing my application.

    1) Are you trying to capture if a row in your listview is clicked? I think you want `listView.setonItemClickListener’ which will capture clicks for each individual row as opposed to the entire listview.

    2) I believe when you have a listview with clickable items inside it, the clickable items inside it take priority in terms of capturing clicks. More than taking priority, I believe they block any click events from being sent back to the parent’s event handlers.

    I believe a somewhat working solution is to set the focusability of those clickable items to false. Once those clickable items are not longer focusable, the listview row will get focus back. The other solution is to work with the descendantFocusability property which controls who (Entire row or Image) gets first crack and picking up events triggered within the row. Here are some other questions that seem to have what you need:

    How to fire onListItemClick in Listactivity with buttons in list?

    Android ListView with clickable items in it's rows causes problems continuing scrolling

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi i have a linear layout and i want to show my custom view
I have a Linear layout with vertical orientation in which I'm inserting several view
I have a linear layout that contains 6 images, which span the linear layout
I have a linear layout which contains an imageview and another linear layout which
I have a horizontal linear layout with an image and a couple text views.
I have a linear layout which consists of imageview and textview , one below
I have a gallery which has 7 + linear layouts in each view, and
I have a linear layout (horizontal) inside which there is 3 linear layouts, each
i m developing application in which i have to change the background image on
I have a linear layout with this form <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=vertical

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.