Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplay animatable instead of text on a button when clicked
    primarykey
    data
    text
    <p>I am trying to build a button with a progress bar which is displayed in the center of the button and instead of text, as soon as the button is tapped, and while some background processing takes place. What I am doing is to have a custom button and set the animatable as a drawable left to the button, and in onDraw to move the animatable in the center of the button. However, for a short period of time after the button is tapped, I do not know why there are 2 progress bars on the button.</p> <pre><code>public class ButtonWithProgressBar extends Button { protected int progressId = R.drawable.progress_bar; protected Drawable progress; protected CharSequence buttonText; @Override public void setText(CharSequence text, BufferType type) { super.setText(text, type); if (Strings.notEmpty(text)) { buttonText = text; } } public void startSpinning() { if(isSpinning()){ return; } progress = getContext().getResources().getDrawable(progressId); final Drawable[] old = getCompoundDrawables(); setCompoundDrawablesWithIntrinsicBounds(old[0], old[1], progress, old[3]); setEnabled(false); if(progress instanceof Animatable){ ((Animatable)progress).start(); } setText(""); } public void stopSpinning() { if(!isSpinning()){ return; } if(progress instanceof Animatable){ ((Animatable)progress).stop(); } setEnabled(true); setText(buttonText); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (progress != null) { final int drawableHeight = progress.getIntrinsicHeight(); final int drawableWidth = progress.getIntrinsicWidth(); final int drawableTop = (getHeight() - drawableHeight) / 2; final int drawableLeft = (getWidth() - drawableWidth) / 2; progress.setBounds(drawableLeft, drawableTop, drawableLeft + drawableWidth, drawableTop + drawableHeight); progress.draw(canvas); } } </code></pre> <p>}</p> <p>And this is displayed for a short period after tap, before everything looking as I want: <img src="https://i.stack.imgur.com/KDTvp.png" alt="enter image description here"></p> <p>Any ideas why this is happening?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload