
First you can create one anim folder in project resources and create one Drawable resource file (xml)
then add below code into project
fadein.xml
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" > <alpha android:duration="1000" android:fromAlpha="0.0" android:interpolator="@android:anim/accelerate_interpolator" android:toAlpha="1.0" /> </set> |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<LinaerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/btnFadeIn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_weight="1" android:text="Fade In" /> </LineraLayout> |
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package com.androindian.raj.newanim import android.annotation.TargetApi import android.content.Intent import android.os.Build import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.Button import butterknife.BindView import butterknife.ButterKnife class MainActivity : AppCompatActivity(), View.OnClickListener { internal lateinit var btnFadeOut: Button internal var btnFadeIn: Button? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) btnFadeIn = findViewById(R.id.btnFadeIn); btnFadeIn?.setOnClickListener(this) } @TargetApi(Build.VERSION_CODES.JELLY_BEAN) override fun onClick(v: View) { if (v === btnFadeIn) { val intent = Intent(this, FadeIn::class.java) startActivity(intent) //startActivity(Intent(this@MainActivity, FadeIn::class)) } } } |
FadeIn.Kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.androindian.raj.newanim import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.view.View import android.view.animation.Animation import android.view.animation.AnimationUtils import android.widget.Button import android.widget.TextView class FadeIn : AppCompatActivity(), Animation.AnimationListener { internal lateinit var txtMessage: TextView internal lateinit var btnStart: Button // Animation internal lateinit var animFadein: Animation override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_fade_in) txtMessage = findViewById<View>(R.id.txtMessage) as TextView btnStart = findViewById<View>(R.id.btnStart) as Button animFadein = AnimationUtils.loadAnimation(applicationContext, R.anim.fade_in) animFadein.setAnimationListener(this) btnStart.setOnClickListener { txtMessage.visibility = View.VISIBLE // start the animation txtMessage.startAnimation(animFadein) } } override fun onAnimationStart(animation: Animation) { } override fun onAnimationEnd(animation: Animation) { } override fun onAnimationRepeat(animation: Animation) { } } |
activity_fade_in.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/txtMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is fadein animation" android:layout_centerInParent="true" android:textSize="25dp" android:visibility="gone"/> <Button android:id="@+id/btnStart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start Animation" android:layout_marginTop="30dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="20dp"/> </RelativeLayout> |