Android TextView fade in animation not working - android

Here is what in the animation xml :
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="4500"
and here where I call it:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
setContentView(R.layout.prayerlayout);
txt = (TextView) findViewById(R.id.textView2);
txt.setText(getString(R.string.eighth));
//apply animation
fade1 = AnimationUtils.loadAnimation(this, R.anim.fade10);
txt.startAnimation(fade1);
}
The issue is that the text doesn't show up for the duration of the animation, then it appears suddenly. I didn't notice this problem in older versions of android. It was working fine, but with Lollipop or Jellybeans it doesn't work. Thanks for any help.

It turned out that my text was too long causing the fade-in animation not to work. I applied the animation on the scrollView instead, and it did the effect I wanted.

I have this in the fadein XML.
<?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>
And the code
public class MainActivity extends Activity {
Animation animFadein,animFadeout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
animFadein = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in);
animFadeout = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_out);
Update();
}
public void Update(){
TextView lblEstadoPuerta = (TextView) findViewById(R.id.lblEstadoPuerta);
ImageButton btnabrirpuerta = (ImageButton) findViewById(R.id.btndoorstate);
btnabrirpuerta.startAnimation(animFadeout);
btnabrirpuerta.setVisibility(View.GONE);
btnabrirpuerta.setImageResource(R.drawable.go_down);
btnabrirpuerta.setVisibility(View.VISIBLE);
btnabrirpuerta.startAnimation(animFadein);
}
}
Need to import this..
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

Did you add some Interpolator?
You can read this:
http://developer.android.com/intl/es/guide/topics/resources/animation-resource.html
Your fade10.xml animation file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="4500"
android:fromAlpha="0.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:toAlpha="1.0" />
</set>
Your Activity file
public class FadeActivity extends Activity {
private TextView txt;
private Animation fade1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.prayerlayout);
txt = (TextView) findViewById(R.id.textView2);
txt.setText(getString(R.string.eighth));
//apply animation
fade1 = AnimationUtils.loadAnimation(this, R.anim.fade10);
txt.startAnimation(fade1);
}
}

Related

Infinite loop of sequential animation of ImageButton

I'm new to android studio and I want to animate an imageButton with a sequential animation set. The animation set (animation_boutons.xml)is in res/anim.
I've tried with animationSet in java but the app crashed every time I launched the emulator.
I've spent a long time looking for a solution. I hope someone can help me !
I apologize if it's something obvious.
java code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
configureCodeurBouton();
}
private void configureCodeurBouton() {
ImageButton boutonCodeur = findViewById(R.id.boutoncodeur);
Animation animBoutons = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_boutons);
animBoutons.setRepeatCount(Animation.INFINITE);
boutonCodeur.setAnimation(animBoutons);
boutonCodeur.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, codeur.class));
}
});
}
}
xml code:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true"
android:fillAfter="true">
<rotate
android:fromDegrees="0"
android:toDegrees="20"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="0"
android:duration="1000"
/>
<rotate
android:startOffset="1000"
android:fromDegrees="20"
android:toDegrees="-20"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"
/>
<rotate
android:fromDegrees="-20"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="3000"
android:duration="1000"
/>
</set>
Also, Vedprakash Wagh give me the advice to try animBoutons.setRepeatCount(Animation.INFINITE) but it has no effect).
Your app is crashing every time because you're trying to find your ImageButton when the class is created first, and not after the layout is set.
You're getting NullPointerException, as there is no ImageButton with id R.id.boutoncodeur in your View hierarchy when you're trying to find it.
You need to find your ImageView AFTER it is available in your View hierarchy i.e. after your setContentView();
You can either do this:
Remove your second line
ImageButton boutonCodeur = findViewById(R.id.boutoncodeur);
as you've already found your ImageView in your configureCodeurButton() function.
Or, you can keep one class variable of ImageView, and make findViewById call after setContentView like below.
public class MainActivity extends AppCompatActivity {
ImageButton boutonCodeur;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boutonCodeur = findViewById(R.id.boutoncodeur);
configureCodeurBouton();
}
private void configureCodeurBouton() {
Animation animBoutons = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_boutons);
boutonCodeur.setAnimation(animBoutons);
boutonCodeur.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, codeur.class));
}
});
}
}
You can learn more about NullPointerException here. Also, learn how to read the errors from tutorials that are available. Or, simply open the logcat tab in Android Studio when the error occurs to know what Error you're getting.
To make your animation run infinitely, you can add this in your code.
animation.setRepeatCount(Animation.INFINITE)
I just had to change the whole xml anim_boutons file so I have only one animation and not three rotate animations. the repeatMode line says to repeat the animation backwards at each repeat. This gives the expected effect.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true"
android:fillAfter="true">
<rotate
android:fromDegrees="-20"
android:toDegrees="20"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="0"
android:duration="1000"
android:repeatCount="infinite"
android:repeatMode="reverse"
/>
</set>

Android - How to make Button text flash/blink?

I have a question similar to this but I am wanting to make only the text on the button flash. I don't want the button background to also flash.
This is my R.anim.blink.xml file:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="500"
android:startOffset="20"
android:repeatMode="reverse"
android:repeatCount="infinite"/>
</set>
But this code...
Animation blinkingAnimation = AnimationUtils.loadAnimation(this, R.anim.blink);
myButton.setAnimation(blinkingAnimation);
...makes the whole button blink. So how to make just the text blink (so the button background is shown all the time)?
Simple way:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn = (Button) findViewById(R.id.btn);
final ObjectAnimator colorAnim = ObjectAnimator.ofInt(btn, "textColor", Color.BLACK, Color.TRANSPARENT); //you can change colors
colorAnim.setDuration(500); //duration of flash
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.start();
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
colorAnim.end();
colorAnim.cancel();
}
});
}
It will finish flashing after pressing.
EDIT:
You can define your animation in xml (use objectAnimator):
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="textColor"
android:duration="500"
android:valueFrom="#000000"
android:valueTo="#android:color/transparent"
android:repeatMode="reverse"
android:repeatCount="infinite"
android:interpolator="#android:anim/accelerate_interpolator" />
and use it in your code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.blink);
final Button btn = (Button) findViewById(R.id.btn);
animator.setTarget(btn);
animator.start();
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
animator.end();
animator.cancel();
}
});
}
XML must be in the 'animator' folder.
Try this code in oncreate method of Activity
final Animation animation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
animation.setDuration(500); // duration - half a second
animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
animation.setRepeatCount(Animation.INFINITE); // Repeat animation infinitely
animation.setRepeatMode(Animation.REVERSE); // Reverse animation at the end so the button will fade back in
final Button btn = (Button) findViewById(R.id.your_btn);
btn.startAnimation(animation);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View view) {
view.clearAnimation();
//also your extra work here
}
});
I suggest you use FrameLayout instead.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="any color" >
<TextView
android:id="#+id/bn1"
android:layout_width="wrap_content"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_height="wrap_content"
android:text="some_txt"/>
</FrameLayout>
Now Apply Blink Animation that TextView
in kotlin we can do that.
blink the element
res\animator\blink.xml
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="alpha"
android:duration="1000"
android:valueFrom="1.0"
android:valueTo="0.1"
android:repeatMode="reverse"
android:repeatCount="infinite"
android:interpolator="#android:anim/accelerate_interpolator" />
in the activity.kt
lateinit var animator : ObjectAnimator
onCreate
animator = AnimatorInflater.loadAnimator(this, R.animator.blink) as ObjectAnimator
animator.target = targetElement
animator.start()
to stop and cancel
animator.end()
animator.cancel()
targetElement?.alpha = 1.0f
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
blink only text
val valueAnimator = ValueAnimator.ofFloat(0.0f, 1.0f)
valueAnimator.duration = 1000
valueAnimator.repeatCount = ValueAnimator.INFINITE
valueAnimator.repeatMode = ValueAnimator.REVERSE
valueAnimator.addUpdateListener { it ->
val fractionAnim = it.animatedValue as Float
targetElement?.setTextColor(
ColorUtils.blendARGB(Color.parseColor("#00cc00"),
resources.getColor(R.color.transparent), fractionAnim))
}
valueAnimator.start()
targetElement?.setOnClickListener{
valueAnimator.cancel()
targetElement?.setTextColor(Color.parseColor("#00cc00"))
}
blink_effect.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="600"
android:repeatMode="reverse"
android:repeatCount="infinite"/>
</set>
MainActivity.java
Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.blink_effect);
yourWidget.startAnimation(animation1);

Animation text not showing properly in android

I am trying to add animated text(something like"Developed by .....") to Home page of my App.
But i couldn't show it completely.every time its length equals to shows only the width of the device. How can I solve this issue.My coding are as follows.
In my Home Activity
public class HomeActivity extends AppCompatActivity implements Animation.AnimationListener {
Animation animation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView txtAnimation=(TextView)findViewById(R.id.txtAnimation);
animation= AnimationUtils.loadAnimation(HomeActivity.this,R.anim.move);
String string="Developed by ................";
// set animation listener
animation.setAnimationListener(this);
txtAnimation.startAnimation(animation);
}
My move.XML
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="100%"
android:toXDelta="-100%"
android:repeatMode="restart"
android:duration="8000"
android:repeatCount="infinite"/>
<!--android:toXDelta="0%p"-->
</set>
I tried by using setMaxWidth,setEmsbut not worked.

How to add animation to a text view in android

I have a TextView and I'm trying to add a fade in animation to it. My code is returning null and I don't understand why.
Here is my implementation
This is the fade_in.xml
<alpha
xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true"
android:duration="1000"
android:fromAlpha="0.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:toAlpha="1.0"/>
and here is how im using it in the corresponding activity
tv= (TextView)findViewById(R.id.textView);
//-- the below line is returning null
animation = AnimationUtils.loadAnimation(this,R.anim.fade_in);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
tv.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
Intent it = new Intent(SplashActivity.this, MainActivity.class);
startActivity(it);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
tv.startAnimation(animation);
Android TextView Annimation example
XML
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="2.0"
android:toYScale="2.0"
android:duration="3000" />
</set>
Code
private void RunAnimation()
{
Animation a = AnimationUtils.loadAnimation(this, R.anim.scale);
a.reset();
TextView tv = (TextView) findViewById(R.id.firstTextView);
tv.clearAnimation();
tv.startAnimation(a);
}
For More :
http://chiuki.github.io/advanced-android-textview/#/5
http://www.hascode.com/2010/09/playing-around-with-the-android-animation-framework/
You can load animations from AnimationUtils class in Android and set it to a textview in android.
textview.startAnimation(AnimationUtils.loadAnimation(context, android.R.anim.fade_in));
and you can stop animation using,
textview.clearAnimation();
Is your textview id correct?? First check if you are getting your textview id correctly in your app
You need setAnimation in your TextView
Example:
tv.setAnimation( animation );
Use Animator/AnimatorSet Animation is legacy code

Android Animate an Image View vertically

Hi i have an image view position and the bottom of the page i'm wanting to animate so it moves down does anyone know how to do this? currently when i run it nothing happens
here is what i have tried so far
heres my animation
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator">
<translate android:fromYDelta="0" android:toXDelta="30" android:duration="1000"
android:fillAfter="true"/>
</set>
heres my java
public class IntialSetup extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_initialsetup);
animations();
}
public void animations(){
final ImageView image = (ImageView)findViewById(R.id.su_shirts);
Animation AnimationMovepos = AnimationUtils.loadAnimation(this, R.anim.shirt_anim);
image.startAnimation(AnimationMovepos);
}
}
Look at your animation:
<translate android:fromYDelta="0" android:toXDelta="30" .../>
fromYDelta ... toYDelta and not toXDelta.
Hope this was the problem.

Categories

Resources