Android TextSwitcher
Android TextSwitcher
- Create TextSwitcher with Manual Controls and Conditions
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project
Click Here.
Step 2 : Open res -> layout ->
activity_main.xml (or) main.xml and add following code :
<?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:background="#1E90FF" >
<Button android:id="@+id/Add"
android:textColor="#FFFFFF"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_marginTop="60dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Addition" />
<Button android:id="@+id/Subtract"
android:textColor="#FFFFFF"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="60dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subtraction" />
<Button android:id="@+id/Multiple"
android:textColor="#FFFFFF"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:rotation="270"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Multiplication" />
<Button android:id="@+id/Divide"
android:textColor="#FFFFFF"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:rotation="90"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Division" />
<TextSwitcher android:id="@+id/switcher"
android:textColor="#FFFFFF"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package com.gudivada.hemanthsomaraju;
import android.app.Activity;
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 MainActivity extends Activity implements ViewSwitcher.ViewFactory,
View.OnClickListener {
TextSwitcher switcher;
float counter = 0;
Button Add, Subtract, Multiple, Divide;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switcher = (TextSwitcher) findViewById(R.id.switcher);
switcher.setFactory(MainActivity.this);
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
switcher.setInAnimation(in);
switcher.setOutAnimation(out);
Add = (Button) findViewById(R.id.Add);
Subtract = (Button) findViewById(R.id.Subtract);
Multiple = (Button) findViewById(R.id.Multiple);
Divide = (Button) findViewById(R.id.Divide);
Add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
switcher.setText(String.valueOf(counter));
}
});
Subtract.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
switcher.setText(String.valueOf(counter));
}
});
Multiple.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
counter = counter*2;
switcher.setText(String.valueOf(counter));
}
});
Divide.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
counter = counter/2;
switcher.setText(String.valueOf(counter));
}
});
}
public View makeView() {
TextView t = new TextView(this);
t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
t.setTextSize(36);
return t;
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Step 4 : Open AndroidManifest.xml and add following code :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gudivada.hemanthsomaraju"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.gudivada.hemanthsomaraju.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>
Step 5 : Our output will be like this :