This blog describe about TextSwitcher functionality in android application development. Using TextSwitcher widget can get next and previous data by click on next and prev button.
main.xml
<LinearLayout 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:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text Switcher"
android:layout_gravity="center"
android:textColor="#336633"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginTop="10dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
<Button
android:id="@+id/btn_minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="-" />
</LinearLayout>
<TextSwitcher
android:id="@+id/textSwitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/btn_multiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*" />
<Button
android:id="@+id/btn_divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="/" />
</LinearLayout>
<TextSwitcher
android:id="@+id/textSwitcher1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_gravity="center"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/btn_prev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev" />
<Button
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:text="Next" />
</LinearLayout>
</LinearLayout>
TextSwitcherActivity.java
package com.rakesh.tiwari.google.textswitcher;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
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.ViewFactory;
public class TextSwitcherActivity extends Activity implements ViewFactory,
OnClickListener {
List<String> arrList = new ArrayList<String>();
TextSwitcher textSwitcher;
TextSwitcher textSwitcher1;
Button btnPlus, btnMinus, btnMultiply, btnDivid, btnPrev, btnNext;
int counter = 0;
int position = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
arrList.add("String 1....");
arrList.add("String 2----");
arrList.add("String 3****");
arrList.add("String 4++++");
arrList.add("String 5####");
btnPlus = (Button) findViewById(R.id.btn_plus);
btnMinus = (Button) findViewById(R.id.btn_minus);
btnMultiply = (Button) findViewById(R.id.btn_multiply);
btnDivid = (Button) findViewById(R.id.btn_divide);
btnPrev = (Button) findViewById(R.id.btn_prev);
btnNext = (Button) findViewById(R.id.btn_next);
textSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
textSwitcher.setFactory(this);
textSwitcher1 = (TextSwitcher) findViewById(R.id.textSwitcher1);
textSwitcher1.setFactory(this);
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.fade_out);
Animation in1 = AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left);
Animation out1 = AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right);
textSwitcher.setAnimation(in);
textSwitcher.setAnimation(out);
textSwitcher1.setAnimation(in1);
textSwitcher1.setAnimation(out1);
btnPlus.setOnClickListener(this);
btnMinus.setOnClickListener(this);
btnMultiply.setOnClickListener(this);
btnDivid.setOnClickListener(this);
btnPrev.setOnClickListener(this);
btnNext.setOnClickListener(this);
}
@Override
public View makeView() {
// TODO Auto-generated method stub
TextView tv = new TextView(this);
tv.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
tv.setTextSize(36);
return tv;
}
public View makeView1() {
// TODO Auto-generated method stub
TextView tv1 = new TextView(this);
tv1.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
tv1.setTextSize(36);
return tv1;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == btnPlus) {
counter++;
textSwitcher.setText(String.valueOf(counter));
} else if (v == btnMinus) {
counter--;
textSwitcher.setText(String.valueOf(counter));
} else if (v == btnMultiply) {
counter = counter * 2;
textSwitcher.setText(String.valueOf(counter));
} else if (v == btnDivid) {
counter = counter / 2;
textSwitcher.setText(String.valueOf(counter));
} else if (v == btnPrev) {
setPositionPrev();
textSwitcher1.setText(String.valueOf(arrList.get(position)));
} else if (v == btnNext) {
setPositionNext();
textSwitcher1.setText(String.valueOf(arrList.get(position)));
}
}
public void setPositionPrev() {
position++;
if (position > arrList.size() - 1) {
position = 0;
}
}
public void setPositionNext() {
position--;
if (position < 0) {
position = arrList.size() - 1;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.text_switcher, menu);
return true;
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rakesh.tiwari.google.textswitcher"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.rakesh.tiwari.google.textswitcher.TextSwitcherActivity"
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>
TextSwitcher Functionality |
TextSwitcher Widget |
TextSwitcher App |
TextSwitcher |
TextSwitcher Creation |
No comments:
Post a Comment