1

I am setting custom trur type font of my TextView and set text size; then using setText() method i set text to the TextView. It shows perfect text. But when i try to get baseline of this TextView it shows 1 or -1. Why is it??

I am doing like this;

TextView tv;
tv.setTypeface(Typeface.createFromAsset(getAssets(),"customFont.ttf"));
tv.setTextSize(20);
tv.setText("My name is Tahir");

tv.getBaseline(); 
//this returns -1. (if i set Height and width of tv suppose 100 the getBaseline() retruns 1)
tv.getTop();
tv.getBottom();

Please help me what may be the problem???

M Tahir
  • 11
  • 1
  • 4

2 Answers2

0

Try obtaining the baseline after layout and rendering has already happened. That is definitely after onCreate(...), for instance.

Zsolt Safrany
  • 13,290
  • 6
  • 50
  • 62
0

Have you declare android:baselineAligned in your Linear or Relative layout whatever you used in your xml.

for example :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:baselineAligned="true" >

    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        android:id="@+id/text1" />

</LinearLayout>
Ajay
  • 4,850
  • 2
  • 32
  • 44
  • Yes sir I have already done it as android:baselineAligned="true". if i android:layout_width="100dp" and android:layout_height="100dp" then tv.getBaseLine() returns 1. Please state that what else i should consider? – M Tahir Feb 24 '12 at 10:18