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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:30:59+00:00 2026-06-13T03:30:59+00:00

(programming Android in Eclipse) I am trying to set up a delay in changing

  • 0

(programming Android in Eclipse) I am trying to set up a delay in changing the text in a button. I am getting errors only after there is a delay and the text needs to be changed. Here is the simplified code without a while loop:

final Button button_target = (Button) findViewById(R.id.button_target);        
    Thread textChange = new Thread(new Runnable() { 
        public void run(){
            button_target.setText("fog");
            try{
                Thread.sleep(3000);
            }catch(InterruptedException e){}                
        }
    });
    textChange.start();

And now here is the code where a change of text on the button is required after the sleep which now causes and error and exit (forced):

 final Button button_target = (Button) findViewById(R.id.button_target);        
    Thread textChange = new Thread(new Runnable() { 
        public void run(){
            button_target.setText("cat");
            try{
                Thread.sleep(3000);
            }catch(InterruptedException e){}                
            button_target.setText("dog");
        }
    });
    textChange.start();

what am I doing to cause the error? Is there another method that I should do to be able to invoke a sleep or delay to the thread so that a text change operation can be performed?

(the actual code has a while loop but I believe this form puts the error in highlight)

  • 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-13T03:31:01+00:00Added an answer on June 13, 2026 at 3:31 am

    You can’t change UI stuff on a thread like setText(), it must be done on the UIThread. See AsyncTask for more informations :

    http://developer.android.com/reference/android/os/AsyncTask.html

    Add more information

    You can use THREAD_POOL_EXECUTOR with AsyncTask in function of which versions of Android you use like this :

    AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
    
        @Override
        protected Void doInBackground(Void... voids) {
    
            try{
                Thread.sleep(3000);
                }catch(InterruptedException e){} 
            }
        }
    
        @Override
        protected void onPostExecute(Void result) {
            button_target.setText("dog");
        }
    
    };
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    } else {
        task.execute();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just begin programming with android home widget, so i could set text to
I am using Eclipse for Android programming. Is there a tool for view an
I'm programming an Android calculator in Eclipse. I want to be able to set
AM using for Eclipse for my android programming stuff. But after running my application
Recently started programming Android Java (Eclipse), Im trying to make a simple reader app
So I'm programming a simple calculator in Eclipse for Android 4.0 and I'm trying
I'm trying to learn some Android and java programming in Eclipse. I can run
I am new to the android programming and my working environment is eclipse helio
I recently followed a way of programming for Android using Scala and Eclipse ,
im currently reading a book about programming Android and there is a nice little

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.