Today I will show you how to do a simple and beautiful text animation while switching the text in a textView in android
The advantage is that you don’t need a seperate xml file for animation in this program, all is done inside the java code.
This is the main java file.
Here we use a widget called TextSwitcher in android.
Now the main.xml file.
Strings.xml
AndroidManifest.xml file contents.
The advantage is that you don’t need a seperate xml file for animation in this program, all is done inside the java code.
This is the main java file.
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
48
49
50
51
52
53
54
55
56
57
58
| package pack.coderzheaven;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.Button;import android.widget.TextSwitcher;import android.widget.TextView;import android.widget.ViewSwitcher;public class TextSwitcherDemo extends Activity implements ViewSwitcher.ViewFactory, View.OnClickListener { private TextSwitcher mSwitcher; private int mCounter = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mSwitcher = (TextSwitcher) findViewById(R.id.switcher); mSwitcher.setFactory(this); Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in); Animation out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out); mSwitcher.setInAnimation(in); mSwitcher.setOutAnimation(out); Button nextButton = (Button) findViewById(R.id.next); nextButton.setOnClickListener(this); updateCounter(); } public void onClick(View v) { mCounter++; updateCounter(); } private void updateCounter() { mSwitcher.setText(String.valueOf(mCounter)); } public View makeView() { TextView t = new TextView(this); t.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL); t.setTextSize(70); t.setTextColor(Color.RED); return t; }} |
Now the main.xml file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello" android:textStyle="bold|italic"/> <Button android:id="@+id/next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Switch" android:textStyle="bold|italic" /> <TextSwitcher android:id="@+id/switcher" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout> |
1
2
3
4
5
| <?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">TextSwitcher Demo from CoderzHeaven</string> <string name="app_name">TextSwitcher Demo</string></resources> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| <?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="pack.coderzheaven" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".TextSwitcherDemo" 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> |
No comments:
Post a Comment