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

  • Home
  • SEARCH
  • 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 9007075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:35:15+00:00 2026-06-16T01:35:15+00:00

I’m working on adding an onClick highlight to a view in a drag and

  • 0

I’m working on adding an onClick highlight to a view in a drag and drop grid in Android. The project is on GitHub (the dev branch contains the code that is using the selector).

The problem I am having is in the adapter when I set the onClick selector as a view’s background, when I click a view, instead of changing the background for just that view the selector changes the backgrounds for all the views.

I’m pretty sure this is a result of how the clicks are being handled in onTouch in the ViewGroup (below). I’m not sure if I should I be returning true/false differently or triggering the onClick listener differently to prevent all of the views from being highlighted.

Any ideas/help is appreciated, thanks.

@Override
public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        touchDown(event);
        break;
    case MotionEvent.ACTION_MOVE:
        touchMove(event);
        break;
    case MotionEvent.ACTION_UP:
        touchUp(event);
        break;
    }
    if (aViewIsDragged())
        return true;
    return false;
}

private void touchUp(MotionEvent event) {
    if(dragged == -1) {
        if(onClickListener != null) {
            View clickedView = getChildAt(getTargetAtCoor((int) event.getX(), (int) event.getY()));
            if(clickedView != null)
                onClickListener.onClick(clickedView);
        }
    } else {
        manageChildrenReordering();
        hideDeleteView();
        cancelEdgeTimer();

        movingView = false;
        dragged = -1;
        lastTarget = -1;
        container.enableScroll();
        cancelAnimations();
    }
}

EDIT: After looking at the ViewGroup documentation android:addStatesFromChildren sounds like what I am looking for, but it doesn’t seem to have any effect.

android:addStatesFromChildren

Sets whether this ViewGroup’s drawable states also include its
children’s drawable states. This is used, for example, to make a group
appear to be focused when its child EditText or button is focused.

Must be a boolean value, either “true” or “false”.

This may also be a reference to a resource (in the form
“@[package:]type:name”) or theme attribute (in the form
“?[package:][type:]name”) containing a value of this type.

This corresponds to the global attribute resource symbol
addStatesFromChildren.

UPDATE

I have found a way to solve this. The pull request is here

  • 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-16T01:35:16+00:00Added an answer on June 16, 2026 at 1:35 am

    Edit

    Add layout.setClickable(true); to the view function (somewhere between lines 133 and 156) to make each item clickable. This is required for them to have their own select-able state. Without it, the grid is receiving the touch and that state gets passed down to each child. This breaks your onTouch functionality, but that would be a separate question.

    If you don’t want to worry about changing the functionality of your onTouch, you could manually change the background of the selected item in the different touch functions instead of using a selector


    Original that missed the point:

    Maybe I’m missing something, but I would think that you could pass the View “v” from onTouch and use that as clickedView instead of trying to figure out which view it is based on location.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a view passing on information from a database: def serve_article(request, id): served_article
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.