0

New to android animation, learning from here https://developer.android.com/guide/topics/resources/animation-resource.html I did what I was taught but it seems it wasn't adequate. Animation is not working and app is getting crashed.

The code of my files:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.example.android.animationsexperiments.MainActivity">

    <TextView
        android:id="@+id/victim"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

</LinearLayout>

MainActivity.java

package com.example.android.animationsexperiments;

import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the object, you wanna target
        final TextView victim = (TextView) findViewById(R.id.victim);

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(getApplicationContext(),
                        R.animator.animation);
                set.setTarget(victim);
                set.start();
            }
        });
    }
}

animation.xml file in res/animator

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:propertyName="string"
        android:duration="1000"
        android:valueTo="@string/app_name"

        />
</set>

Any answer and comment is appreciated, Thanks in advance.

Arpit Chhabra
  • 193
  • 1
  • 12

0 Answers0