Search code examples
androidtextviewviewflipper

Cannot be able to add TextView in ViewFlipper dynamically


I am making an application to display slideshow of different views like Images(ImageView), Videos(VideoView) and Texts(TextView) using ViewFlipper. My problem is when I add Image or Video to ViewFlipper it is working perfectly.But, when I try to add TextView , ViewFlipper is not displaying anything. Check the following code and let me know how can I solve this issue or what I did wrong in coding?

This is how I added TextView in ViewFlipper :

LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.ems_source, relativeLayout, false );
TextView tvEms = (TextView) view.findViewById(R.id.tvEms);
tvEms.setText(ems.get(0).getMessage());


viewFlipper.addView(tvEms);
viewFlipper.setInAnimation(context, R.anim.slide_from_left);
viewFlipper.setOutAnimation(context, R.anim.slide_out_right);
viewFlipper.showNext();

Here is my ems_source.xml :

<TextView android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/tvEms"
    android:textSize="20sp"
    android:gravity="center"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    xmlns:android="http://schemas.android.com/apk/res/android" />

Here is my DisplayActivity XML :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/relative"
    android:background="#000000"
    tools:context="com.signageocean.signageocean.activities.ActivityDisplay"
    android:gravity="center">

    <ViewFlipper
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewFlipper"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true" >

    </ViewFlipper>

</RelativeLayout>

Solution

  • Your ems_source.xml should looks like below:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:background="#000000"
        android:gravity="center">
    
     <TextView android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/tvEms"
        android:textSize="20sp"
        android:gravity="center"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    
    
    </RelativeLayout>
    

    Reason: You are directly binding your TextView with View.

    Hope this would work for you.