Android Question Get text height of RichString

edgeryder1000

Member
Licensed User
Longtime User
I have a label which displays a RichString using agraham's library. StrUtil.MeasureMultilineTextHeight is not giving the correct height. Is there a solution using reflection or some other method?
 

edgeryder1000

Member
Licensed User
Longtime User
B4X:
        rs.Initialize(File.ReadString(File.DirAssets, CurrentBook & i    & ".txt")) 
        rs = rsf.Format(rs)

        Label1.Text = rs
 
        Panel1.AddView(Text, 20dip, 10dip, 100%x - 40dip, 10000)
        ScrollView1.Panel.Height = SU.MeasureMultilineTextHeight(Text, rs) + 20dip
        Text.RemoveView
        ScrollView1.Panel.AddView(Text, 20dip, 10dip, 100%x - 40dip, Panel1.Panel.Height)

If I remove rs = rsf.Format(rs) then it gives the correct height
 

Attachments

  • Screenshot_2013-07-31-11-21-00.png
    221.9 KB · Views: 323
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this inline Java code:
B4X:
#if JAVA

import android.text.StaticLayout;
import android.text.Layout.Alignment;
import android.widget.TextView;
public int MeasureMultilineTextHeight(TextView Label) {
    StaticLayout sl = new StaticLayout(Label.getText(), Label.getPaint(),
          Label.getLayoutParams().width - Label.getPaddingLeft() - Label.getPaddingRight(),
    Alignment.ALIGN_NORMAL, 1, 0 , true);
    return sl.getLineTop(sl.getLineCount());
    }
#end if
 
Upvote 0

AscySoft

Active Member
Licensed User
Longtime User

Yes, it does seems to propperly work.
I tested it on my 4.2.2 device it does calculate a heigher value than stringutils method.
I further need to test it on 2.3 devices for propper feedback.
May I suggest you one thing?
Why don't you update the stringutils method with above code?
Second: If i place this code inside a module how shoud I make a call from an activity (I don't know if this is possible)?

Forgot: a big thanks 2 you Erel!
 
Upvote 0

AscySoft

Active Member
Licensed User
Longtime User
That simple? In my case i needed to change the
B4X:
public int MeasureMultilineTextHeight(TextView Label) {
with
B4X:
 public static int MeasureMultilineTextHeight(TextView Label) {
and then create propper call with array parameter. I'm new to java I know
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…