I have used EditText in my application, the problem is the entered text is not aligned properly, check it out my screen shot for what exactly my issue is.
My EditText XML file is
<EditText
android:id="#+id/signupFullName"
android:layout_width="179dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/updatedprofieledit"
android:ems="10"
android:hint="#string/enterfullanme"
android:inputType="textPersonName"
android:paddingLeft="10dip"
android:singleLine="true" >
<requestFocus />
</EditText>
Kindly help me to clear this issue, have tried with some EditText Specification but no luck.
Thanks
It appears that your background image is smaller than your hard-coded width. Adding padding reserves some "unused room" at the end of your EditText. It would be better if you converted your background image to a 9-patch image so that it could resize automatically.
add this line into your edit text
android:padding="5dp"
use this
android:paddingRight="10dip"
Related
I am working on android application in which i want to fix the size of the box of editfield. For example if i want to add 100 words on it, then it will not increase the size of the edit field. Characters should remains inside the box without increasing the size of edit text. I have paste the xml code of my editText along with the images.
<EditText
android:id="#+id/fnametxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.1"
android:background="#color/Beige"
android:enabled="false"
android:lines="1"
android:ems="15"
android:maxLines="1"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="ufyufgiuhjlkh"
android:textColor="#color/Black"
android:textSize="12sp" />
Thanks in advance
Fix your field's width.
You can do this in several ways:
LinearLayout with weights. Your container should have android:weightSum="100" (instead of 100 you can place your number). For example you can go here or here. Using weights you should set android:layout_width="0dp" in order to see result.
RelativeLayout with margins. Set first EditText to android:layout_alignParentRight="true" and android:layout_toRightOf="IdOfTextView". Set right margin to (e.g.) 10dp. EditField will be stretched in order to fill all free space.
You can set android:maxWidth to some value in dp or px. Or maxLength or maxEms can do the trick.
Change:
android:layout_width="wrap_content"
To Something like
android:layout_width="200dp"
or whatever size you choose.
If your are intending to set the height using the layout_weight property set your
layout_height="0"
otherwise set
layout_height="100dp"
or whatever you want your fixed height to be.
Maybe you need to use the XML android:maxLines and android:lines attribute and manipulate the maxLength.
<EditText
android:id="#+id/fnametxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.1"
android:background="#color/Beige"
android:enabled="false"
android:lines="5"
android:ems="15"
android:maxLines="100"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="ufyufgiuhjlkh"
android:textColor="#color/Black"
android:textSize="12sp" />
Source: Android Developers Max Lines.
I want to make a multi line edit text but the problem is that whenever i enter new line height of edit text increases. how to fix its height. My code is
<EditText
android:id="#+id/editAdditionalInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editPhoneCutomerDetails"
android:layout_alignParentBottom="true"
android:layout_marginBottom="60dp"
android:layout_below="#+id/editPhoneCutomerDetails"
android:layout_marginTop="10dp"
android:background="#drawable/message_additional_box1x"
android:ems="10"
android:padding="10dp"
android:gravity="top">
<requestFocus />
</EditText>
To fix the size of editText you can use
android:singleLine="true"
but it can limit your editText line to 1 .
if you want more lines in editText then use the following property.
Use android:lines="3" .
Use android:lines="2" for fixing multilines and for fixing single line use android:singleLine="true"
for example use like this as shown for multilines
<EditText
android:id="#+id/.."
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:drawable/editbox_background_normal"
android:ems="10"
android:hint="Name"
android:maxLength="20"
android:lines="2"// fix the multiline limit
android:textColor="#000000" />
for example use like this as shown for single line
<EditText
android:id="#+id/.."
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:drawable/editbox_background_normal"
android:ems="10"
android:hint="Name"
android:maxLength="20"
android:singleLine="true"// fix the single line limit
android:textColor="#000000" />
try this;
android:inputType="textMultiLine"
<EditText
android:inputType="textMultiLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
As singleline is now deprecated use inputType:multiline and set the number of lines you want with lines tag
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="4"/>
I had a similar requirement but ended up not changing the size of the box. i.e it grows as text is typed into it but stops short of blocking the other widgets in view. I read this post during my quest and had found a solution. Decided to post it back here. Restricting size to a value may possibly have different effects on different screens. So I think it is better to let it be.
The thing is to put the edit text inside a linearlayout with the rest of the widgets. The code and snapshot are here
Multi line edit box with ok cancel buttons
I have seen many similar questions to this one but I think this still covers new ground:
1) Hint text disappears when you set gravity
2) android:ellipsize="start" fixes that so you can have centered hints and centered text
3) why does the code below still show centered hint text?
<EditText
android:id="#+id/details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="use this area to provide specific identification information including approximate size, vessel name and registration numbers if available"
android:background="#android:drawable/editbox_background"
android:layout_below="#id/details_title"
/>
is it because of fill_parent instead of wrap_content? i thought maybe it was because this hint is multiline, but i shortened it and it still appeared and was centered. those are the only differences between this and my other EditTexts that needed android:ellipsize="start" to display correctly. is it just a bug?
You need to use android:gravity="top" instead of center in order to have your hint and your text start at the top left of the box.
add this attribute to your EditText
android:ellipsize="start"
It just works
<EditText
android:id="#+id/details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:ellipsize="start"
android:hint="use this area to provide........"
android:background="#android:drawable/editbox_background"
android:layout_below="#id/details_title"
/>
My EditText configured as follows won't show the hint:
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:hint="The hint..."
android:scrollHorizontally="true"
android:singleLine="true" />
It works if I set android:gravity="left" or if I remove android:scrollHorizontally and android:singleLine attributes, which is not desirable. Any suggestions?
using android:ellipsize="end" fixed it for me
Weird bug !! (but Android has a lot of these weirdo bug)
In Lollipop version the default text and hint text color is white for EditText.
So we have to change like this in EditText
android:textColorHint="#color/grey"
I wanted my single-line EditText box to scroll but keep the hint on the right also.
I had the same issue and got the hint to stick by keeping gravity="right", and setting singleLine="true" and ellipsize="end".
You need to give text color to hint
android:textColorHint="#000000"
No need of android:scrollHorizontally attribute. Remove it.EditText is a fixed item on the screen. we want scroll the layout contains the EditText is enough. that is the best design too. you have put android:ellipsize="end" instead of android:scrollHorizontally.
Using android:ellipsize="end" resolves the obvious platform bug. Unfortunately, Xperias still misbehave :(
I found no other solution than to:
if (android.os.Build.MANUFACTURER.matches(".*[Ss]ony.*"))
editText.setGravity(Gravity.LEFT);
else
editText.setGravity(Gravity.CENTER);
The below worked for me:
<EditText
android:id="#+id/UserText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/UserPassword"
android:layout_marginTop="85dp"
android:ems="10"
android:hint="#string/UserHint"
android:inputType="textPersonName"
android:singleLine="true"
android:text="#string/UserName" >
<requestFocus />
</EditText>
This is how, I did for by EditText to have hint in it.
<EditText
android:id="#+id/productQuantity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right|center_vertical"
android:hint="#string/quantity"
android:inputType="numberSigned"
android:ellipsize="end"
android:singleLine="true" >
</EditText>
Try changing the hint text color, sometimes the hint color is the same as the background color
android:textColorHint="#color/colorBlack"
I changed the style to "#style/Base.WidgetMaterialComponents.TextImputEditText". For me it wasn't gravity or color.
I have an EditText and a Button set next to each other on the same horizontal line. It looks great, except when the user enters a lot of text, the EditText is resized, and the Button is squished.
I have both EditText and Button set to layout_width="wrap_content". "fill_parent" messes up the layout, and I don't want to use absolute sizes if I don't have to - the way it is now looks great in both landscape and portrait, I just don't want the EditText to resize.
My layout:
<TableLayout
android:id="#+id/homelayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<TextView
android:id="#+id/labelartist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Find artists:" />
</TableRow>
<TableRow>
<EditText
android:id="#+id/entryartist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="6"
android:background="#android:drawable/editbox_background"
android:editable="true"
android:padding="5px"
android:singleLine="true" />
<Button
android:id="#+id/okartist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:layout_weight="1"
android:text="Search" />
</TableRow>
</TableLayout>
For EditText use
android:layout_width="0dp" - not required but preferred.
android:layout_weight="1"
And for Button dont specify android:layout_weight
What I do is in the onCreate for the activity, measure the EditText first then apply its maxWidth.
You can do so using code similar to the following:
EditText someedittext = (EditText)findViewById(R.id.sometextview);
someedittext.setMaxWidth(someedittext.getWidth());
Give EditText a maxWidth. That should stop it from resizing beyond the width you provided. Also, if you want it to be contained within 1 line set the maxLines setting to 1 too.
You need to add android:imeOptions to prevent this behavior.
Just use:
android:layout_width="fill_parent"
for both EditText and Button. It will also work set for EditText but it is better to have it on both.
you can define size of your edittext as it shows above
android:minWidth="80dp"
android:maxWidth="80dp"
or
android:layout_width="60dp"
or
you can apply a background image of size you want
but don't forget to define its height and width
with wrap_content.
android:layout_width="wrap_content"
android:layout_height="wrap_content"
I had a similar problem, but I couldn't get the above to work. What I did, is take in the text from EditText box and then format it down to the max size that my screen could handle. When I save this off to the DB, I will save it as display Text and original text.
If you go to this page I put a longer explanation.
The correct way to do this would be to set the android:minWidth and android:maxWidth equal to the width you want. This way you will not have to adjust your layout to use the android:layout_weight.
For example if you want your EditText to always be 80 density pixels no matter how many characters you type in use the following:
android:minWidth="80dp"
android:maxWidth="80dp"
I had the exact query. But i had an EditText and a Button next to each other in a Linear Layout.
I tried android:layout_weight="1" for EditText
android:layout_width="0dp"
And it worked just perfect!!
Thanks a lot!
The chosen solution works, but let me add a little complement:
if you use
android:layout_weight="0.15" (the value is not important)
then
android:layout_width="0dp" if the LinearLayout is Horizontal
or
android:layout_height="0dp" if the LinearLayout is Vertical.