tbp
tbp

Reputation: 131

Animating button with object animator

I just want to animate my button.

I am getting:

W/PropertyValuesHolder: Method setAlpha() with type int not found on target class class android.support.v7.widget.AppCompatButton

Mainactivity.java:

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ObjectAnimator objectAnimator;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView mListView= (ListView) findViewById(R.id.listView);
        Button button= (Button) findViewById(R.id.button);
        objectAnimator=ObjectAnimator.ofInt(button,"alpha",0,1).setDuration(1000);
        objectAnimator.setTarget(button);
        objectAnimator.start();


        adapter myAdapter = new adapter(this);
        AlphaInAnimationAdapter animationAdapter = new AlphaInAnimationAdapter(myAdapter);
        animationAdapter.setAbsListView(mListView);
        mListView.setAdapter(animationAdapter);
    }

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="autogenie.mp.MainActivity">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:id="@+id/listView"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp" />
</RelativeLayout>

Why is it not animating?

What comes under property name parameter of method ofint() of objectanimator class?

Upvotes: 0

Views: 1285

Answers (1)

Danail Alexiev
Danail Alexiev

Reputation: 7772

The alpha property is of type float. You are trying to animate it with int values. The object animator tries to find a setAlpha(int) method in the View class and can't do it - hence the exception.

You either need to use ObjectAnimator.ofFloat() or the simpler button.animate().alpha() variant. I would personally recommend the second option - it's much cleaner and removed some boilerplate code.

Upvotes: 1

Related Questions