Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – RecyclerView items misaligned only on add

Posted on November 14, 2022

Solution 1 :

As you did not share your code, i think you have to specify match parent instead of wrap content for RecyclerView .

Problem :

I have a RecyclerView that contains items entered in a dialog, the RV is also part of the dialog and the items appear as a button is pressed. The item is simply a LinearLayout TextView whose width matches the parent RV, and the text of the textview is centered.

The issue is that when I add a new item, they appear to be very far to the left. It is only when I scroll the item out of view and then bring it back to the items appear in the center. Also if I close the dialog and reopen it, the items then appear all in the correct alignment on reopening it. Still, if I add a new item, it will have the wrong alignment.

UPDATE: I changed the background color bc i realized the textview was just too small. I want the width to match the width of the RV, the text is properly centering but the TV is not filling the width of the RV… Here’s a pic:
enter image description here

See code below:

SpecAdapter.kt

    class SpecsAdapter(val context: Context, val specs: List<Specification>) :
    RecyclerView.Adapter<SpecsAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_spec, parent, false))
    }

    override fun getItemCount() = specs.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val spec = specs[position]
        holder.bind(spec)
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind(spec: Specification) {
            itemView.tvSpec.text = "    ${spec.name}    "
            if (!spec.include) {
                itemView.tvSpec.apply {
                    paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
                }
            }
        }
    }
}

Main Activity.kt (the part with the dialog where the RV appears)

fun showSpecificationDialog(specType: String) {
    val searchFormView = LayoutInflater.from(this).inflate(R.layout.specification_add, null)

    val dialog = AlertDialog.Builder(this)
        .setTitle("Include/Exclude")
        .setView(searchFormView)
        .setNeutralButton("Close", null)
        .show()

    dialog.findViewById<EditText>(R.id.etSearch)?.hint = "Enter $specType"

    val rvSpecs = dialog.findViewById<RecyclerView>(R.id.rvSpecs)
    val adapter = if (specType == "Ingredient") SpecsAdapter(this, ingredients) else SpecsAdapter(this, tags)
    rvSpecs?.adapter = adapter
    rvSpecs?.layoutManager = LinearLayoutManager(this)

    //clicking this adds the item to one of two lists, which appears in the recyclerview
    dialog.findViewById<Button>(R.id.btnInclude)?.setOnClickListener {
        val spec = dialog.findViewById<EditText>(R.id.etSearch)?.text.toString()
        if (specType == "Ingredient") {
            ingredients.add(Specification(spec, false, true))
            adapter.notifyItemInserted(ingredients.size - 1)
            Log.i(TAG, "added $spec to ingredients - exclude")
        } else {
            tags.add(Specification(spec, false, true))
            adapter.notifyItemInserted(tags.size - 1)
            Log.i(TAG, "added $spec to tags - exclude")
        }
        dialog.findViewById<EditText>(R.id.etSearch)?.setText("")
    }

    //clicking this adds the item to one of two lists, which appears in the recyclerview
    dialog.findViewById<Button>(R.id.btnExclude)?.setOnClickListener {
        val spec = dialog.findViewById<EditText>(R.id.etSearch)?.text.toString()
        if (specType == "Ingredient") {
            ingredients.add(Specification(spec, false, false))
            adapter.notifyItemInserted(ingredients.size - 1)
            Log.i(TAG, "added $spec to ingredients - include")
        } else {
            tags.add(Specification(spec, false, false))
            adapter.notifyItemInserted(tags.size - 1)
            Log.i(TAG, "added $spec to tags - include")
        }
        dialog.findViewById<EditText>(R.id.etSearch)?.setText("")
    }

    dialog.getButton(DialogInterface.BUTTON_NEUTRAL).setOnClickListener {
        dialog.dismiss()
    }
}

specification_add.xml (this holds the recyclerview and the buttons and whatnot)

<?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="match_parent">
    <EditText
        android_id="@+id/etSearch"
        android_layout_width="0dp"
        android_layout_height="wrap_content"
        android_layout_marginStart="24dp"
        android_layout_marginEnd="24dp"
        android_textAlignment="textStart"
        android_textSize="24sp"
        app_layout_constraintEnd_toEndOf="parent"
        app_layout_constraintStart_toStartOf="parent"
        app_layout_constraintTop_toTopOf="parent"
        tools_text="Ingredient/Tag" />

    <Button
        android_id="@+id/btnExclude"
        android_layout_width="wrap_content"
        android_layout_height="wrap_content"
        android_layout_marginTop="8dp"
        android_text="Exclude"
        android_textSize="16sp"
        app_layout_constraintEnd_toEndOf="@id/etSearch"
        app_layout_constraintTop_toBottomOf="@+id/etSearch" />

    <Button
        android_id="@+id/btnInclude"
        android_layout_width="wrap_content"
        android_layout_height="wrap_content"
        android_layout_marginTop="8dp"
        android_text="Include"
        android_textSize="16sp"
        app_layout_constraintStart_toStartOf="@id/etSearch"
        app_layout_constraintTop_toBottomOf="@+id/etSearch" />

    <androidx.recyclerview.widget.RecyclerView
        android_id="@+id/rvSpecs"
        android_layout_width="0dp"
        android_layout_height="200dp"
        android_scrollbars="none"
        android_textAlignment="textStart"
        app_layout_constraintBottom_toBottomOf="parent"
        app_layout_constraintEnd_toEndOf="parent"
        app_layout_constraintStart_toStartOf="parent"
        app_layout_constraintTop_toBottomOf="@+id/btnInclude"
        app_layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>

item_add.xml (xml file for each individual item in RV)

    <?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"
    xmlns_tools="http://schemas.android.com/tools"
    android_layout_width="match_parent"
    android_layout_height="wrap_content">
    <TextView
        android_id="@+id/tvSpec"
        android_layout_width="match_parent"
        android_layout_height="wrap_content"
        android_padding="8sp"
        android_textAlignment="center"
        android_textColor="#000000"
        android_textSize="24sp"
        tools_text="Ingredient/Tag" />
</LinearLayout>

Comments

Comment posted by beastlyCoder

show an example of what you’re talking about

READ  [ANSWERED] java - onCreateOptionsMenu() does not work in Fragment
Powered by Inline Related Posts

Comment posted by beastlyCoder

so how do you want the items to be formatted

Comment posted by Miles Zoltak

I want it centered in the RV, but for some reason when it try to center it, the text appears to the far left. It is as if they textview was centered on the left edge of the RV, bc the text does a linefeed way before it seems to run out of space

Comment posted by Miles Zoltak

I updated this question with a screenshot of the issue! I still need help.

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress