Android Edit text alignment

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 10:46:57
Asad Rao

This should resolve your problem:

EditText t = (EditText) findViewById(R.id.text);
t.setGravity(Gravity.CENTER);

... in XML:

<EditText
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="text" />

It makes the EditText inputField entered text centered horizontally.

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 alignment.

Ajay B

This is a tested simpler solution. Just add android:gravity="center" to the layout XML file to align the text to center of EditText. There is no need to change Java code.

<EditText
    android:id="@+id/LastName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/back"
    android:inputType="numberDecimal"
    android:gravity="center" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!