Android TextView and how to determine the number of visible characters - android

How can I determine the number of visible characters that a TextView can display. For example if I change the orientation this number may change. If I change the resolution then also the number of visible characters changes.
Thanks in advance
Thank you for your answer.
Currently I am developing a small text based game to become acquainted with the Android API. For that reason I need to know exactly how much characters can be displayed in the visible area of a TextView widget. I saw an example of Paint but wanted to know if there are better solutions.

Ideally, you design your GUI such that it does not matter. For example, you can use android:ellipsize to deal with strings that are too long for the available space.
There are classes in the 2D drawing APIs (e.g., Paint) that seem to be tied into this, but it does not look like much fun.

You can use ellipsize property but there has been a bug that has been filed on the same
http://code.google.com/p/android/issues/detail?id=2254
On the bottom of this page you could find an alternate approach which can draw exactly the number of lines on a given space...

Related

Suggestions on approaches on achieving a specific layout on Android

I am trying to replicate the below layout (Boxes with Text and a line going outwards on a dedicated section) on Android
As evident, these boxes can be implemented as individual views (or drawable) to have the shape as
.
It is apparent that these boxes need to have some flexibility for the "wires" going out of them, most importantly deciding the "turning" point of the line. I have thought of a few approaches to achieve this:
Achieve the entire layout just by using image drawable and positioning the text boxes at exact places
Implement this with a dedicated view to have full flexibility of positioning the text boxes at any position and be compatible with all screen sizes.
I am inclined towards trying #2, but can't get my head around where to start. At first, I am not able to decide on whether I should be using a ViewGroup as the base class and add a TextView and a plain view as a child or should I be using a single View to implement this? The second thing I am concerned about, is whether I am overthinking it and there is an easy way to achieve the same thing (Just to save time, nothing else)?
Any help/guiding material is deeply appreciated. Thanks in advance.
I think that approach #2 will be better longer term. Because of the nature of the image, you will have to maintain the aspect ratio; otherwise, the person is stretched. Because you are maintaining the aspect ratio (at least the person-part), the placement of each text box and end point can be expressed as a percentage distance from an edge or the center lines.
Assuming the image you show is the entire image, the belly end point can be set at, say, 45% of of distance from the left edge and, also let's say, 42% of the distance from the top. The text boxes can be placed likewise. Once the text boxes and end point are place, the lines simply connect them. Now the image can stretch to any size to support multiple screen sizes and, as long as the aspect ratio is respected, and look good.
Take a look at ConstraintLayout and its percentage guidelines and barriers. There is also some radial placement which may help. You may still have to support the layout with a little code, but ConstraintLayout should be able to get you 95% of the way to a solution.
Edit: I meant to mention biases as well which may be the most helpful to you. Here is an example of using biases for a checkerboard solution that may be useful.

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.

Changing text's size according to TextView

Hi guys I've wondered if I could resize the TextView's text in order to make it fit withing the text view.
I want the text view to have a default text size, but in case the text is too long and requires more text than the TextView could offer, the text size should get smaller.
I've seen this answer which is awesome, but it's also from 2013, so I've wondered if there's a simpler way of doing this. Android keeps changing and evolving so I just wanted to make sure there's no easier solution first.
You can use AutoFitTextView for better result .
A TextView that automatically fit its font and line count based on its
available size and content.
There is no built-in way of achieving this in Android, so you'll have to resort to 3rd party classes and libraries to fix your problem (Like the one you linked)
I have tried a few different classes i found from googling around about 6 months ago, but i never found a 100% working solution, there were always some little kinks or flaws in my experience.

Android TextView text splitting system

I'm going to make an app for which I need to know how does android split the text in it's TextView.Because of the splitting the text doesn't go out of the window it retains in side a bound.
The help what I need is like the image
In the image the text didn't overlap with the triangle. So I need a sample code spinnet which will help me to do that. Or idea to how to do it will also help me.
One of the easiest way, I think - split one TextView on several ones (for example - 3, or more if you need precise approximation).

Letter Spacing in EditText for Android

I am trying to have a custom EditText based on the background that i am using for. The Background image has some spaces between the entry areas so i need to have some space between the characters(kerning) to fit them right in. So for example after every character the user enters, i need to put 4 whitespace after that.
I couldn't find any solution for this on the net so far, some people suggested TextWatcher, but i couldn't manage to make it work as i want it too.
Can someone help me about it?
Thanks
I have you considered using a custom font? Some font types are made to stretch out or shrink or have empty spaces. With so many different fonts available online, you can definitely find something. You can also make your own with a software. It might be time consuming if you start the lettering from scratch. I'm not 100% sure if it'll fit exactly to your background, but it's idea that you can consider.
If it doesn't fit, I supposed you can always customized the background to fix your font too. Here's the code for those who might want to use custom fonts in their app too.
Typeface myfont = Typeface.createFromAsset(getAssets(),
"fonts/Blocks2.ttf");
myeditText.setTypeface(myfont);
The font is in the asset folder under another folder called fonts.
This question is related to How to change letter spacing in a Textview?
As shown at this issue: android format edittext to display spaces after every 4 characters a solution might be to insert spaces with Java code...
You need to use TextWatcher to achieve visual purpose spaces.
And use any simply split string by space logic to join it back or loop
through the entire string per character wise and eliminate (char) 32
from the string
As far as i know actual character spacing is not possible, though i'd love to use that myself as well.
Another option might be to use a custom font with the character spacing included.

Categories

Resources