1

I am new to animations. I have a spin Animation on a button. The problem is when that button animation is executed the button intersects other buttons in the layout. So during animation the moving button cannot be seen since it is being covered up by other buttons. Any idea on how to make my animation stay on the top layer of the layout? I would like to figure out a solution that is API 8 compatible.

//create shake animation
    final Animation shakeIt = AnimationUtils.loadAnimation(this, R.anim.shake);
    findViewById(R.id.button_spin).startAnimation(shakeIt);


// use shake animation
    spinnerButton.startAnimation(shakeIt);

EDIT: (here is a lot more info)

So i have played with it over the past few hours and discovered some more things. Lecho intuition is correct, the z order is the driving factor in whether a widget goes "above" or "below" another one. The problem I am facing is could be because of two things:

  1. The button that is at the front of view view (and i want to move it back) has text that is edited near the end of the activity. Does adding text forcing the widget to redraw change the Z order of that view???

  2. The button text is changed in the onResume() and it seems that I'm not able to change the Z-order of the buttons in that method???

answers to these above 2 questions would solve my problem

Edit 2: XML in question

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"
    android:orientation="vertical"
     >

     <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:orientation="vertical"
       android:layout_weight=".7"
       android:layout_marginTop="5dp" >

         <Button
            android:id="@+id/categoryButton"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/drop"
            android:prompt="@string/planet_prompt"
            android:paddingRight="20dp"
            android:textColor="#000000"
            android:textStyle="bold"
            android:textSize="35dp"
            />


     </LinearLayout>


 <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="0dp"
     android:layout_weight="0.1"
     android:orientation="horizontal" >

     </LinearLayout>

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:orientation="horizontal"
       android:layout_weight="1.5" >

               <LinearLayout
               android:layout_width="0dp"
               android:layout_height="match_parent"
               android:orientation="horizontal"
               android:layout_weight="1" >
               </LinearLayout>

                <Button
                android:id="@+id/button_spin"
                android:layout_width="0dp"
                android:layout_weight="2.35"
                android:layout_height="fill_parent"
                android:background="@drawable/letseat"
                android:layout_gravity="center_horizontal"
                android:gravity="center" />

              <LinearLayout
               android:layout_width="0dp"
               android:layout_height="match_parent"
               android:orientation="horizontal"
               android:layout_weight="1" >
               </LinearLayout>

</LinearLayout>



 <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="0dp"
     android:layout_weight="3"
     android:orientation="vertical">

      <Button
        android:id="@+id/button_additems"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:background="@drawable/add_rest"
        android:gravity="center_horizontal" />   

        <Button
        android:id="@+id/button_showrestaurant"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:background="@drawable/edit_rest"
        android:gravity="center_horizontal" />

        <Button
        android:id="@+id/button_nearbyplaces"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:background="@drawable/whats_near"
        android:gravity="center_horizontal" />

  </LinearLayout>
</LinearLayout>
Goku
  • 1,565
  • 1
  • 29
  • 62

1 Answers1

0

Just my idea. In android z-order is determined by order in which views are added to XML. Maybe animated button was added to xml before the other one so it is "deeper". Try to put animated button later in XML or use method bringChildToFront(buttonView) on your layout or add your button to layout from code.

Edit:

Ad 1. Adding text seems to not change the Z order. When I change text and call button.bringToFront() the Z order is changed but not when I call bringToFront() or setText() alone.

Ad 2. In onResume adding text doesn't change Z order but method bringToFront() seems to works as expected.

I am not able to test those methods together with view animation right now.

Test activity:

public class MainActivity extends Activity {
    private int i = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button btn1 = (Button) findViewById(R.id.btn1);
        final Button btn2 = (Button) findViewById(R.id.btn2);
        btn2.bringToFront();
        final Button btn3 = (Button) findViewById(R.id.btn3);
        btn3.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if ((i % 2) == 0) {
                    final Button btn1 = (Button) findViewById(R.id.btn1);
                    btn1.setText("BUTTON_BLACK_NEW");
                    btn1.bringToFront();
                } else {
                    final Button btn2 = (Button) findViewById(R.id.btn2);
                    btn2.setText("BUTTON_GREY_NEW");
                    btn2.bringToFront();
                }
                ++i;

            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        if ((i % 2) == 0) {
            final Button btn1 = (Button) findViewById(R.id.btn1);
            btn1.bringToFront();
        } else {
            final Button btn2 = (Button) findViewById(R.id.btn2);
            btn2.bringToFront();
        }
        ++i;
    }

}

Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" 
android:id="@+id/main_layout">

<Button
    android:id="@+id/btn1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/black"
    android:text="BUTTON_BLACK"
    android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:text="BUTTON_GREY"
        android:textColor="@android:color/black" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CHANGE_Z"
        android:layout_below="@+id/btn2" />

</RelativeLayout>
Leszek
  • 6,568
  • 3
  • 42
  • 53
  • I follow your logic. However, my animated button is the defined after the other widget in XML. also, the bringChildToFront() method doesnt seem to have any effect while the animation is taking place. any other ideas? – Goku Dec 04 '12 at 20:52
  • Can you look at my edit to the original question. This could be an unusual case – Goku Dec 04 '12 at 21:52
  • Im starting to think that my xml linear layout with nested layouts has somthing to do with it and that bringtofront() might only be bringing the widget to the front of that particular nested layout. can you look at my XML? – Goku Dec 05 '12 at 16:37
  • and yes, when i change your xml layout so that btn 2 is within a nested Linear layout, that grey is no longer able to be brought to the front. any ideas on how to force the view to get braught to the front for my XML? i need to keep those nested layouts since its pretty adaptable for all screen sizes. – Goku Dec 05 '12 at 16:46
  • 1
    And it looks like i can bring the nested layout to the front thereby bringing the widget to the front. this is interesting – Goku Dec 05 '12 at 16:51
  • Any luck with bringing layouts to the front? – Leszek Dec 05 '12 at 19:15
  • not yet. When i use bringtofront() im able to get it to the front of the layout. problem is that the widget gets moved to a different spot on the screen – Goku Dec 05 '12 at 20:07