Loading a TextView from xml into a TextSwitcher
There are several things wrong with your code:
First, the LayoutInflater.inflate()
method expects the id of a layout file in the form of R.layout.layout_name
and not in the form of R.id.some_id
:
TextView textView = (TextView) inflater.inflate(R.layout.textView, null);
Second, the code you use will throw a ClassCastException
because the root of the inflated layout is a LinearLayout
and not a TextView
as you try to cast it. Your code should be:
LinearLayout ll = (Linearlayout) inflater.inflate(R.layout.textView, null);
But even the line above will not work because, as its name suggest, a TextSwitcher
will take only children of type TextView
, nothing can come between the TextSwitcher
and the two TextView
children. The correct code to use will be in the end:
Layout for textview.xml:
<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
And the viewFactory
field will be:
private ViewFactory viewFactory = new ViewFactory() { public View makeView() { LayoutInflater inflater = LayoutInflater.from(TextSwitcherTest.this); TextView textView = (TextView) inflater.inflate(R.layout.textView, null); return textView; }};
Pretty simple using mostly XML.
In your XML add a TextSwitcher root view and two TextViews within it:
<TextSwitcher android:id="@+id/textswitcher_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:inAnimation="@android:anim/fade_in" android:outAnimation="@android:anim/fade_out"> <TextView android:id="@+id/tv_onboarding_next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Text1" /> <TextView android:id="@+id/tv_onboarding_letsgo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Text2" /></TextSwitcher>
Then in code, wherever you want the animation action to occur:
Using Kotlin:
textswitcher_id.setText(getString("Text2"))// and somewhere else, switch it back:textswitcher_id.setText(getString("Text1"))
Or in Java:
findViewById(R.id.textswitcher_id).setText(getString("Text2"));// and somewhere else, switch it back:findViewById(R.id.textswitcher_id).setText(getString("Text1"));
Note:
Within the XML you can also use two includes rather TextView twice, see: https://stackoverflow.com/a/42724239/1852191
The reason we have the default text in the XML even though we set it programmatically later is because on the first view it will be blank, until it's set
Actually, you don't need to set factory if you put two TextViews
inside your TextSwitcher
in layout XML. This worked for me:
<TextSwitcher android:id="@+id/id1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inAnimation="@android:anim/fade_in" android:outAnimation="@android:anim/fade_out"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:lines="1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:lines="1"/></TextSwitcher>
(no additional configuration done in code, just inflate
and findViewById
)
However, you have to duplicate the attributes across the TextViews
. This might be avoided by moving the TextView
to the separate file and then including it twice:
<TextSwitcher android:id="@+id/id1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inAnimation="@android:anim/fade_in" android:outAnimation="@android:anim/fade_out"> <include layout="@layout/titlebar" /> <include layout="@layout/titlebar" /></TextSwitcher>
Other way to avoid duplication would be creating a custom style and applying it to both TextViews
.