liveAnyway
liveAnyway

Reputation: 649

Gravity:End is not taking effect Programmatically

I am giving gravity:end in xml and it shows at the right side in Android Studio. (go end)
but programmatically doesn't work.

  1. layout_gravity:end in chat_image worked just in Android Studio preview but it's not working programmatically.
  2. changed item_parent to RelativeLayout but not work and same with LinearLayout
  3. All possible cases I did when I saw right preview in xml with attributes gravity or layout_gravity if possible.

    I think the problem related to Glide


-My code-
msg_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <!--this item-->
    <ImageView
        android:id="@+id/chat_image"
        android:layout_width="200dp"
        android:layout_height="wrap_content"

        />
    <LinearLayout
        android:id="@+id/msg_linear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/user_id"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textAppearance="@android:style/TextAppearance.Material.Large" />

        <TextView
            android:id="@+id/msg_area"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textAppearance="@android:style/TextAppearance.Material.Large" />
    </LinearLayout>

</LinearLayout>

TalkAdapter.kt

class TalkAdapter(val list: List<ChatDto>, val glide: RequestManager) :
    RecyclerView.Adapter<TalkAdapter.ViewHolder>() {

    private var mList = list as ArrayList<ChatDto>

    private val myId = list[0].id

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TalkAdapter.ViewHolder =
        ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.msg_item, parent, false))

    override fun getItemCount(): Int = mList.size

    override fun onBindViewHolder(holder: TalkAdapter.ViewHolder, position: Int) {

        holder.itemView.apply {
            if (mList[position].id == myId) {
                msg_area.apply {
                    if (position != 0)
                        text = mList[position].msg
                }
                user_id.text = ""
                //my intend
                item_parent.gravity = Gravity.END
            } else {
                msg_area.apply {
                    text = mList[position].msg
                    //and this
                    gravity = Gravity.START
                }
                user_id.text = mList[position].id
                item_parent.gravity = Gravity.START
            }

            chat_image.apply {
                if (mList[position].uri != null) {
                    visibility = View.VISIBLE
                    glide.load(mList[position].uri)
                        .into(this)
                } else {
                    visibility = View.GONE
                }
            }
        }
    }

    fun addItem(item: ChatDto) {
        mList.add(item)
        Log.e("mList-size", mList.size.toString())
        notifyItemInserted(mList.size - 1)
    }
}

Problem:

Problem

Android Studio preview:

Android Studio preview

Upvotes: 0

Views: 160

Answers (0)

Related Questions