Tuesday 6 September 2016

Text Switcher




activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
 <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:paddingBottom="@dimen/activity_vertical_margin"     
 android:paddingLeft="@dimen/activity_horizontal_margin" 
 android:paddingRight="@dimen/activity_horizontal_margin" 
 android:paddingTop="@dimen/activity_vertical_margin" 
 android:orientation="vertical" 
 tools:context="showgallery.example.com.textswitcher.MainActivity">

    <TextSwitcher         
android:id="@+id/id_txtdescription" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:background="#dedede"         
android:padding="20dp" 
 android:gravity="center_horizontal"/>
    <Button 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:text="Text1" 
 android:onClick="gotoText1"        />
    <Button         
android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:text="Text2"         
android:onClick="gotoText2"        />
    <Button         
android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:text="Text3"         
android:onClick="gotoText3"        />
</LinearLayout>



MainActivity.java



import android.support.v7.app.AppCompatActivity;
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.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

public class MainActivity extends AppCompatActivity {

    TextSwitcher txtSwitcher;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtSwitcher = (TextSwitcher) findViewById(R.id.id_txtdescription);

        txtSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override            public View makeView() {
        
                TextView t = new TextView(MainActivity.this);
                t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
                return t;
            }
        });

        Animation in = AnimationUtils.loadAnimation(this,
                android.R.anim.slide_in_left);
        Animation out = AnimationUtils.loadAnimation(this,
                android.R.anim.slide_out_right);
        txtSwitcher.setInAnimation(in);
        txtSwitcher.setOutAnimation(out);

        txtSwitcher.setCurrentText("Hello, Everyone");
    }

    public void gotoText1(View view)
    {
        txtSwitcher.setText("Hello");
    } public void gotoText2(View view)
    {
        txtSwitcher.setText("TextSwitcher");
    } public void gotoText3(View view)
    {
        txtSwitcher.setText("This is demo!");
    }
}

No comments:

Post a Comment