How to make a fix size of edit field android - android

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.

Related

Edit Text view increase its size when inserted long text in android

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.

Limitation of TextView on Android

I want to set TextView max line 2 and show "..." in the line middle.
My layout xml as below:
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="abcdefghijklmnopqrstuvwxyz"
android:ellipsize="middle"
android:maxLines="2"
android:textAppearance="?android:attr/textAppearanceLarge" />
But it show as below:
abcdefgh
ijklmnop
It show 2 lines, but it doesn't show any "..." at middle or end.
How can I modify it? 
android:ellipsize will work only when there's not enough space to display the whole text of the TextView. In your case, you've set android:layout_width to fill_parent and the text is not long enough, so no ellipsize is being used. Change the android:layout_width to some small amount of dip and you'll see the difference. Hope this helps.
Try to set android:ellipsize="end".
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="abcdefghijklmnopqrstuvwxyz"
android:ellipsize="end" <-----change it to end
android:maxLines="2"
android:textAppearance="?android:attr/textAppearanceLarge" />
Set the android:ellipsize property on the TextView as required (probably to 'end').

How to use android:maxWidth?

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.

How to end TextView with 3 dots by using maxLength

I have a TextView in my layout which is wrap_content in layout_width. It is limited to maximum of 15 characters so I'm using maxLength.
I need to end this TextView with 3 dots (...) and it happens only when I give the layout_width a fixed size in dp, something that I don't want to do.
I know it is possible programmatically by trimming the string after the 15th character and then adding the 3 dots, but I prefer to do that by XML.
Any idea how to end the text with 3 dots and leave it wrap_content?
<TextView
android:id="#+id/inbox_contactName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"
android:maxLines="1"
android:ellipsize="end"
android:singleLine="true"
android:maxLength="15"
android:textColor="#0670b4"
android:textSize="16sp" />
This will solve your problem, Use ellipsize Property in your XML Code
android:ellipsize="end" <!-- This makes the magic ... thing -->
android:maxEms="15" <!-- Limit of the Text -->
android:singleLine="true" <!-- In case if you want everything in one line -->
Edit: singleLine is deprecated. Use maxlines="1" instead.
You cannot use both maxLength and Ellipsize although you can define Maximum EMS see the example below
<TextView
android:id="#+id/tv_hist_source_lang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxEms="8"
android:maxLines="1"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceMedium" />
I gather (from comment) that #Yaniv already solved it using code - but this is the right way to do it (with xml). May help other users who land here. Trick is to use both toleftof and torightof.
<RelativeLayout>
...
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:singleLine="true"
android:layout_toRightOf="#id/some_element1"
android:layout_toLeftOf="#id/some_element2"/>
...
<RelativeLayout>
You can use
android:maxWidth="100dp"
android:maxLines="1"
android:ellipsize="end"
Only this works for me.
One of the easiest way is to add Right Padding + Ellipsize
<TextView
android:id="#+id/txtvw_contentcell_subhead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Polem sampler, Lorem Ipsum golep tolem burop yemit noski"
android:ellipsize="end"
android:maxLines="1"
android:textColor="#color/caption_black_color"
android:textSize="#dimen/caption_size"
android:paddingRight="40dp"/>
Here is an example Subtitle Text with char limit.
use this android:ellipsize="end"
Tried most of the solutions above. Using maxWidth was the key for me:
android:maxWidth="100dp"
android:maxLines="1"
android:ellipsize="end"
Very Important: ellipsize = "end" only works when maxLength is set to a value that is more than the number of characters on one line.
You can use wrap_content if you align the TextView's end and start to any other view(s).
I needed to do this with Radio Button and I wanted to limit the size to one line. When I used Android:singleline="true", radio button's check circle disappeared. Here's the code that finally worked:
android:ellipsize="end"
android:maxLines="1"
There's no need to set ems. This will probably work also in TextView and other UI components. Hope this helps to someone.
You can just modify the String when it's length is above 20 to add the ellipsize.
remove the line from xml
android:lines="1"
android:maxLines="1"

Android, How to limit width of TextView (and add three dots at the end of text)?

I have a TextView that I want to limit characters of it. Actually, I can do this but the thing that I'm looking for is how to add three dots (...) at the end of string. This one shows the text has continue. This is my XML but there is no dots although it limit my text.
<TextView
android:id = "#+id/tvFixture"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_toLeftOf = "#id/ivFixture_Guest"
android:text = "#string/test_06"
android:lines = "1"
android:ems = "3"
android:gravity = "right"
style = "#style/simpletopic.black"
android:ellipsize="end"/>
Deprecated:
Add one more property android:singleLine="true" in your Textview
Updated:
android:ellipsize="end"
android:maxLines="1"
The following is what I learned by playing around with various options for forcing a TextView to a single line (with and without the three dots).
android:maxLines="1"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="one two three four five six seven eight nine ten" />
This just forces the text to one line. Any extra text is hidden.
Related:
android:maxLines
android:singleLine (Note this and this)
android:lines
ellipsize="end"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:text="one two three four five six seven eight nine ten" />
This cuts off the text that doesn't fit but lets users know that the text has been truncated by adding an ellipsis (the three dots).
Related:
ellipsize="start" (...aaabbbccc)
ellipsize="middle" (aaa...ccc)
android: Ellipsise , meaning of the options
ellipsize="marquee"
<TextView
android:id="#+id/MarqueeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="one two three four five six seven eight nine ten" />
This makes the text scroll automatically across the TextView. Note that sometimes it needs to be set in code:
textView.setSelected(true);
Supposedly android:maxLines="1" and android:singleLine="true" should do basically the same thing and since singleLine is apparently deprecated I would prefer not to use it, but when I take it out, the marquee doesn't scroll anymore. Taking maxLines out doesn't affect it, though.
Related:
Marquee text in Android
HorizontalScrollView with scrollHorizontally
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/horizontalScrollView">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:scrollHorizontally="true"
android:text="one two three four five six seven eight nine ten" />
</HorizontalScrollView>
This allows the user to manually scroll to see the whole line of text.
Try this property of TextView in your layout file..
android:ellipsize="end"
android:maxLines="1"
I take it you want to limit width to one line and not limit it by character? Since singleLine is deprecated, you could try using the following together:
android:maxLines="1"
android:scrollHorizontally="true"
android:ellipsize="end"
eg. you can use
android:maxLength="13"
this will restrict texview length to 13 but problem is if you try to add 3 dots(...), it wont display it, as it will be part of textview length.
String userName;
if (data.length() >= 13) {
userName = data.substring(0, 13)+ "...";
} else {
userName = data;
}
textView.setText(userName);
apart from this you have to use
android:maxLines="1"
Use
android:singleLine="true"
android:maxLines="1"
app:layout_constrainedWidth="true"
It's how my full TextView looks:
<TextView
android:id="#+id/message_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:maxLines="1"
android:singleLine="true"
android:text="NAME PLACEHOLDER MORE Text"
android:textColor="#android:color/black"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toStartOf="#id/message_check_sign"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintStart_toEndOf="#id/img_chat_contact"
app:layout_constraintTop_toTopOf="#id/img_chat_contact" />
I am using Horizonal Recyclerview.
1) Here in CardView, TextView gets distorted vertically when using
android:ellipsize="end"
android:maxLines="1"
Check the bold TextViews Wyman Group, Jaskolski...
2) But when I used singleLine along with ellipsize -
android:ellipsize="end"
android:singleLine="true"
Check the bold TextViews Wyman Group, Jaskolski...
2nd solution worked for me properly (using singleLine). Also I have tested in OS version: 4.1 and above (till 8.0), it's working fine without any crashes.
Steps to add '...' at end of the text if it is too long:
check that the text width is constant
add these two lines
android:ellipsize="end" android:maxLines="1"
Complete code of textview in constraint layout:
<TextView
android:layout_width="75dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textSize="15sp"
android:textAllCaps="false"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent" />
code:
TextView your_text_view = (TextView) findViewById(R.id.your_id_textview);
your_text_view.setEllipsize(TextUtils.TruncateAt.END);
xml:
android:maxLines = "5"
e.g.
In Matthew 13, the disciples asked Jesus why He spoke to the crowds in parables. He answered, "It has been given to you to know the mysteries of the kingdom of heaven, but to them it has not been given.
Output:
In Matthew 13, the disciples asked Jesus why He spoke to the crowds in parables. He answered, "It has been given to you to know...
I got the desired result by using
android:maxLines="2"
android:minLines="2"
android:ellipsize="end"
The trick is set maxLines and minLines to the same value... and Not just android:lines = "2", dosen't do the trick.
Also you are avoiding any deprecated attributes.
You can limit your textview's number of characters and add (...) after the text.
Suppose You need to show 5 letters only and thereafter you need to show (...), Just do the following :
String YourString = "abcdefghijk";
if(YourString.length()>5){
YourString = YourString.substring(0,4)+"...";
your_text_view.setText(YourString);
}else{
your_text_view.setText(YourString); //Dont do any change
}
a little hack ^_^. Though its not a good solution. But a work around which worked for me :D
EDIT:
I have added check for less character as per your limited no. of characters.
You need to add following lines into your layout for the textview
android:maxLines="1"
android:ellipsize="end"
android:singleLine="true"
Hope this works for you.
In order to work with the android:ellipsize attribute, you have to limit the layout width of the TextView, such that the text is out of bounds from view of TextView.
So, android:layout_width attribute plays a key role here, set it accordingly.
One example can be:
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:text="This is a very long text to be displayed"
android:textSize="12sp"
android:maxLines="1"
/>
Now, here if the text in android:text="This is a very long text to be displayed" goes out of view from TextView with a android:layout_width="120dp", android:ellipsize="end" will truncate the text and place ...(3 dots) after it. i.e. This is very long... will be displayed in the TextView.
<TextView
android:id="#+id/product_description"
android:layout_width="165dp"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:text="Pack of 4 summer printed pajama"
android:textColor="#d2131c"
android:textSize="12sp"
android:maxLines="2"
android:ellipsize="end"/>
I think you give fix height and width of text view. Then your solution will work.
you can write this line in xml where you take the textview :
android:singleLine="true"
The approach of #AzharShaikh works fine.
android:ellipsize="end"
android:maxLines="1"
But I realize a trouble that TextView will be truncated by word (in default). Show if we have a text like:
test long_line_without_any_space_abcdefgh
the TextView will display:
test...
And I found solution to handle this trouble, replace spaces with the unicode no-break space character, it makes TextView wrap on characters instead of words:
yourString.replace(" ", "\u00A0");
The result:
test long_line_without_any_space_abc...
Apart from
android:ellipsize="end"
android:maxLines="1"
you should set
android:layout_width="0dp"
also know as "match constraint", because the wrap_content value just expands the box to fit the whole text, and the ellipsize property can't make its effect.
Simple for three dots
android:layout_width="100dp" <!--your dp or match_parent or 0dp>
android:maxLines="2" <!--count your line>
android:ellipsize="end"
Add These two lines in your text
android:ellipsize="end"
android:singleLine="true"
you can do that by xml:
<TextView
android:id="#+id/textview"
android:maxLines="1" // or any number of lines you want
android:ellipsize="end"
/>
set android:maxLength="8" in Textview
if you want to set yourString >5. then set textview length 5+3 (for three-dot)
if (yourString.length()>5) //
{
textview.setText(yourString.substring(0,5)+"...");
}
else {
textview.setText(title);
}
1.set static width like 7odp
2.use android:ellipsize="end"
3.use android:maxLines="1"
4.use android:singleLine="true"
<TextView
android:id="#+id/tv_status"
**android:layout_width="70dp"**
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/padding_8"
android:gravity="center|center_horizontal"
android:includeFontPadding="false"
android:textColor="#color/black_2a"
android:textSize="#dimen/text_size_1"
**android:ellipsize="end"
android:maxLines="1"
android:singleLine="true"**
app:layout_constrainedWidth="true"
app:borrowStatusText="#{item.lenders[0].status}"
app:layout_constraintEnd_toStartOf="#id/iv_vector"
app:layout_constraintTop_toTopOf="parent" />
In this way you can set the maximum length of the view and, at the same time, you will have dots at the end ("num" is your number of dp):
android:maxWidth="{num}dp"
android:ellipsize="end"
You just change ...
android:layout_width="wrap_content"
Use this below line
android:layout_width="match_parent"
.......
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/visitBox"
android:orientation="vertical" >
<TextView
android:id="#+id/txvrequestTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="Abcdefghighiklmnon"
android:textAppearance="?
android:attr/textAppearanceLarge"
android:textColor="#color/orange_color" />
</LinearLayout>

Categories

Resources