1

I am making a RecyclerView with Kotlin that will display an image and text. So far the text should be just "Textview" and the image should be one of the images that come with android. The text displays "Textview" the specified number of times but doesn't show the image at all.

I am actually following [this video](https://www.youtube.com/watch?v=jS0buQyfJfs&list=PL0dzCUj1L5JGfHj1lwxOq67zAJV3e1S9S&index=2&t=409s! exactly and run into the problem.

I have changed the cell layout of the image to wrap content and it did not help.

Adapter

class MainAdapter : RecyclerView.Adapter<CustomViewHolder>() {
    val vidTitles = listOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")

    override fun getItemCount(): Int {
        return vidTitles.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
        //create the view for the rows
        val layoutInflater = LayoutInflater.from(parent.context)
        val cellForRow = layoutInflater.inflate(R.layout.video_row, parent, false)
        return CustomViewHolder(cellForRow)
    }

    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
        holder?.view?.videoTitleTextView?.text= vidTitles[position]
    }
}

class CustomViewHolder(val view: View): RecyclerView.ViewHolder(view) {

}

RecyclerView's Cell layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:contentDescription="avatar image"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/videoTitleTextView"
        android:layout_width="0dp"
        android:layout_height="31dp"
        android:text="animals"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>

Main Activity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //recyclerView_main.setBackgroundColor(Color.CYAN)
        recyclerView_main.layoutManager = LinearLayoutManager(this)
        recyclerView_main.adapter = MainAdapter()
    }
}

I expect it to show both the image and the text; It shows the text but not any image and it doesn't even leave any space for the image.

Community
  • 1
  • 1
pokumars
  • 161
  • 11

3 Answers3

2

Change Your Image View as below.

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/colorAccent"
    android:contentDescription="avatar image"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:src="@mipmap/ic_launcher" />

You are using tools:srcCompat which is wrong.

Also Change height of Constraint Layout to wrap_content. Otherwise each item in a Recyclerview is going to take full height of screen.

Tools namespace is used for Compile time behaviour. When you build the app this tools features is removed. Its helps you to preview your view at compile time.

Rajnish suryavanshi
  • 3,168
  • 2
  • 17
  • 23
  • Did as instructed works perfectly thanks. Question is why did **tools:srcCompat** not work/ is wrong? – pokumars Aug 30 '19 at 11:14
2

Replace this

tools:srcCompat="@mipmap/ic_launcher"

with this:

android:src="@mipmap/ic_launcher"

Note: Here tools:srcCompat will show only in the preview of Android studio but not in the real device. For more info check here

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
0

When I read your code right, you should see "animals" in your TextViews only :) This is because in your cell's layout file you set the android:text and not the tools:text attribute.

Just implement your ViewHolder and bind the Views to your model.

If you want only to verify your Views are loaded, you could replace the

tools:srcCompat="@mipmap/ic_launcher"

to

app:srcCompat="@mipmap/ic_launcher"
Zholoth
  • 86
  • 3