Search code examples
androidandroid-linearlayout

TextView Params in LinearLayout not work programmatically


I want to set Params for 2 TextView in my Android Program programmatically but I've a problem. This is the piece of code interessed:

private void setChatItemStyle(Boolean mineMsg, ChatViewHolder holder) {

        if(mineMsg) {
            holder.params.gravity = Gravity.END;
            holder.autore.setTextColor(Color.BLUE);
        }else{
            holder.params.gravity = Gravity.START;
            holder.autore.setTextColor(Color.MAGENTA);
        }

        holder.autore.setLayoutParams(holder.params);
        holder.messaggio.setLayoutParams(holder.params);
    }

Now the problem is; the color of the text is setted but the gravity not. How can I fix it?

The TextView Cioscos with its respective text, should be shown on the right and not under Peter text on the left of the activity.

This is my ChatViewHolder:

public class ChatViewHolder extends RecyclerView.ViewHolder {

        TextView autore, messaggio;
        LinearLayout.LayoutParams params;

        public ChatViewHolder(View itemView) {
            super(itemView);

            autore = itemView.findViewById(R.id.tv_autore);
            messaggio = itemView.findViewById(R.id.tv_messaggio);
            params = (LinearLayout.LayoutParams) autore.getLayoutParams();
        }
    }

Edit in app

Only the message go to the right but still it doesn't go to the right of the screen.

XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:visibility="visible">

    <TextView
        android:id="@+id/tv_autore"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="Autore"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="24sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv_messaggio"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Messaggio" />
</LinearLayout>

Solution

  • Hard to answer when we dont have the layout for TextView which should be in your ChatViewHolder.

    What can I say about gravity is, there are two types of gravity.

    1. Gravity in LayoutParams is equal to layout_gravity in xml (layout for the TextView)
    2. Gravity in View is equal to gravity in xml (layout for children inside this view, in your case: the text inside the TextView)

    In your case:

    1. will work if you have your TextView as WRAP_CONTENT in width
    2. will work if you have your TextView as MATCH_PARENT in width

    One way to do it in your case:

    private void setChatItemStyle(Boolean mineMsg, ChatViewHolder holder) {
    
        int gravity = Gravity.START;
        if (mineMsg) {
            gravity = Gravity.END;
            holder.autore.setTextColor(Color.BLUE);
        } else {
            holder.autore.setTextColor(Color.MAGENTA);
        }
    
        holder.autore.setGravity(gravity);
        holder.messaggio.setGravity(gravity);
    }
    

    Also set TextViews to match_parent in your xml file

    I work with kotlin, so the syntax may be wrong? Sorry if I missed anything, you get the picture...