I have this text view
<TextView android:id="#+id/name"
android:singleLine="true"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="16sp"
android:layout_width="fill_parent"/>
The problem is if the text is bigger then the textview can display it just stop displaying it looks something like this |some unfinished tex| I want to have dots on the end in this way it will be more clear to the user that this is unfinished and it it is displayed only a part of the text
I would prefer something like this |Some unfinished te..|
How to implement this ?
Have you tried setting android:ellipsize paremeter to "end"?
I used the hack by checking text size every time i set it to textview and appended those dots after a substring of text.I don't know it is bad practice or so!! :P
you can do this type;
public String getChar(String inputstring){
String substring;
if(inputstring.length() >0 ){
substring=inputstring.substring(int beginIndex, int endIndex);
}
StringBuilder sb=new StringBuilder(substring);
sb.append("....");
return sb.tostring();
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 days ago.
Improve this question
1--> Ive been trying to make a calculator app. My problem is that I want my app to ignore unnecessary repeating input by the user to avoid having this kind of repeating zeros(0) and dot(.) : 00000.003, 3..03, 3+00000.003, etc
I'm new here so I don't know what else I could experiment on.
I've tried a combination of while loop with nested ifs with some conditions like text.startsWith, .endsWith("str", ignorecase: false) something like that for my onclick buttons.
Some code suggestions is highly appreciated.
2-->Also i've been thinking if there's a series of command/code where an individual textview will be created every time characters(created by onclick buttons) formed an integer/expression/decimalnumber(etc) as well as operation(+,-,*,/)
in a way that every number and math operations are separated into textviews(for editing purposes)
I hope someone can give me an idea and codes/commands that I might try to experiment with to develop my app
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
or
In this sample code, the text in the EditText is first converted to an Int value. If the conversion fails (that is, there is no numeric value in the text box), 0 is set by default. Next, the EditText's text is replaced with the numeric value with the redundant 0's removed.
val editText: EditText = findViewById(R.id.editText)
val number: Int = editText.text.toString().toIntOrNull() ?: 0
editText.setText(number.toString())
allows only text values to be entered
val editText: EditText = findViewById(R.id.editText)
editText.filters = arrayOf(InputFilter { source, _, _, _, _, _ ->
if (source.toString().matches(Regex("[0-9]+"))) {
source
} else {
""
}
})
I don't want to use scrollView.The behavior I want is this:
Short Text:
Long text:
Have you tried minLines in XML?
something like this:
<TextView
android:id="#+id/tv_previous_story"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:minLines="2" //(could be any minimum amount of lines)
android:textColor="#color/text_color"
android:textSize="#dimen/privacy_text_size" />
this should work fine for you if you always have at least two words (or the minimal amount of lines you've set in the XML)...
If this isn't what you search for, maybe you could handle it programmatically and set the width and height of the TextView according to the number of words... not the exact method, but for example:
void updateWidthAndHeightOfTextView(TextView tv, String t) {
tv.setWidth(t.length * [bla bla bla]);
tv.setHeight(tv.getWidth() / [bla bla bla]); //(if you want to change the height too, in this case according to the width)
}
You write it your way, you know your needs...
And, there's also this answer, that I'm actually not familiar with but might also answer your needs...
UPDATE
In case I didn't understand when I wrote the above and you want the TextView to have maxLines and only dynamic width, you could use the tv.setWidth(t.length * [bla bla bla]); in the semi method that I wrote above, and in the .XML use maxLines...
Also, next time please give a bit more specific questions so people won't have to struggle to try to understand how to give you the relevant answer to what you want.
I hope it helps you!
Try this:
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:text="Your text here"
android:textColor="#color/purple_500"
android:textStyle="bold"
android:lines="2"
android:gravity="center"
android:autoSizeTextType="uniform"
android:autoSizeMinTextSize="16sp"
android:autoSizeMaxTextSize="100sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
For short text your output be like:
For long text:
Databinding is the most commonly used to bind UI views in layout to data source. that is why I decided to use this approach.
Actually, I am to concatenate to of the values from data source and to be shown in view. concatenation is not a big deal I just added "+" operator between the values or variables of the data source.
The problem is how to add space between two of them.
concatenating two of the values by "+" operator was working fine.
<TextView
android:id="#+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="#sample/title"
android:text="#{item.location.get(0).getlocation_text + item.location.get(0).city}"
android:maxLines="2"
android:ellipsize="end"
android:gravity="start"
android:textAppearance="#style/TextDesc2"
card_view:layout_constraintTop_toBottomOf="#+id/title"
card_view:layout_constraintStart_toStartOf="parent"
card_view:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="8dp" card_view:layout_constraintHorizontal_bias="0.0"/>
I need the result as "locatin_text, city"
but now I am getting "location_textcity"
You can use double quotes with back quotes or single quotes with double quotes. Check this Android developer document reference.
1st way
android:text="#{item.location.get(0).getlocation_text + `, ` + item.location.get(0).city}"
2nd way
android:text='#{item.location.get(0).getlocation_text + ", " + item.location.get(0).city}'
try this
android:text='#{String.format("%s %s", item.location.get(0).getlocation_text, item.location.get(0).city)}'
I have an UItextView and i set the AttributedText at the starting of the app so with en empty text because user didn't yet fill anything. The problem is that with an empty String the AttributedText seam to not apply for the new text i will enter. how to do ?
As far as I know there is no straight solution for that. Setting AttributedText with "" doesn't work.
However you can do easy fix:
if let text = field.attributedText?.string {
//normal way
} else {
field.font = ...
field.fontColor = ...
//sorry, no shadow and other nice tricks
}
Of course you could implement delegate to text field and adjust attributes when textFieldDidChange, but that doesn't work well with typing in Chinese language where letter can be composed from multiple characters so I couldn't use that.
What is the best way to find a string (sentence of 1-3 lines) in the multiline textfield.
I have a textfield with a list of messages. In order to change every second messages color, i have to get the index where this message beggins.
ANy ideas?
I solved my problem. Maybe it will be useful for someone.
As i'm appending text, i use textfield.caretIndex to see the inserts. So i'm switching formats using this function:
if (i % 2 != 0) {
textfield.setTextFormat(colorFormat, lastCaret , textfield.caretIndex);
formatStart = textfield.caretIndex;
}
else {
textfield.setTextFormat(textFormat, formatStart, textfield.caretIndex);
lastCaret = textfield.caretIndex;
}