user3555472
user3555472

Reputation: 836

Android TextView Right Align in LinearLayout Programmatically

I am working on dynamic UI. I have Parent LinearLayout and I created another LinearLayout in it and add TextView in this LinearLayout. I want to Right Align of Screen that TextView

I tried like below

public MessageView(LinearLayout parent, Message msg) {
    myParent = parent;

    messageDetails = new LinearLayout(parent.getContext());
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);

    layoutParams.gravity = RelativeLayout.ALIGN_PARENT_RIGHT;

    messageDetails.setLayoutParams(layoutParams);

    senderTV = new TextView(parent.getContext());
    senderTV.setTypeface(null, Typeface.ITALIC);
    messageDetails.addView(senderTV);
}

How can I do it?

Upvotes: 3

Views: 2320

Answers (1)

user123456
user123456

Reputation: 265

Try this:

messageDetails = new RelativeLayout(parent.getContext());
    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
messageDetails.addView(senderTV);

Upvotes: 3

Related Questions