I have created an Android app in Android Studio, written in Kotlin. One of the pages shows an overview of bicycles with their name below. Works like a charm in Android Studio, all emulators I tested it in and even in production for most of the users. However I have one comment from someone who only sees images, and not the text below.
The code looks pretty straightforward:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_bikes)
val myDataset = Datasource().loadBikes()
val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
recyclerView.adapter = ItemAdapter(this, myDataset)
recyclerView.setHasFixedSize(true)
}
Then the model creates an empty class:
class Bikes(@StringRes val stringResourceId: Int, @DrawableRes val imageResourceId: Int) { }
The datasource then creates the list of bikes:
class Datasource {
fun loadBikes(): List<Bikes> {
return listOf(
Bikes(R.string.bike11, R.drawable.bike11),
Bikes(R.string.bike1, R.drawable.bike1),
.....
The adapter glues it all together:
class ItemAdapter(private val context: Context, private val dataset: List<Bikes>) : RecyclerView.Adapter<ItemAdapter.ItemViewHolder>() {
class ItemViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
val textView: TextView = view.findViewById(R.id.item_title)
val imageView: ImageView = view.findViewById(R.id.item_image)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
val adapterLayout = LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false)
return ItemViewHolder(adapterLayout)
}
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
val item = dataset[position]
holder.textView.text = context.resources.getString(item.stringResourceId)
holder.imageView.setImageResource(item.imageResourceId)
}
override fun getItemCount() = dataset.size
The activity layout even defines the text color:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/header_text"
android:textColor="@color/black"
android:adjustViewBounds="true"
android:scrollbars="vertical"
app:layoutManager="LinearLayoutManager" />
The list_item has the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">
<ImageView
android:id="@+id/item_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:importantForAccessibility="no"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
app:layout_constrainedHeight="true"
app:layout_constraintHeight_max="0dp"
app:layout_constraintVertical_bias="0" />
<TextView
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:textAppearance="?attr/textAppearanceHeadline6" />
</LinearLayout>
In the app it then shows up nicely for nearly every one:
However for the complaining customer the text is missing:
The only thing that is noticeable different is the text font, but I guess he would run into other issues in different apps as well. He is using a Huawei Android phone.
Any ideas?
There is some things which you need to change as the text view height need to be the wrap_content
instead of the match_parent
and sometimes because of the textSize
and textColor
there might be the possibility of text view cannot be visible or create issue to show in your case in some phone because prefix text size selection from the os and the theme in terms of the color.
Below is the example for your reference.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">
<ImageView
android:id="@+id/item_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:importantForAccessibility="no"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
app:layout_constrainedHeight="true"
app:layout_constraintHeight_max="0dp"
app:layout_constraintVertical_bias="0" />
<TextView
android:id="@+id/item_title"
android:layout_width="match_parent"
android:textSize="32sp"
android:textColor="@color/black"
android:layout_height="wrap_content"
android:padding="10dp"
android:textAppearance="?attr/textAppearanceHeadline6" />
</LinearLayout>