Re: [android-developers] TextView maxLines based on height

2014-02-17 Thread Ankur Avlani
Hi,

I guess now i have understood my issue and can explain it much better and
clearly.

I am loading all my view dynamically in a Horizontal ViewGroup.  Inside one
view there a collection of pictures along with text and the bottom.  I have
the width and height of the view fixed (When creating a view i pass the
width and height to the layout params).

The image is loaded dynamically, till then a loading icon is display.  But
the text is already display in the TextView.  The visible lineCount that I
get before the image is different than the lineCount that I actually
displayed after the Image is loaded.  I want to revalidate the whole view
again after the image loading and displayed in the UI.

I tried sevaral methods to revalidate the TextView, the Layouts, but none
of it help me to get the correct visible line count after image is loaded.

-Ankur.


On Sat, Feb 15, 2014 at 5:27 PM, Ralph Bergmann | the4thFloor.eu 
ra...@the4thfloor.eu wrote:

 Am 13.02.14 03:54, schrieb Ankur Avlani:
  Can someone please suggest me some faster was to achieve my requirement.

 try this one:

 /**
  * Calculates the lines needed to show the text.
  *
  * @param paint
  *   the TextPaint from the TextView
  * @param text
  *   the text for the calculation
  * @param textAppearance
  *   text typeface, size, and style
  * @param avail
  *   the available width
  * @return the number of lines needed to show the text
  */
 public static int neededLines(final TextPaint paint, final String text,
 final TextAppearanceSpan textAppearance, final int avail) {

if (TextUtils.isEmpty(text)) {
   return 0;
}

final TextPaint textPaint = new TextPaint();
textPaint.set(paint);

textPaint.setTextSize(textAppearance.getTextSize());
textPaint.setTypeface(Typeface.create(textAppearance.getFamily(),
 textAppearance.getTextStyle()));

final StaticLayout layout = new StaticLayout(text, textPaint, avail,
 Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
final int lineCount = layout.getLineCount();

return lineCount;
 }



 Ralph



-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
Android Developers group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [android-developers] TextView maxLines based on height

2014-02-15 Thread Ralph Bergmann | the4thFloor.eu
Am 13.02.14 03:54, schrieb Ankur Avlani:
 Can someone please suggest me some faster was to achieve my requirement.

try this one:

/**
 * Calculates the lines needed to show the text.
 *
 * @param paint
 *   the TextPaint from the TextView
 * @param text
 *   the text for the calculation
 * @param textAppearance
 *   text typeface, size, and style
 * @param avail
 *   the available width
 * @return the number of lines needed to show the text
 */
public static int neededLines(final TextPaint paint, final String text,
final TextAppearanceSpan textAppearance, final int avail) {

   if (TextUtils.isEmpty(text)) {
  return 0;
   }

   final TextPaint textPaint = new TextPaint();
   textPaint.set(paint);

   textPaint.setTextSize(textAppearance.getTextSize());
   textPaint.setTypeface(Typeface.create(textAppearance.getFamily(),
textAppearance.getTextStyle()));

   final StaticLayout layout = new StaticLayout(text, textPaint, avail,
Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
   final int lineCount = layout.getLineCount();

   return lineCount;
}



Ralph



signature.asc
Description: OpenPGP digital signature


[android-developers] TextView maxLines based on height

2014-02-12 Thread Ankur Avlani
Hi All,

I am designing an native custom article view in android.  My requirement is
such that I have a long text which i need to break into pages.  I can
find out the total height of my TextView, how do i calculate the max
visible lines for the view based on the height?

I know Paint.breakText will do for me, but its very slow.  Please have a
look at the following code snippet:

public static int getTextLineCountForHeight(String text, int maxWidth,
float textSize, Typeface typeface, int maxHeight) {
TextPaint paint = new TextPaint(Paint.LINEAR_TEXT_FLAG |
Paint.SUBPIXEL_TEXT_FLAG);
paint.setTextSize(textSize);
paint.setTypeface(typeface);

int lineCount = 0;

int index = 0;
int length = text.length();
Rect bounds = new Rect();
bounds.setEmpty();
while(index  length - 1) {
bounds.setEmpty();
index += paint.breakText(text, index, length, true, maxWidth, null);
lineCount++;
paint.getTextBounds(Py, 0, 2, bounds);
if((int)Math.floor(lineCount * bounds.height())=maxHeight){
return lineCount;
}

}

return lineCount;
}

Can someone please suggest me some faster was to achieve my requirement.

Any help is highly appreciated.

-Ankur.

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
Android Developers group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.