I have code:
Text infoText = new Text(560, 10, mFont, "this text is too long to be in one line", activity.getVertexBufferObjectManager());
How I can set this text width(for example 200px). When text will be longer than this width it should break line and write more in next line.
I can' t find any example for this. Is it possible in Andengine gles 2 ?
I was looking around in AndEngine's code to find a solution for your problem, and I think I've found it. The method splitLines in the FontUtils class seems to be what you need.
It's arguments are: the the text you're splitting, the font to measure widths with (The font you'll be using for the text), an object that extends List<CharSequence> to save the new lines in, the type of split (AutoWrap.WORDS to split by words) and the maximum width per line.
Here's a link to the example from the AndEngine examples
https://github.com/nicolasgramlich/AndEngineExamples/blob/GLES2/src/org/andengine/examples/TextBreakExample.java
that should do it for you
Related
I'm using Android GraphView. I want to use DateAsXAxisLabelFormatter for x-axis, and a custom LabelFormatter for y-axis. Otherwise the y-axis labels gets cut off. See this: android graph-view y axis numbers being cut out
If I use static padding as suggested in the answers of above question, then there is extra padding sometimes.
See the images below:
When there are integers in y-labels (and I have given large padding to cope with numbers like: -0.0231, -0.0450, etc.):
When there are floating points in y-labels (and I have give small padding to make it look nice for numbers like: 10, 25, 40, etc):
And I'm unable to find the sweet spot.
If I could use a separate for both of the axes then I would set setMaximumFractionDigits(2), and provide a static padding. That remove my problem. But I couldn't do both, it seem to override the other (see the code for better understanding). I can either use DateAsXAxisLabelFormatter or a DefaultLabelFormatter but not both at the same time.
Here is the code:
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
nf.setMaximumIntegerDigits(2);
graphView.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(MainActivity.this, simpleDateFormat));
graphView.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter(nf, nf)); // This overrides the previous LabelFormatter
// If I try to set them both together. I get this error.
graphView.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(MainActivity.this, simpleDateFormat), nf);
//setLabelFormatter (LabelFormatter) in GridLabelRenderer cannot be applied to (DateAsXAxisLabelFormatter, java.text.NumberFormat)
How do I use both of the LabelFormatter separately for both axes?
I have a TextView, and i want to know, at runtime, if the text is to long.
The requirement that is making this tricky, and thus haven't found a solution to it, is that I DO NOT want to use ellipsize, because I do NOT want to show three dots at the end.
ideas?
thanks
to check on runtime you have to use a paint object.
doc ref: Paint.measureText(String) and TextView.getPaint()
// the code below must be run AFTER the TextView have been layout on the screen.
Paint p = textView.getPaint();
float width = p.measureText("your text here");
if(width > textView.getWidth()){
// bigger
}
TextUtils class also have some interesting methods that you might be interested, for example, the method that calculates the ellipsize: TextUtils.ellipsize(...)
I would like to draw vertical lines between Numbers/Letters in my TextView. So it should look similar to
A|B|C|D
without the use of the | character and use drawLine() instead.
I am trying to do it using the width of the TextView and assuming the centre of each character will find itself at , 1/8, 3/8, 5/8, 7/8 of the TextView width for this example. However the lines dont line up as they should.
Not sure whats not working, help appreciated.
I am trying to do it using the width of the TextView and assuming the centre of each character will find itself at , 1/8, 3/8, 5/8, 7/8 of the TextView width for this example.
That's your problem. For starters, you haven't specified that you're using a mono-spaced font. If you're not, then the letters won't be evenly distributed. Even if you are using a mono-spaced font, likely the padding at the beginning (and possibly end) of the TextView are going to offset things. I can't remember exactly how TextView measures things, but I suspect looking at actual left padding value would be a good start to find the left padding. If you want to use this with a variable width font, you'll want to use something like Paint.measureText to measure the width of the characters.
Once you have all that, you can add the width of the character(s) to the left padding to find the position to place each line.
I am working on Android textview,case is i have a specific height of textview,i want to adjust the text in the given textview height by adjusting the font size.If a text is of 10 charachers it will covers the whole height and if text is off 400 characters it will decreases the font to adjust in height of textview. I have tried auto text resize class but it does not getting the desire result.
You are looking for FontFit TextView. Check out here.
You just have to use it instaed of normal TextView like this..
TextView tv = new FontFitTextView(context);
Hope this helps
You can also write your own logic for that like
1- : Count no or character in side text watcher
2- : if no of character more then 10 then reduce the size of test using textsize method.
3- : the same logic if character more then 10 then reduce the size of test using textsize method.
or you can also put it on loop then i think you can achieve it also.
I am writing a reader under Android. Pages are separated into different View. How to calculate how many characters fit on the screen to correctly divide the text into different View? Maybe there is a simple method to do this?
The code for adding text to Views:
for (int i = 0; i < pages; i++) {
TextView textView = new TextView(getApplicationContext());
textView.setText(text);
realViewSwitcher.addView(textView);
}
you could probably make assumptions based on text size, maybe the default here would be 12dip. And then assuming that the average word length is 5 words. determining how many dips a 5 character words takes. and then taking the width and height and and divide it by that number.
Maybe Paint can help you. There is methods like "measureText", or "breakText". You can get a Paint object from a TextView with getPaint().
perhaps you could calculate the number of ems that fit in the textview somehow.
or perhaps you could create an algorithm that try's to fit the characters based on computerVerticalScrollRange(). Through trial and error, it could eventually find the perfect fit.