Joel8080
Joel8080

Reputation: 1

Changing transition drawable resources in operating time

I am trying to dynamically change the resource of a transition drawable that I assigned to the app background. I can change the first layer easily (by changing getId to 1 or 0)but when I try to change the second layer (by increasing getId to 2) the app crashes. Can anyone help?

    TransitionDrawable td = new TransitionDrawable(new Drawable[]{getDrawable(R.drawable.beach),getDrawable(R.drawable.animal)});


    RelativeLayout rl = (RelativeLayout) findViewById(R.id.background);
    if (mInputText.getText().toString().contains("money")){
        rl.setBackground(td);
        td.setCrossFadeEnabled(true);
        td.setDrawableByLayerId(td.getId(2),ContextCompat.getDrawable(this,R.drawable.money));
        td.startTransition(3000);

    }
}

Here is the logcat output

FATAL EXCEPTION: main
                                                 Process: com.joel808.flashchatnewfirebase, PID: 5534
                                                 java.lang.IndexOutOfBoundsException
                                                     at android.graphics.drawable.LayerDrawable.getId(LayerDrawable.java:548)
                                                     at com.joel808.flashchatnewfirebase.MainChatActivity.newtransition(MainChatActivity.java:193)
                                                     at com.joel808.flashchatnewfirebase.MainChatActivity$1.onEditorAction(MainChatActivity.java:119)
                                                     at android.widget.TextView.onEditorAction(TextView.java:5597)
                                                     at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138)
                                                     at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:364)
                                                     at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:91)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:154)
                                                     at android.app.ActivityThread.main(ActivityThread.java:6688)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)                                  

Upvotes: 0

Views: 173

Answers (0)

Related Questions