Search code examples
androidkotlinandroid-viewpagerandroid-imageviewtextview

Unable to view text in the viewpager kotlin


I am suing viewpager for sliding images along with their text in android using kotlin. Despite having text in the layout and also adding the text to the view I am unable to view the text.

The layout code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp"
android:fitsSystemWindows="true"
android:orientation="vertical">

<ImageView
    android:id="@+id/sliding_image"
    android:layout_width="match_parent"
    android:layout_height="270dp"
    />
<TextView
    android:id="@+id/sliding_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:textStyle="bold"
    android:textColor="#200"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/sliding_image"
    android:gravity="center"
    />
    </RelativeLayout>

The code snippet of the Adapter:

class SliderAdapter:PagerAdapter{

var context:Context
private var images:Array<Int>
private var texts:Array<Int>
lateinit var inflater: LayoutInflater

constructor(context: Context,images:Array<Int>,texts:Array<Int>):super(){
    this.context=context
    this.images=images
    this.texts=texts
}

override fun isViewFromObject(view: View, `object`: Any): Boolean = view == `object` as RelativeLayout

override fun getCount(): Int = images.size

override fun instantiateItem(collection: ViewGroup, position: Int): Any {
    var image:ImageView
    var text:TextView
    inflater=context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    var view = inflater.inflate(R.layout.splashscreenslider, collection,false)
    image = view.findViewById(R.id.sliding_image)
    image.setImageResource(images[position])
    text = view.findViewById(R.id.sliding_text)
    text?.text= texts[position].toString()
    println("${text?.text}")
    collection.addView(view)
    return view
}

override fun destroyItem(collection: ViewGroup, position: Int, `object`: Any) {
    collection.removeView(`object` as RelativeLayout)
}

}

By the way I'm not using fragments. I'm using only the views.

Any help is appreciated.


Solution

  • The answer to this question is modifying the layout file. This worked for me.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:fitsSystemWindows="true"
    android:orientation="vertical">
    
    <ImageView
        android:id="@+id/sliding_image"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        />
    <TextView
        android:id="@+id/sliding_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:layout_marginTop="4dp"
        android:textColor="#200"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/sliding_image"
        android:gravity="center"
        />
      </RelativeLayout>
    

    The code snippet in the Adapter is given below.

    class SliderAdapter:PagerAdapter{
    
    var context:Context
    private var images:Array<Int>
    private var texts:Array<Int>
    private lateinit var inflater: LayoutInflater
    
    constructor(context: Context,images:Array<Int>,texts:Array<Int>):super(){
        this.context=context
        this.images=images
        this.texts=texts
    }
    
    override fun isViewFromObject(view: View, `object`: Any): Boolean = view == `object` as RelativeLayout
    
    override fun getCount(): Int = images.size
    
    override fun instantiateItem(collection: ViewGroup, position: Int): Any {
        var image:ImageView
        var textview:TextView
        inflater=context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        var view = inflater.inflate(R.layout.splashscreenslider, collection,false)
        image = view.findViewById(R.id.sliding_image)
        image.setImageResource(images[position])
        textview = view.findViewById(R.id.sliding_text)
        textview?.setText(texts[position])
        println("${textview?.text}")
        collection.addView(view)
        return view
    }
    
    override fun destroyItem(collection: ViewGroup, position: Int, `object`: Any) {
        collection.removeView(`object` as RelativeLayout)
    }
    
    }
    

    Thank you Mike for responding.