Make android app with view background flipping animation dynamically on button click.
In this tutorial we are creating a TextView background slowly changeable colors so it feels like gradient effects swiping view. You can set this type of effect on any widget using our tutorial code. There is a class named as TransitionDrawable is performs most important role. So here is the complete step by step tutorial for Create background color changing animation in android programmatically.
Note: There is a method setBackground( ) will works on minimum api level 16. So please set android:minSdkVersion=”16″ inside your project’s AndroidManifest.xml file.
How to Create background color changing animation in android programmatically.
Code for MainActivity.java file.
package com.backgroundcolorchanginganimation_android_examples.com; import android.annotation.SuppressLint; import android.app.Activity; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.TransitionDrawable; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { TextView AniText; Button AppEffect; ColorDrawable[] BackGroundColor = { new ColorDrawable(Color.parseColor("#fa0707")), new ColorDrawable(Color.parseColor("#3507fa")), new ColorDrawable(Color.parseColor("#07faec")), new ColorDrawable(Color.parseColor("#faec07")) }; TransitionDrawable transitiondrawable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AniText = (TextView)findViewById(R.id.textView1); AppEffect = (Button)findViewById(R.id.button1); transitiondrawable = new TransitionDrawable(BackGroundColor); AppEffect.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub AniText.setBackground(transitiondrawable); transitiondrawable.startTransition(2000); } }); } }
Code for activity_main.xml layout file.
<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="com.backgroundcolorchanginganimation_android_examples.com.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="250dp" android:layout_height="100dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="186dp" android:text="Sample TextView" android:textAppearance="?android:attr/textAppearanceLarge" android:gravity="center"/> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="26dp" android:text="Click Here To Start Animation" /> </RelativeLayout>
Code for AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.backgroundcolorchanginganimation_android_examples.com"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Screenshot :