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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:26:17+00:00 2026-06-11T18:26:17+00:00

This has to be a very simple fix, but I can’t seem to figure

  • 0

This has to be a very simple fix, but I can’t seem to figure it out. I have got my program to change the background color to change onClick, and onTouch with ACTION_DOWN and ACTION_UP. But I need it to change the color when touching the screen and going in and out of the TextView. I need it to function like a mouseOver/mouseOut event. Is there a way to do this on android? Or am I stuck with onTouch, where the action has to start from within the TextView?

Right now I set the onTouchListener on the TextView itself. Should I set it somewhere else and then check if the x and y are within the Textview? Or is there another event listener I should be using? I am new to Android, any help would be appreciated. Thanks.

Mike

  • 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-11T18:26:18+00:00Added an answer on June 11, 2026 at 6:26 pm

    I would implement the OnTouchListener on your application and then on the onTouch method, keep checking if the current position of the touch event is within the bounds of the bounding box of the view. If it is, apply the new background and if it isn’t apply the original one.

    Since all the views implement the setBackgroundColor I didn’t do any casting to TextView but the example should suffice, at least as a starting point to develop your application further.

    The full code for this is the following:

    public class Main extends Activity implements OnTouchListener{
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            //Set the listener for the parent/container view
            findViewById(R.id.cont).setOnTouchListener(this);
    
            //Get a hold of the view and create the Rect for the bounds
            View target = findViewById(R.id.target);
            Rect b = new Rect();
    
            //Get the bounding box of the target view into `b`
            target.getGlobalVisibleRect(b);
        }
    
        public boolean onTouch(View v, MotionEvent event) {
    
            //Check if it's within the bounds or not
            if(b.contains((int)event.getRawX(),(int) event.getRawY())){
                target.setBackgroundColor(0xffff0000);
            }else{
                target.setBackgroundColor(0xff000000);
            }
    
            //You need to return true to keep on checking the event
            return true;
        }
    }
    

    As for the user interface for the previous code, it’s just a linear layout with an ID cont and a view (a TextView in your case) with an ID target. The rest is totally by default so there is no point in me pasting it here. Note I only tested this on an emulator and ymmv when trying it on real devices, but as far as I can think of, it should be fine.


    Relevant documentation:

    • getGlobalVisibleRect method
    • onTouchListener
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm hoping this question has a very simple answer. I can think of ways
Im very sorry if this has been answered before but I have been looking
Okay guys, I'm sure this has a very simple solution but my searches are
This is probably a very simple fix but I've been unable to find an
I wrote this very simple macro to delete all rows when Column P has
I'm generally not a fan of microbenchmarks. But this one has a very interesting
I have a messaging system (very basic) that has a table like this: **MESSAGE_ID**
I have a very simple silverlight interface that has a bunch of links on
I'm very experienced with CSS but this one has me confused - apologies if
First of all thanks in advance, this has been very frustrating and I'm hoping

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.