Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 650
user3555472 Avatar asked Jan 27 '26 08:01

user3555472


1 Answers

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);
like image 86
user123456 Avatar answered Jan 28 '26 21:01

user123456



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!