textswitcher android example using android studio
Hello Guys. Welcome to our new tutorial of textswitcher android example using android studio. this post you can learn how to use textswitcher and how it works.
please follow the code given below to display textswitcher android example using android studio
- First Create a new project in Android Studio
- File ⇒ New Android ⇒ Application Project
- Then Open src -> package -> MainActivity.java and then add following code :
JAVA (MainActivity.java)
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 59 60 61 62 63 64 |
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.graphics.Color; import android.view.Gravity; import android.view.View; import android.widget.Button; import android.widget.TextSwitcher; import android.widget.TextView; import android.widget.ViewSwitcher; public class AndroidTextSwitcher extends AppCompatActivity { TextSwitcher textSwitcher ; Button Next ; int StringIndex = 0 ; String[] Row = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; TextView textView ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_android_text_switcher); textSwitcher = (TextSwitcher)findViewById(R.id.textswitcher1); Next = (Button)findViewById(R.id.button); Next.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { if(StringIndex == Row.length-1){ StringIndex = 0; textSwitcher.setText(Row[StringIndex]); } else{ textSwitcher.setText(Row[++StringIndex]); } } }); textSwitcher.setFactory(new ViewSwitcher.ViewFactory(){ @Override public View makeView() { textView = new TextView(AndroidTextSwitcher.this); textView.setTextColor(Color.BLACK); textView.setTextSize(40); textView.setGravity(Gravity.CENTER_HORIZONTAL); return textView; }}); textSwitcher.setText(Row[StringIndex]); } } |
- Now Open res -> layout -> activity_main.xml and then add following code :
XML (activity_main.xml)
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 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFF8E1"> <TextSwitcher android:id="@+id/textswitcher1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:inAnimation="@android:anim/slide_in_left" android:outAnimation="@android:anim/slide_out_right" android:layout_marginTop="65dp" /> <Button android:text="Next Text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:id="@+id/button" /> </RelativeLayout> |
Output:
Finally, run this project.
This is how textswitcher android example looks like.