I need to dynamically change Android text Gravity
to the TOP
in My ListItems.
Code used for ListItem:
XML layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="@dimen/ma_list_item_height">
..............
<TextView
android:id="@+id/driver_passage_text_view"
android:layout_width="0dp"
android:layout_height="@dimen/ma_list_item_height"
android:layout_weight="1"
android:gravity="center_horizontal|center_vertical"
android:text="Ivanov Ivan Ivanich"
android:padding="0"
/>
I have read about Gravity
and property setIncludeFontPadding
, I have tried to use it but it is not working in my case, Code used :
private void setGravityAndText(TextView textView,String text){
if(text.length()>24){
textView.setIncludeFontPadding(true);
textView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
}
else {
textView.setIncludeFontPadding(false);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
}
textView.setText(text);
}
Horizontal alignment is working successfully, but on the top anyway doesn't work? What did I do wrong?
Please Help.
Thanks in Advance.