Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Center text inside EditText programmatically

Is there any way to center the inputted text in an EditText field? More specifically, instead of the cursor starting at the left of the box, it should start in the center and move outward towards the left as it is populated with inputs.

like image 757
capcom Avatar asked Aug 20 '12 19:08

capcom


People also ask

How do you center text in EditText?

Simpler Solution to align text in EditText is to add android:gravity="center" in layout xml. There is no need to add Java code.

How do I center align text in TextView?

android:gravity="center" for text center in TextView. android:gravity="center_horizontal" inner text if you want horizontally centered. android:gravity="center_vertical" inner text if you want vertically centered. android:layout_centerInParent="true" if you want TextView in center position of parent view.

How do you change text Center in HTML?

Just add android:gravity="center" to the layout XML file to align the text to center of EditText.

How do I center text in Kotlin?

To center align text in TextView in Kotlin Android, set android:textAlignment attribute with the value “center” in layout file, or programmatically set the textAlignment property of the TextView object with View. TEXT_ALIGNMENT_CENTER in activity file.


2 Answers

you should use

 textView.setGravity(Gravity.CENTER_HORIZONTAL); 
like image 166
ColdFire Avatar answered Sep 19 '22 15:09

ColdFire


Try android:gravity="center_horizontal" in the widget definition in your layout file. This will not strictly "move outward towards the left" -- it should move outward in both directions evenly. If you truly mean you want the text to only be on the left side of the EditText, I doubt that will be possible.

like image 35
CommonsWare Avatar answered Sep 21 '22 15:09

CommonsWare