Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically set text alignment in a TextView

In my app, I have a button that creates a TextView with a certain message. How can I set the text alignment of the text to the right of the TextView? I see the attribute in XML, but I don't know how to do it in java.

like image 996
Tchibo Avatar asked Mar 04 '18 11:03

Tchibo


3 Answers

In Kotlin you can use something like the below,

 val textView = TextView(context)
 textView.apply {
     layoutParams = TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
                                    TableRow.LayoutParams.WRAP_CONTENT)
     textAlignment = View.TEXT_ALIGNMENT_VIEW_START
 }

like image 126
Azhar Avatar answered Sep 22 '22 01:09

Azhar


Try using

textView.setGravity(Gravity.RIGHT)
like image 36
Joshua Avatar answered Sep 20 '22 01:09

Joshua


You can set textview's gravity using

textLabel.setGravity(Gravity.CENTER | Gravity.BOTTOM);
like image 27
fi - Anand Dubey Avatar answered Sep 22 '22 01:09

fi - Anand Dubey