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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:32:27+00:00 2026-06-09T15:32:27+00:00

I need the Button to change the layout when I hold it down for

  • 0

I need the Button to change the layout when I hold it down for 3 seconds, but when I push once quickly it should just play a sound.

In the code below the sound comes no matter what (because that’s okay in this case).

My problem is that it only changes page the very first time you use the Button whether it’s held down (to change layout) or just a short click (only to play sound).
So if I hold down button and it changes layout, but then I go back it won’t work a second time. Also, if a short touch is used to play the sound I can’t hold the button down to change layout.

final Button b1pad1 = (Button) findViewById(R.id.b1pad1);
b1pad1.setOnTouchListener(new View.OnTouchListener() {

    public boolean onTouch(View v, final MotionEvent event) {
        // TODO Auto-generated method stub
        switch(event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            MediaPlayer mp = MediaPlayer.create(PhotosActivity.this, R.raw.sub);
            mp.start();
            holdTimer = new Thread() {
                public void run() {
                    try {
                        int timer = 0;
                        while (timer<3000) {
                            sleep(100); 
                            if (event.getAction() == MotionEvent.ACTION_UP) { 
                                //checks whether you lifted your finger before 
                                //the 3 seconds.
                                breaked = "no"; 
                                //"no" means "don't change layout"
                                changetobrowser(b1pad1, breaked);
                                break;    
                            }
                            timer = timer + 100;
                        }
                        if (breaked.equals("")){
                            breaked = "go"; //go means "change the layout"
                            changetobrowser(b1pad1,breaked);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };

            holdTimer.start();
            break;

        case MotionEvent.ACTION_UP:
            // End
            break;
        }

        return false;
    }
});

public void  changetobrowser(Button but, String breaked) {
    if (breaked.equals("go")){
        Intent browseIntent = new Intent(PhotosActivity.this, BrowseScreen.class);
        startActivity(browseIntent); 
    } else {
    }
}

What could the problem be? And is this even the easiest way to do it?

  • 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-09T15:32:28+00:00Added an answer on June 9, 2026 at 3:32 pm

    Have you considered using an OnLongClickListener? It’s invoked when a View has been clicked and held for a short while. For other cases, you can use a simple OnClickListener, which is invoked upon press.

    More information here: http://developer.android.com/reference/android/view/View.OnLongClickListener.html

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

Sidebar

Related Questions

I have this situation: $(#button).toggle(function(){ $(#window).animate({top:'0%'},1000); },function(){ $(#window).animate({top:'-100%'},1000); }); but I need change it
I need to change the background colour of a button using C# code (Visual
I need that on every page change or on some elemetents (a, input[button, submit],
Possible Duplicate: Android Button: set onClick background image change with XML? I need that
When i click on OK button of an UIAlertView, i need to change the
I need to change the layout of the Search Block in Drupal. It’s currently
There is a button in the layout of the rows of my list.I need
I need a little help.I'm using a ViewFlipper to change views in TabActivity but
I have 3 buttons to change language. So I need to make method, where
I have following code which works for radio buttons but need to be changed

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.