Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make the drawableLeft of a Android Button keep rotating?
    primarykey
    data
    text
    <p>I need to create a button where there is a icon in the left and a text in the right. After pressing the button, I want to see there is an animation of rotating image at the place of the left icon in the button.</p> <h2>I know how to rotate a image with ImageView, but it is not helpful to my current requirement.</h2> <p>I tried to use AnimationDrawable, but it did not work either, there is no animation but only the first png file shown. It is then same whatever I use the background or leftDrawable of the button to run the AnimationDrawable. The code is as below:</p> <pre><code>package com.example.layout; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.widget.Button; public class TestLinearlayoutActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override public void onStart() { super.onStart(); Button locationTitleButton = (Button) findViewById(R.id.LocationTitleButton); //locationTitleButton.setBackgroundResource(R.drawable.loading); locationTitleButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.loading, 0, 0, 0); Drawable[] locationTitleButtonDrawables = locationTitleButton.getCompoundDrawables(); AnimationDrawable animDrawable = (AnimationDrawable) locationTitleButtonDrawables[0]; //AnimationDrawable animDrawable = (AnimationDrawable) locationTitleButton.getBackground(); animDrawable.start(); } } //loading.xml: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"&gt; &lt;item android:drawable="@drawable/loc1" android:duration="200" /&gt; &lt;item android:drawable="@drawable/loc2" android:duration="200" /&gt; &lt;item android:drawable="@drawable/loc3" android:duration="200" /&gt; &lt;item android:drawable="@drawable/loc4" android:duration="200" /&gt; &lt;/animation-list&gt; // layout file, main.xml: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:orientation="horizontal" android:padding="0dp" android:layout_height="wrap_content" android:gravity="fill_horizontal" android:layout_margin="0dp"&gt; &lt;Button android:id="@+id/LocationTitleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="0dip" android:layout_gravity="center_vertical" android:gravity="center_vertical" android:layout_weight="1" android:ellipsize="end" android:scrollHorizontally="true" android:singleLine="true" android:text="Add location" android:textStyle="bold" /&gt; &lt;Button android:textColor="#FF000000" android:layout_weight="0" android:id="@+id/AddLocationButton" android:text="Search" android:gravity="center_vertical" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="-8dp" /&gt; &lt;/LinearLayout&gt; </code></pre>
    singulars
    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.
 

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