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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:33:11+00:00 2026-06-12T17:33:11+00:00

I have a bunch of Buttons (custom, with a few extra methods to apply

  • 0

I have a bunch of Buttons (custom, with a few extra methods to apply other backgrounds, and revert to original), which can be assigned a background. But since I don’t know how large these backgrounds will or can be, I decided to set the background in a separate Thread. First, I had this code:

public void updateButton(final Button b, final String d, final String s) {
    b.nullify(); //Recycles previous background, else OOM error
    b.setText(s);
    if (d != null) {
        new Thread() {
            @Override
            public void run() {
                b.setBackgroundFromBase64(d);
            }
        }.run();
    }
    else b.setBackgroundToDefault(); //Sets standard android background
}

but I soon found out that this wasn’t the way to go. Slowly, the memory got flooded when I called this method a few thousand times. When I removed the new Thread() part, the memory wasn’t flooded, so the Thread was the cause of this (Memory Analyzer Tool told me the same).

Then I tried the AsyncTask:

private class UpdateButtonTask extends AsyncTask<Object, Void, Void> {
    @Override 
    protected Void doInBackground(Object... objs) {
        String s = (String)objs[0];
        Button b = (Button)objs[1];
        String d = (String) objs[2];
        b.nullify(); //Recycles previous background, else OOM error
        b.setText(s);
        if (d != null) b.setBackgroundFromBase64(d);
        else b.setBackgroundToDefault();
        return null;
     }
     @Override
     protected void onProgressUpdate(Void v) {
         //As far as I know, I don't need this method
     }
     @Override
     protected void onPostExecute(Void v) {
         //Neither this one
     }
 }

and call

new UpdateButtonTask().execute("Button", myButton, base64String);

in the button update method. But that didn’t work either (the button wasn’t updated at all. No text, no custom background). I read some articles and questions about Threading (this, this, and this amongst others), but I can’t seem to get it working. For now, no Thread and hoping the phones are fast enough to not cause trouble on the UI thread seems to be the best option, but I’d rather have something more reliable, like a Thread.
What am I doing wrong? or maybe just the full question How can I update a Button background on a background Thread (so the UI doesn’t stop working when applying larger backgrounds)?

  • 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-12T17:33:13+00:00Added an answer on June 12, 2026 at 5:33 pm

    I’m guessing that the problem is that you’re trying to update the UI on a non-UI thread. This throws an exception and kills your AsyncTask.

    You should separate the processing-intensive tasks to the AsyncTask and then apply UI changes on the main thread (using runOnUiThread(Runnable)).

    Since you’re using a custom button, I don’t really know what you’re not allowed to do on a non-UI thread. I’m guessing the setText function is causing problems, if you haven’t overridden it.

    You should start by wrapping the whole body of your doInBackground method in a try/catch block and logging the exception to isolate the problem.

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

Sidebar

Related Questions

So I have a bunch of buttons on a page, interspersed with other HTML,
I'm trying to have a page with a bunch of buttons, each of which
I have a bunch of radio buttons, and depending on the choices of other
I have a bunch of buttons that have a tapGestureRecognizer linked to them, and
I have a whole bunch of buttons that all need to have both a
I have a main menu activity with a bunch of buttons. One button starts
i have a table which contains a bunch of dynamically created radio button lists,
I have bunch of strings, some of which are fairly long, like so: movie.titles
We have bunch of Domain Entities which should be rendered to an html format,
I have a bunch of buttons named: button1 button2 button3 etc. Is there a

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.