How to make cursor stay in the center of edittext - android

I have a normal edittext like this :
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/et_word"
android:ems="12"
android:background="#android:color/transparent"/>
How can I make the cursor stay always in the center of edittext? No matter if user enters something or not. Thanks.

Try adding the following code
android:gravity="center"
This makes sure that your text will always be entered in the center of the EditText. By default its value is left.
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/et_word"
android:ems="12"
android:gravity="center"
android:background="#android:color/transparent"/>

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="start"
android:maxLines="1"
android:gravity="center"
android:hint="Some hint text"
></EditText>

please Try adding the following code
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="start"
android:maxLines="1"
android:gravity="center"
android:id="#+id/et_word"
android:ems="12"
android:background="#android:color/transparent"/>

Here you can add to property in xml file to gravity center then you can get cursor center
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/et_word"
android:ems="12"
android:gravity="center"
android:background="#android:color/transparent"/>
That can be write from center also.

Related

Edit text overlaps each other

I am creating an android application in which i am collecting user's some messages.But i am getting some issues such as following..
I have added 3 edit text in alert dialog..the third edit text box having multi line textbox so whenever i am adding multiple lines its getting overlapped to above two edit text
here is the snapshot of the error
Here Is Layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<ScrollView
android:layout_width="245dp"
android:layout_height="245dp"
android:layout_weight="0.40"
android:layout_gravity="center_horizontal">
<RelativeLayout
android:layout_width="245dp"
android:layout_height="200dp">
<EditText
android:layout_width="235dp"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/editText6"
android:layout_gravity="center_horizontal"
android:hint="Your Email Id"
android:layout_alignBaseline="#+id/editText5"
android:layout_alignBottom="#+id/editText5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="240dp"
android:layout_height="wrap_content"
android:id="#+id/editText4"
android:layout_gravity="center_horizontal"
android:hint="Title"
android:singleLine="true"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="236dp"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/editText5"
android:hint="Description"
android:layout_gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" /></RelativeLayout></ScrollView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="#+id/button10"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Reason for your issue is android:inputType="textMultiLine" pushes content when there is a new line!
You can use android:maxLines="1" for your EditText [any way this will be useful for you since you have used android:singleLine="true" with is kind of dipricated ]
Ps: Hard coding android:maxLength will not solve your issue since screens can be in different sizes which makes you hard to decide how many lines you keep!
Also you can use android:maxHeight="xdp" for the editText view, then the limit won't exeed!
You should use
android:maxLength="4"
row numbers you want to fix for your edit text
Use LinearLayout with vertical orientation instead of RelativeLayout.
If u want to use RelativeLayout, then set layout_below attribute of RelativeLayout.
You need to set android:maxLines="4". Set your max lines to solve this issue.
<EditText
android:id="#+id/editText4"
android:maxLines="4"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:hint="Title"
android:layout_alignParentStart="true" />
You can use android:layout_below and android:layout_marginTop in your EditText views and keep distance between your views.
Example :
<EditText
android:layout_width="236dp"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/editText5"
android:hint="Description"
android:layout_gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/editText4"
android:layout_marginTop="20dp"/>

TextInputLayout EditText gravity not working

I have started using TextInputLayout. I want the hint of the EditText to align in the center. But for for some reason the hint isn't getting aligned to the center. However, once I start typing in the EditText field, the text is tetting aligned to the centre. Please help me with the proper alignment. I want my hint to be center aligned as well.
<android.support.design.widget.TextInputLayout
android:id="#+id/dobLyt"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/dob"
style="#style/MyEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:hint="Date of Birth"
android:inputType="number"
android:textColor="#333333"
android:textSize="15dp" />
</android.support.design.widget.TextInputLayout>
use this
<EditText
android:id="#+id/et"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:ems="10"
android:gravity="center_vertical|center_horizontal"
android:inputType="numberDecimal"/>

Android EditText text is not vertically centered

I have search bar that defines EditBox like
<EditText
android:id="#+id/search_editbox"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/search_execute_imageview"
android:layout_toRightOf="#id/layout_back_logo"
android:gravity="left"
android:hint="#string/search_hint"
android:maxLength="20"
android:textCursorDrawable="#null"
android:drawableRight="#drawable/abc_ic_clear_holo_light"
android:background="#drawable/rounded_edittext_states"
android:padding="5dip"
android:singleLine="true"
android:textSize="#dimen/main_text_size" />
Despite android:layout_centerVertical="true" the text is not in the middle, but closer to upper side.
Use
android:gravity="left|center_vertical"
The correct answer is right but it is recommended to use:
android:gravity="start|center_vertical" instead of ("left") to avoid formatting errors.
try this:
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:ems="10"
android:gravity="left|center_horizontal"
android:textAlignment="center" >
</EditText>
just change your gravity value to "left center align".
android:gravity:"left|center"

Android align drawable and text in EditText

i've an edittext :
<EditText android:id="#+id/chp"
android:layout_width="match_parent"
android:paddingLeft="10dp"
android:layout_height="340dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:gravity="top"
android:hint="#string/hintCommentaire"
android:textSize="25sp"
android:drawablePadding="10dp"
android:drawableLeft="#drawable/ic_comment"
android:layout_margin="5dp" />
The problem is, i've my hint at the top of the edittext, but the drawable stays in the center..
I tried with "drawableStart", but it's not working.
Someone to help me ?
EDIT : I want the text and drawable in the top(same line). (i want to keep the height of my edittext)
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/etMobile"
android:hint="Mobile"
android:fontFamily="sans-serif-normal"
android:singleLine="true"
android:textSize="15sp"
android:gravity="center|start"
android:textCursorDrawable="#null"
android:drawableStart="#drawable/ic_profile_bio"
android:drawablePadding="10dp"
app:met_floatingLabel="normal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
Set this Property android:gravity="center|start" in above EditText

Android: how to make multiline text auto scroll down?

I want to make the multiline text auto scroll down as I add in more text:
<EditText
android:id="#+id/Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/TypeBox"
android:layout_below="#+id/ReceiverName"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_weight="0.65"
android:ems="10"
android:focusable="false"
android:gravity="bottom"
android:inputType="textMultiLine|textNoSuggestions" >
</EditText>
I have tried searching for it but all i could find is auto scroll of a text box within a scroll view.
android:inputType="textMultiLine"
android:editable="true"
android:enabled="true"
android:minLines="6"
android:maxLines="6"
android:isScrollContainer="true"
android:focusable="true"
Refer this link for more info
https://alvinalexander.com/source-code/android/android-edittext-isscrollcontainer-example-how-make-multiline-scrolling
I think, you need to add a line:
android:inputType="textFilter|textMultiLine|textNoSuggestions"
Add bellow code in activity onCreate(), this Add scroll behavior to
text view
textView.setMovementMbehavior ScrollingMovementMethod());
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:text="text"
android:textColor="#ffffff"
android:textSize="20sp"/>

Categories

Resources