CristianoWilson
CristianoWilson

Reputation: 143

On button click animation must be triggered

I have this button which is already linked to a few string-arrays. What I need is that when that same button is clicked an animation must play. At the moment I have this animation when i go from my first activity to my second:

overridePendingTransition(R.anim.animation , R.anim.animation2);

I want that same animation each time I click the button, how do I do that?

Here's the rest of my code:

public class MyActivity2 extends Activity {
    private String[] colors;
    private String[] values;
    private TextView tv;
    private RelativeLayout rl;
    int index = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_activity2);
    Button n = (Button) findViewById(R.id.button);
    tv = (TextView) findViewById(R.id.textView);
    rl = (RelativeLayout) findViewById(R.id.layout_view);

    Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
    n.setTypeface(typeface);
    Typeface face = Typeface.createFromAsset(getAssets(),"OSP-DIN.ttf");
    tv.setTypeface(face);

    values = getResources().getStringArray(R.array.things_array);
    colors = getResources().getStringArray(R.array.colorcode_array);

    n.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Random RAND = new Random();
            int position = RAND.nextInt(colors.length);
            int position2 = (index++);
            String nextValue = colors[position];
            String textValue = values[position2];
            overridePendingTransition(R.anim.animation , R.anim.animation2);
            tv.setText(textValue);
            rl.setBackgroundColor(Color.parseColor(nextValue));


            }
        });
    }

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.animation4, R.anim.animation3);
    }
}

and my .xml file for the activity:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity2"
android:background="#ffdb4b5e">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Next"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:textColor="#ffffffff"
    android:textSize="72sp"
    android:background="#00000000"
    />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/thing1"
    android:id="@+id/textView"
    android:textColor="#ffffffff"
    android:textSize="36sp"
    android:gravity="center"
    android:layout_above="@+id/button"
    />


</RelativeLayout>

anim.xml

<?xml version="1.0" encoding="utf-8" ?> 
<translate xmlns:android="http://schemas.android.com/apk/res/android"
 android:fromXDelta="100%p" android:toXDelta="0" android:duration="500" /> 

anim2.xml

<?xml version="1.0" encoding="utf-8" ?> 
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0" android:toXDelta="-50%p" android:duration="500" /> 

Upvotes: 0

Views: 299

Answers (1)

Simon Marquis
Simon Marquis

Reputation: 7526

    n.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Some code
            v.startAnimation(AnimationUtils.loadAnimation(MyActivity2.this, R.anim.animation));
        }
    });

Upvotes: 1

Related Questions