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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:46:33+00:00 2026-05-21T12:46:33+00:00

I have a small program where i have 2 images, right now i am

  • 0

I have a small program where i have 2 images, right now i am doing OnTouch events for each image when clicked the image on top is set to invisible. This is working fine.. but having this in the main Activity is getting messy..

I created a TestView, I would like to move the code I have in the main activity to the testView, but I do not know where to put the OnTouch events and how to do the same from there?, I dont know where to put the OnTouch events and also how to draw the ImageViews from my testView.. when I was in the Main activity they drew fine.. but now.. i dont know how to draw them from the “testView extends View” class… can you help me organize it?

public class TestView extends View{


    public TestView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }
    public TestView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public TestView(Context context) {
        super(context);
                // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas); 


    } 

}

public class GameActivity extends Activity {    
/** Called when the activity is first created. */
public int ColorId;
public boolean isUserTurn = false;
public int globalIndex = 1;
@Override  
public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myviewlay);

final ImageView image1= (ImageView)findViewById(R.id.ImageView03); 
image1.setOnTouchListener(new OnTouchListener()
    {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub

        try {
            Thread.sleep(200);
         ColorId = 1;
        int action = event.getAction(); 

        if(action == MotionEvent.ACTION_DOWN){             

            image1.setVisibility(View.INVISIBLE);    

            }
        else
        {

            image1.setVisibility(View.VISIBLE);     
        }
        //return super.onTouchEvent(event);   
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return true;
        } 


        }); 
final ImageView image2= (ImageView)findViewById(R.id.ImageView02); 
image2.setOnTouchListener(new OnTouchListener()
    {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub

        try {
            Thread.sleep(200);
             ColorId = 2;
        int action = event.getAction();         
        if(action == MotionEvent.ACTION_DOWN){             

            image2.setVisibility(View.INVISIBLE);         
            }
        else
        {            
            image2.setVisibility(View.VISIBLE);     
        }
        //return super.onTouchEvent(event);  
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        return true;
        } 

        });

I just want to know, if i move it to the test view, how can i re organize it so that it works from there. I am very new to Android…

Thank you

  • 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-05-21T12:46:33+00:00Added an answer on May 21, 2026 at 12:46 pm

    For the onTouch events you just need to implement onTouchListener on your TestView. If both ImageViews are inside your TestView you need some kind of ViewGroup to do the layout of your custom view. If not you just extend ImageView instead of extending View and put your custom TestView in a XML layout and call setContentView(<your_layout>) in your activity.

    If not clear enough, say something 😛

    Hope it helped,
    JQCorreia

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

Sidebar

Related Questions

I have written a small program displaying sounds and images on the screen when
I have a small program that has several checkedboxlists in VS2010. I wanted to
I have a small program to order and sort email messages, outputting to a
I have a small program <350ish lines of code on 5 forms>. It works
i have a small program where an element is draged and dropped, when the
I have a small program to do in Java. I have a 2D array
I have a small program I want to execute to test something #include <map>
I have a small program that's supposed to sample some value from a device
I have a small program that reads in a file and processes the data
I have a small program that I'm trying to create to get ip addresses

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.