Dynamic ellipsis support for Android autosizing TextViews - android

The new Autosizing TextViews are pretty awesome, but it seems a fundamental thing is missing: ellipses.
Adding ellipses still requires defining the maxLines attribute, but if I want to be able to dynamically resize the text size according to the text view boundaries, I'd also like to be able to dynamically add ellipses when needed. Right now, if the text doesn't fit even with the minimum text size, it just gets cropped.
How could I add support for dynamic ellipses without giving up the new autosizing support?

The best solution I came up with so far was to programmatically set the maxLines to the proper value on runtime. Something like this will get the job done:
fun TextView.setMaxLinesForEllipsizing() = doOnPreDraw {
val numberOfCompletelyVisibleLines = (measuredHeight - paddingTop - paddingBottom) / lineHeight
maxLines = numberOfCompletelyVisibleLines
}
Be aware that this depends on Android KTX (but can also be easily achieved with a regular OnPreDrawListener).
Then we can simply call this extension from any TextView we want to get the dynamic ellipsis.
textView.setMaxLinesForEllipsizing()
If the text changes it might be necessary to call it again, though. So it might also possible to reach a more complete (and complicated) solution by moving this logic to a custom TextView and maybe overriding onTextChanged() there.

Related

Android MotionLayout custom attribute methodName

What is the usage of custom attribute in MotionLayout "methodName"?
an example will be great :-)
I'm looking for a good solution for cases where I've a textView and I need to change the fontFamily or gravity when it's not supported by MotionLayout.
MethodName and custom method allows you to call Methods that are not in the standard set and get
Usually associated with
for example:
<KeyTrigger motion:framePosition="60"
motion:motionTarget="#+id/button" motion:onNegativeCross=".">
<CustomMethod motion:methodName="performHapticFeedback" motion:customIntegerValue="3"/>
</KeyTrigger>
Generally the TextView is not design with animation and would not give you what you are looking for.
For example gravity, Change it would not animate the text to the new position because they are positions
Animation of FontFamily etc is also not easy.
To change things like that you really have 2 choices:
Cross fading between two textView
Use MotionLabel which is limited but was designed with animation in mine.
MotionLabel
MotionLabel has the limitation of only supporting a single line of text
But provides a rich set of attributes you can use to animate
for example instead of gravity with its limited "start" "center" etc.
It supports textPanX and textPanY where textPanX=0.0 is center, -1.0 is left justified, and +1.0 right justified.
allowing you to smoothly pan from center to left.
It also support scaleFromTextSize to allow you to set a base text size which the text will scale from. Animating the text size will cause loading a different font on each frame.
For more information on MotionLabel the see the GitHub

Is there a way to fix letter width in EditText?

I am trying to find a library or a simple trick to fix the width of each letter in an EditText so that the dashed lines on the background match exactly the number in the EditText. You can see the photo for the desired effect.
Using various widget i.e. Edittext in a row with the text style to underline will helps to get these type of design else i don't think there is any hard and fast rule or tricks.
There is no simple way of doing this, especially not when aligning to a given background, because many factors such as display size and density can change the relative positioning of the EditText. You could perhaps work around this limitation by drawing the white lines under each digit yourself, using a custom font or underlining and separating the digits with a space. See this question for a more detailed explanation on the limitations and possibile solutions concerning letter spacing.
I only know that, font monospace will be useful. Other fonts set different widths to each character.

Can I get and set textcolor of a textView using Accessibility in Android

I am developing an app for color-blind, I want to know if I can change the textcolor of a textView using Accessibility in Android.
Also, I would like to change the textSize using Accessibility.
Can I do these things?, If yes, how?
No, you cannot. You don't get the actual TextView of the represented text. You get access to an AccessibilityNodeInfo. The accessibility APIs don't provide this information. You could do some hacky things if you also controlled the app content, but if you want something that will work universally over all applications, you simply can't do this.
You could guess at the size of the text by checking the size of the TextView. The size of the TextView is passed to you through
aNodeInfo.getBoundsInScreen(resultBounds);
Although, this is very hacky and not reliable. The size of the view and the text size don't have to be the same, or even remotely related. Though generally for single line TextViews there will be a tight correlation. Notably, you cannot detect when a TextView is single line :)
For text size of the textview you can provide size in terms of 'sp' instead of 'dp'. Framework will automatically take care of size.
And for color above answer given by #rakesh can be one of the answers.

Autosizing text in TextView on Android

Is it possible to set TextView's text size to be the same as TextView height, when TextView height isn't predefined(WRAP_CONTENT or FILL_PARENT)?
solution : Auto Scale TextView Text to Fit within Bounds
i also wanted to do something like this and the closest you can seem to get is to say android:textSize=20dp (or whatever size you think is appropriate) for either your style or each element that is displaying text. since dp is device independent pixels, if it appears to be taking up the whole screen on your device, then it is supposed to appear that way on all other devices too. you might want to check on this as you might have to choose a different dp value for each of the different size/density combinations possible (depending on what kind of devices you are aimed at, also whether you are allowing the use to change the orientation) this has all that info.
I've found the library that do exactly what I want : SizeAdjustingTextView

Is it possible to style the android fonts styles with leading and tracking?

Is it possible to have the followings in android font styling.
Leading (the space vertically between lines of text - name comes from the physical piece of lead that used to be used in mechanical printing process to separate lines of text).
Tracking (the horizontal space between each character).
If you have any ideas please share with me.
You can change leading by calling TextView's method setLineSpacing() or changing corresponding XML attributes of TextView in layout (android:lineSpacingExtra or android:lineSpacingMultiplier).
As answered here:
AFAIK, you cannot adjust kerning in TextView. You may be able to adjust kerning if you draw the text on the Canvas yourself using the 2D graphics APIs.
Update: since API 21 there is an option to set kerning/tracking/letter spacing. You can call method setLetterSpacing() or set it in XML with attribute letterSpacing.
For Tracking, check out this answer. It works fine for me.

Categories

Resources