Android fade out animation for tablerow

dev_android picture dev_android · Apr 8, 2011 · Viewed 8.7k times · Source

I have a TableRow with a TextView. Here is the xml for it.

<TableRow
    android:layout_height="fill_parent" 
    android:layout_gravity="bottom"
    android:layout_width="fill_parent"
    android:background="#BF000000">

    <TextView
        android:id="@+id/topText"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        android:textSize="19sp"
        android:background="#BF000000"
        android:layout_gravity="center_horizontal"
        android:text="@string/text_searchword"
        android:layout_width="fill_parent">
    </TextView>

</TableRow>

I want to make the table row invisible with fadeout effect on a button touch and vice versa. How can I do it?

Answer

devunwired picture devunwired · Apr 8, 2011

Any View (TableRow included) can have a fade animation attached to it, but you'll need to be able to reference your view in code, so the row will need an id:

<TableRow
  android:id="@+id/my_row"
  android:layout_height="fill_parent" 
  android:layout_gravity="bottom"
  android:layout_width="fill_parent"
  android:background="#BF000000">
  <TextView
    android:id="@+id/topText"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF"
    android:textSize="19sp"
    android:background="#BF000000"
    android:layout_gravity="center_horizontal"
    android:text="@string/text_searchword"
    android:layout_width="fill_parent">
  </TextView>
</TableRow>

Now you can reference the row itself in your Java code somewhere (like onCreate() perhaps) as

View row = findViewById(R.id.my_row);

Notice I don't cast it as a TableRow. You could if you need to do other things with it, but for just setting visibility leaving it as a View is fine. Then just construct a button click method like this:

public void onClick(View v) {
    View row = findViewById(R.id.myrow);
    if(row.getVisibility() == View.VISIBLE) {
        row.startAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
        row.setVisibility(View.INVISIBLE);
    } else {
        row.startAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        row.setVisibility(View.VISIBLE);
    }
}

Fade in and Fade out are standard animations defined in the Android package, you don't need to create them yourself, just load them using AnimationUtils.loadAnimation(). In this example, the clicking the same button just toggles between fade in and fade out depending on whether or not the View is already visible.

Hope that Helps!