Topna
Topna

Reputation: 13

Transition Drawable - 3 options

I want to change button's style between 3 drawables : "normal", "green" and "red". I used TransitionDrawable, but it works only with 2 layers.

<transition xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:drawable="@drawable/button" />
     <item android:drawable="@drawable/buttongreen" />
</transition>

How would I implement another "android:drawable" or is there other way to achieve this, else than TransitionDrawable? How to switch between drawables, based on input (0 - stands for red, 1 - for green)?

Upvotes: 0

Views: 661

Answers (2)

Nemanja
Nemanja

Reputation: 218

At this point you can't have more than 2 layers.

So, here is how I did it:

myView.setBackgroundResource(R.drawable.transition_drawable_green_yellow);
TransitionDrawable transition = (TransitionDrawable) myView.getBackground();
transition.startTransition(2000);
    
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {            
      
myView.setBackgroundResource(R.drawable.transition_drawable_yellow_red);
TransitionDrawable transition = (TransitionDrawable) myView.getBackground();
transition.startTransition(2000);
     }
}, 2000);

Upvotes: 0

Rick Sanchez
Rick Sanchez

Reputation: 4756

The constructor for TransitionDrawable takes an array, which may contain more than two drawables, as per the documentation

public TransitionDrawable (Drawable[] layers)

Added in API level 3
Create a new transition drawable with the specified list of layers. At least 2 layers are required for this drawable to work properly.

Though I haven't used this class, so in case it doesn't work, you can set up two TransitionDrawables, one for the transition from color A to B, and another for B to C. Then you'll have to call start on the second transition after the end of the first, and it'll look like it's transitioning from A to B to C.

Upvotes: 0

Related Questions