how many string can display on full screen - java

I've large string, i want to split it. i got screen width and height using below code,
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenHeight = metrics.heightPixels;
screenWidth = metrics.widthPixels;
I want to know how many character to display on screen.
how to calculate ? and split the string.?

On a Swing / AWT Java platform, you could use a FontMetrics object to measure the width of the particular characters you are trying to display.
References:
How to calculate the font's width?
But it would probably be simpler to use something that can take care of the character rendering and wrapping for you.
On the Android platform, the Paint class has a number of methods that will help you do this kind of thing.

i think if you set width property to wrapcontent than string automatically split and display on next line.
use this property
android:layout_width="wrap_content"
android:layout_height="wrap_content"

use the substring method
String substring(int startIndex, int endIndex)
split the strings per line and store them in an array of Strings
String originalString=" ....some long text " ;
String stringsByLine[]= new String(screenHeight);
int i,j=0;
for ( i = 0; j<originalString.length;i++,j++){
stringsByLine[i]=originalString.substring(j,j+screenWidth);
j+=screenWidth;
}
havent tried it myself, but this logic should work. :-)

Related

How to figure out the width and height of a written bitmap font string

Okay, so I have this.
calibri.draw(FBLA_Game.spriteBatch, "Rooftop Defender",0 ,0 );
Anyways, this code... I need the text to be centered. Instead of the 0's, I would have it like this:
calibri.draw(FBLA_Game.spriteBatch, "Rooftop Defender",Gdx.graphics.getWidth()/2 ,Gdx.graphics.getHeight()/2 );
But this isn't what I want. this centers the beginning of where it writes the words. So then for the width I would need to subtract the width of the word being written, But I cannot figure out how to get the width of "Rooftop Defender" that is being written.
Thanks for any and all help,
Alex
In new versions (>1.6) of LibGDX BitmapFont is being handled with GlyphLayout class which you can use to calculate the future width of your rendered string
//out of render() method! this call is expensive
GlyphLayout glyphLayout = new GlyphLayout(calibri, "Rooftop Defender");
...
//render()
float glyphLayoutWidth = glyphLayout.width;

How do i set text size in a text view programmatically according to the width of a text view.?

The text view i am using in the application is supposed to be single line and has a predefined width which may contain text fetched from database ranging from single word to maximum 3 words. I found a link in stack overflow for adjusting the text size according to height of the text view but i am looking for something more particular to width of text view.
This will work, but you might find faster ways posted. The incrementation in the loop does cost some time, so use sparingly in a single layout.
Display display = this.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x; //Get screen width. There are other ways to do this too.
int tSize = 10;
tv.setTextSize(tSize); //tv is your TextView
while (tv.getPaint().measureText((String) tv.getText())
< width){
tSize++;
tv.setTextSize(tSize); //Increment up till screen width is filled.
}

How to calculate the height of an element?

I am generating pdf files by XML data.
I calculate the height of a paragraph element as :
float paraWidth = 0.0f;
for (Object o : el.getChunks()) {
paraWidth += ((Chunk) o).getWidthPoint();
}
float paraHeight = paraWidth/PageSize.A4.getWidth();
But this method does not works correctly.
Can you give me an idea?
Your question is strange. According to the header of your question, you want to know the height of a string, but your code shows that you are asking for the width of a String.
Please take a look at the FoobarFilmFestival example.
If bf is a BaseFont instance, then you can use:
float ascent = bf.getAscentPoint("Some String", 12);
float descent = bf.getDescentPoint("Some String", 12);
This will return the height above the baseline and the height below the baseline, when we use a font size of 12. As you probably know, the font size is an indication of the average height. It's not the actual height. It's just a number we work with.
The total height will be:
float height = ascent - descent;
Or maybe you want to know the number of lines taken by a Paragraph and multiply that with the leading. In that case, there are different possibilities. As it's not clear from your question what you want (height of chunks, width of chunks, vertical position of the baseline,...), you won't get any better answers than the ones that are already given. Please rephrase your question if the height of the glyphs in a Chunk wasn't what you expected.
Firstly why you iterating over Chunk collection casted to Object ? If all elements of this collection are Chunk, use this:
for (Chunk c : el.getChunks()) {
paraWidth += c.getWidthPoint();
}
What do you mean saying method does not works correctly ?

Figure out width of a String in a certain Font

Is there any way to figure out how many pixels wide a certain String in a certain Font is?
In my Activity, there are dynamic Strings put on a Button. Sometimes, the String is too long and it's divided on two lines, what makes the Button look ugly. However, as I don't use a sort of a console Font, the single char-widths may vary. So it's not a help writing something like
String test = "someString";
if(someString.length()>/*someValue*/){
// decrement Font size
}
because an "mmmmmmmm" is wider than "iiiiiiii".
Alternatively, is there a way in Android to fit a certain String on a single line, so the system "scales" the Font size automatically?
EDIT:
since the answer from wsanville was really nice, here's my code setting the font size dynamically:
private void setupButton(){
Button button = new Button();
button.setText(getButtonText()); // getButtonText() is a custom method which returns me a certain String
Paint paint = button.getPaint();
float t = 0;
if(paint.measureText(button.getText().toString())>323.0){ //323.0 is the max width fitting in the button
t = getAppropriateTextSize(button);
button.setTextSize(t);
}
}
private float getAppropriateTextSize(Button button){
float textSize = 0;
Paint paint = button.getPaint();
textSize = paint.getTextSize();
while(paint.measureText(button.getText().toString())>323.0){
textSize -= 0.25;
button.setTextSize(textSize);
}
return textSize;
}
You should be able to use Paint.setTypeface() and then Paint.measureText(). You'll find other methods on the Paint class like setTextSize() to help too.
Your followup question about scaling text was addressed in this question.

Canvas size - text rendering

Is there any way how to determine the optimal canvas size for text rendering?
The input is a string with newlines, I want to contruct the canvas to fit (no insets) while using both font types - proportional and non-proportional, these types will be never mixed.
Thanks.
From the Java Tutorial Measuring Text
FontMetrics metrics = graphics.getFontMetrics(font);
int hgt = metrics.getHeight();
int adv = metrics.stringWidth(text);
Dimension size = new Dimension(adv+2, hgt+2);
You probably need to do this line by line of your text and detect whether your font changes between lines.

Categories