I want to set a maximum width of an edit box. So I created this small layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxWidth="150dp" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10" />
</LinearLayout>
But it doesn't work, the box could be more than 150 dp anyway. android:maxWidth="150dp" in the EditText will give the same result. I have read ( maxWidth doesn't work with fill_parent ) that setting both max- and min width to the same size should solve it:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:maxWidth="50dp"
android:minWidth="50dp" />
But it doesn't.
I have found a solution to my problem here here:
Setting a maximum width on a ViewGroup
And this works great. But I guess the maxWidth attribute is here for I reason. How should it be used? Or is there any documentation on this? I have spend hours on this problem, and still doesn't understand how to use this simple attribute.
I want to set a maximum width of an edit box.
In your example:
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10" />
The layout_width and ems attributes are trying to set the same value. Android seems to choose the larger fill_parent value and ignores the other. And when you use this:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:maxWidth="50dp"
android:minWidth="50dp" />
Here ems and maxWidth are trying to set the same value, again the greater value is used. So depending on what you actually want you can use:
<EditText
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
or replace android:ems="10" with android:maxWidth="50dp" if you prefer to work with dp.
Lastly your LinearLayout only has one child, the EditText. Typically when this happens you can remove the LinearLayout tags and use the EditText by itself.
To those who are seeing this question in recent years, we have app:layout_constraintWidth_max="225dp" now. Put your layout inside contsraint layout and use this on the view you want to have max width.
Related
It seems that maxWidth has no effect on EditText. Could anyone shed some light on this?
<EditText
android:id="#+id/editTextFoo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autofillHints="port"
android:ems="10"
android:maxWidth="100dp"
tools:targetApi="o" />
The above code results in the following:
If I set layout_width="100dp" as following:
<EditText
android:id="#+id/editTextFoo"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:autofillHints="port"
android:ems="10"
android:maxWidth="100dp"
tools:targetApi="o" />
the width is correct:
Yes I just checked in the IDE and it's because ems="10" is preventing the view from maintaining maxWidth 100dp. Apparently ems of 10 requires a larger width so it is overriding the maxWidth attribute.
Instead of ems go with minWidth to prevent the view from shrinking.
Alternatively, you can use maxEms and minEms.
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 have edittext view in my application but when i insert long text in edit text this increases view size.I do not want happened in my app because it created bad imapact on app so I decided to use
android:singleLine="true"
android:ellipsize="end"
android:maxLine="1"
xml attribute to the edit text but it not worked for me .I don't have any idea how to prevent this from my problem.I did lot of R&D but i am not get satisfactory answer.Here is my Edit Text.
<EditText
android:id="#+id/etaccountholdersname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvaccountholdername"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#drawable/ic_input_field"
android:hint="Account holder's name"
android:singleLine="true"
/>
Here Is full code of Xml file
<RelativeLayout
android:id="#+id/innerrel1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<TextView
android:id="#+id/tvaccountholdername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#drawable/ic_account_holders_name_text"
android:gravity="left" />
<EditText
android:id="#+id/etaccountholdersname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvaccountholdername"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#drawable/ic_input_field"
android:hint="Account holder's name"
android:singleLine="true"
android:scrollbars="vertical" />
</RelativeLayout>
Please provide some help me to solve this issues.
In your layout design you have "wrap_content" for both parent and children views. Make the parent layout width as static. If you working with multiple screens, use the dimens.xml for the sizes of the views in different screen sizes.
Use either android:layout_weight property or use android:ems="7" it will work for you definetely
do not use
android:ellipsize="end"
its uses for textview
use
android:ems="10" (it limits edittext to show 10 charecters but you can insert many characters)
and you are good to go.
Is there any way to make smaller DatePicker and TimePicker (but to be visible all parts ) in Android ? I tried to set layout_width="130dp" but then DatePicker isn't visible, just left upper corner.
Use properties follow:
Example smaller 80% original
android:scaleY="0.8"
android:scaleX="0.8"
(This actually works and others don't) After much searching this worked:
<DatePicker
android:id="#+id/dp_datepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="0.7"
android:scaleY="0.7"
android:layout_marginLeft="-30dp"
android:layout_marginRight="-30dp"
android:datePickerMode="spinner"
/>
<TimePicker
android:id="#+id/tp_timepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="0.7"
android:scaleY="0.7"
android:layout_marginLeft="-30dp"
android:layout_marginRight="-30dp"
android:timePickerMode="spinner"
/>
The android:scaleX="0.7" is what shrinked the visible picker (it works for DatePicker and TimePicker) and the android:layout_marginLeft="-30dp" allowed for the space to be reduced.
Note:
All attempts at using android:padding="-30dp" did not work and using android:layout_margin="-30dp" (without a side specified) also did not work. Hopefully this helps those who were as lost as me
P.S. Google get your freaking DatePicker API fixed!
Instead of using "dp" for the layout width , use percentages in weight to make it smaller.
For android:layout_weight to be effective, put android:layout_width to 0 dip.
android:layout_height="wrap_content"
android:layout_weight=".33"
android:layout_width="0dip"
android:textSize="12sp"
This worked for me to fit a date and time picker in a dialogue with a custom layout in landscape mode on a small screen. Set the weightsum of a container (LinarLayout) to 100 and then fill it with widgets adding up to 100 in layout_weight property.
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="100"
android:layout_gravity="center_horizontal">
<DatePicker
android:id="#+id/dlgDateTimePickerDate"
android:layout_width="wrap_content"
android:layout_weight="50"
android:layout_height="wrap_content" />
<TimePicker
android:id="#+id/dlgDateTimePickerTime"
android:layout_weight="50"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
You can simple scale like this:
android:scaleY="0.5"
android:scaleX="0.5"
to get the half of the size.
And to get a smaller padding, android:padding="-20dp"
Looking from the framework the picker may overlap other views, but in the app, it will not.
The padding is going to work perfectly and the buttons also.
Another solution:
Put it in a LinearLayout and set the height of the LinearLayout manually
I have this EditText and Button, and I have to reduce its height. I try with android:height="10px" but it doesn't work. Btw android:width="180px" works OK, then I don't know why I can't adjust the height.
Here is the code:
<EditText
android:id="#+id/Movile"
android:layout_alignBaseline="#+id/MovileLabel"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="180px"
android:height="10px"/>
<Button
android:id="#+id/inviteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/invite"
android:width="100px"
android:height="5px"
android:layout_marginLeft="40dip"/>
Instead of setting values to android:height="10px" use android:layout_height="10px"
android:layout_width="180px"
android:layout_height="10px"
use android:minHeight="120dp" attribute.
if need to increase height dynamically add
android:layout_height="wrap_content"
You need to get rid of the "wrap_content" properties and just have the fixed width and height as so:
<EditText
android:id="#+id/Movile"
android:layout_alignBaseline="#+id/MovileLabel"
android:layout_alignParentRight="true"
android:layout_width="180px"
android:layout_height="10px"/>
<Button
android:id="#+id/inviteButton"
android:text="#string/invite"
android:layout_width="100px"
android:layout_height="5px"
android:layout_marginLeft="40dip"/>
You can't decrase size of TextEdit if the font is too big inside.
try android:textSize='6sp'