File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Android and the fly likes Android edittext text alignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Android edittext text alignment" Watch "Android edittext text alignment" New topic

Android edittext text alignment

Dhaval J. Patel
Ranch Hand

Joined: Mar 10, 2011
Posts: 90

I am making a small app for a tablet, something like a display sign.

I have a few text elements that are center aligned in the display sign. When I tap on it, it gets converted to editText. I want the edit text in such a way that the text in it is center aligned and content wrapped. But when i input something, editText expands both ways, keeping the text aligned to the center. I want to do the same thing with right aligned text elements (only difference is that the edit text would expand only towards the left).

When i am trying to do this right now it expands towards the right.This is my code for laying out the edit Text element

I am setting the width of the layout as Wrap_content in the layout parameters. I have also specified the x and y in my layout parameters for the top left corner of the edit text.

Is there any way to achieve this ?
Przemek Boryka
Ranch Hand

Joined: Dec 06, 2011
Posts: 51

Hi !

This should resolve your problem :

EditText t = (EditText)findViewById(;

it makes the EditText inputField entered text centered horizontally

... in mylayout.xml :

... <EditText

you can change the layout_width from fill_parent to 150 dp or any other size, but not wrap_content. The text will be centered horizontally.

set the t.setGravity(Gravity.RIGHT); or LEFT, that makes entered text left or right alignement.
I agree. Here's the link:
subject: Android edittext text alignment
It's not a secret anymore!