0

I want to change the background color of a textview when I press a button. It should do this: first be white for 10ms, and then just the regular color. Is there some kind of delay function or do I need to write my own function for this using a loop or some kind? Any tip is greatly appreciated :)

At this moment I just use

button.setBackgroundColor(Color.parseColor("#ffa500"));
Darin Kolev
  • 3,401
  • 13
  • 31
  • 46
Matthias Vanb
  • 923
  • 5
  • 14
  • 32
  • 1
    You can find some possibilities here: http://stackoverflow.com/questions/3072173/how-to-call-a-method-after-a-delay – Darin Kolev Dec 27 '12 at 10:37

3 Answers3

6

every view have post and postDelayed methods to respectively post a runnable to the UI thread or post delayed it.

    button.postDelayed(new Runnable() {

    @Override
    public void run() {
    // change color in here
    }
}, 10);

edit: if you're going to be calling this very often, you can do it even better with something like this:

int currentColor;
private Runnable changeColorRunnable = new Runnable() {

    @Override
    public void run() {
        switch(currentColor){
        case Color.RED: currentColor = Color.BLACK; break;
        case Color.BLACK: currentColor = Color.RED; break;
        }
        button.setBackgroundColor(currentColor);

    }
};

and then:

    button.postDelayed(changeColorRunnable, 10);

this will avoid unnecessary object creation and garbage collection

Budius
  • 39,391
  • 16
  • 102
  • 144
  • Awesome! This does it! Pretty straightforward actually :D – Matthias Vanb Dec 27 '12 at 10:54
  • 1
    hey thanks. I even updated my answer with an even better way. It's a bit less straight forward but, you're saying that it will be called very often, so you can do this way to use less memory. – Budius Dec 27 '12 at 11:01
  • Just as info, i am new to android programming, but why would this use less memory? It does call the run method anyway? – Matthias Vanb Dec 27 '12 at 11:42
  • Mobile platforms are very memory constrained, so when I'm programming, I'm paranoid up to the last byte on saving it. Said that, basic Java and object orientation. Every time it goes through the keyword 'new', it creates a NEW object in the memory, and if this object won't be used again it will later be garbage collected (which will take some extra processing). This way it uses less memory because it only creates ONE runnable, and re-use the same runnable again and again. Makes sense? But as I said, that only makes sense if it will be called more than once. – Budius Dec 27 '12 at 11:49
1

The easiest way to do that would be to create an handler and to execute it with postDelayed : http://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable, long)

ben
  • 1,151
  • 10
  • 20
0
 private class ButtonColorChange extends AsyncTask<String, Void, String>
 {
    protected void onPreExecute() 
    { 
         //do
    }

    protected String doInBackground(String... params)
    {
        try 
        {
             Thread.sleep(10000);   //waiting here          
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(String result) 
    {
         button.setBackgroundColor(Color.parseColor("#ffa500"));
    }
 }

use this method whenever you click on button as

ButtonColorChange  btc = new ButtonColorChange();
 btc.execute();
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
  • That is a very bad approach. This way you're creating a new object for the GC to clean, this object will do nothing but run a thread that will sleep. The method `View.postDelayed(Runnable, long)` is a much better approach as the runnable is a much smaller object that can be re-used as many times as necessary and you won't need to have threads sleeping. – Budius Dec 27 '12 at 10:44