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.
Related
I have a fixed height in dp and layout_width="wrap_content" for my TextView, and the textSize in sp. I do not want a fixed text size (due to variable screen density), and rather I would like the text to scale vertically to the fixed view bounds, and for the TextView width to wrap to the text. I tried using autoSizeTextType="uniform" but the text started wrapping to the next line, which was weird. Has anyone been able to accomplish this vertical scaling with layout_width="wrap_content" for the TextView?
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/name"
android:layout_height="50dp"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:autoSizeTextType="uniform"
android:text="Some Text"
android:background="#drawable/shape_tag_name"
/>
Have you tried use app:autoSizeTextType="uniform"?
Using android:autoSizeTextType="uniform" only works on API level 26 or higher.
One more thing, try to use android:maxLines="1".
I recommend you to have a look on this tutorial
Add two lines in your textview:
android:maxLines="1"
android:ellipsize="end"
Your code like :
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/name"
android:layout_height="50dp"
android:layout_width="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:textColor="#color/white"
android:autoSizeTextType="uniform"
android:text="Some Text"
android:background="#drawable/shape_tag_name"
/>
I woud like to EditText multi line text with fixed height and width without scrollbar. The scrollbar stays and I don't want it.
This my code for EditText:
<EditText
android:id="#+id/write_text"
android:layout_width="#dimen/_200mdp"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:layout_centerInParent="true"
android:inputType="textMultiLine|textCapWords"
android:background="#null"
android:textSize="#dimen/xxxl_fontsize"
android:lines="3"
android:maxLines="3"
android:minLines="1"
android:maxLength="144"
android:scrollHorizontally="false"
android:scrollbars="none"
/>
I have tried a lot but the scrollbar remains, I want the fixed height and width with inputType="textMultiLine|textCapWords" without scrollbar
Can any one help me.
you must not make the height warp_content.
android:layout_height="#dimen/yourHeight"
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 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.
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'