Android: Difference between setTextAlignment and setGravity - android

I'm working with an EditText, and know that setTextAlignment was not implemented until API 17, but don't see how using setTextAlignment is any easier or better than setGravity. Which should I use for what purposes, or does it not matter?
I find setGravity to be very easy to work with and do what I want, so when should I NOT use this?
Thank you

setTextAlignment() is easier to work with in right-to-left locales: You can align by locale-specific text start or text end without needing to know if it's to the left or to the right.
textAlignment alone won't work. For many alignment modes, you need to actually combine it with a gravity.

setTextAlignment available from API Level 17, You can't use it before 17. But setGravity available from API Level 1. You can use it anywhere.
SetGravity
Sets the horizontal alignment of the text and the vertical gravity that will be used when there is extra space in the TextView beyond what is required for the text itself

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

Dynamic ellipsis support for Android autosizing TextViews

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.

What do margin values for a Toast do?

The Android Toast class provides methods to get and set margins. I'm pretty sure they refer to the outside margins for the whole toast message. Since Toast messages float over the UI, why exactly are these Margins necessary?
I tried looking over the SDK reference as well as searching the Internet. The closest thing to a solution I found was a one line suggestion that both margins and offsets allowed control over the positioning of a Toast. Why would I need two methods (albeit conceptually different, since margins allow specification in terms of percentage container width), to control the positioning of the Toast?
Just to be sure, these margins don't work like padding for other layouts does it? That would not make sense, but I'd like to be clear.
In sum, I want to know why margins are needed, what margins do, and the use-cases for margins versus offsets, that is, when should I use margins, when should I use offsets, and why?
Update:
I haven't managed to find any answers yet. I've tried using margins versus using offsets in code and found that they seem to offer two different paradigms of positioning the Toast. The design intent (why two methods), when I should use one method versus the other (or at least examples of when one was found more useful than the other by other programmers/UI designers), and even the exact operation (do margins "center" the toast inside them? are margins applied against the closest container edges?) of these methods remain unclear.
Update 2:
I looked at the docs closely, and also at some code for Toast.java that Google pointed me to. What became apparent is that the Toast is contained within a Window (Activity window?) and that it might be an overlay. The WindowManager.LayoutParams class has also provided further clues. I've decided to play a bit more with using Toasts, offsets and margins, as well as look at the code from the AOSP to get a clearer understanding.
I'll update here as I discover more.
I believe the margins determine how far the toast appears from the screen edge. You can also call setGravity() to change which side of the screen it appears on, and then use the margins to control how far away it is from the side of the screen. So for example:
myToast.setMargin(10, 20);
Will create a toast that has 10% of the containers width between the edge and the container, and 20% of the containers height between the toast and the containers edge
To create a toast that is in the top left corner of the container, with a 10 pixel margin on the left and 20 pixel margin from the top:
myToast.setGravity(Gravity.LEFT| Gravity.TOP, 10, 20)

AbsoluteLayout - do I have an alternative?

In an app I'm developing, I need to layout 2 to 5 buttons as if on the edge of a circle. The app starts with 5 buttons, but buttons gradually disappear (based on user input) until there are 2 of them.
I thought I would use an AbsoluteLayout control, and set the position of each button in code (taking into account the screen size). However, it says more or less everywhere that AbsoluteLayout should not be used. Since I'm targeting this app to Android 2.2 and up, I can't use the fancier layouts introduced with ICS.
I know I can use a RelativeLayout and play with the margins, but this seems less intuitive, and just as error prone, as using AbsoluteLayout.
Do I have any reasonable alternative?
I think, you dont have many alternatives. Except relative layout you mentioned, you could of course use FrameLayout and set left and bottom margin to position your buttons correctly.

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