I have a TextView that i inflate in my activity. When i set text to this TextView its content is spread in multiple lines as this is the default behaviour.
e.g
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location_details);
// inflate the view
descriptionTextView = (TextView)findViewById(R.id.descriptionTextView );
descriptionTextView.setText("This is a long text that should be multilined");
....
}
when i use the same text inside a Runnable in a runOnUiThread the text goes single line by default. I have to call setSingleLine(false) to be multilined …
e.g
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location_details);
// inflate the view
descriptionTextView = (TextView)findViewById(R.id.descriptionTextView );
new Thread(new Runnable(){
public void run() {
...
runOnUiThread(new Runnable(){
@Override
public void run() {
descriptionTextView.setText("This is a long text that should be multilined");
}
});
});
}
Why is this happening ?
EDIT
My layout file is like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="6dp"
android:gravity="center|top"
android:background="@drawable/background">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="@drawable/bg_dashboard"
android:gravity="center|top"
android:orientation="vertical"
android:padding="10dp" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_gravity="center"
android:layout_weight="1">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:gravity="top"
android:orientation="vertical" >
<TextView
android:id="@+id/titleTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textStyle="bold"
android:textColor="#565A5D"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:textSize="24sp" />
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/divider"
android:contentDescription="@string/metaDivider"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:paddingBottom="4dp"
android:paddingTop="4dp" />
<ImageView
android:id="@+id/photoImageView"
android:layout_width="fill_parent"
android:adjustViewBounds="true"
android:layout_height="160dp"
android:padding="5dp"
android:src="@null"
android:contentDescription="@string/metaArticleImage" />
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageDivider"
android:src="@drawable/divider"
android:contentDescription="@string/metaDivider"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:paddingBottom="4dp"
android:paddingTop="4dp" />
<TextView
android:id="@+id/descriptionTextView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:textColor="#55595C"
android:layout_weight="1"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"/>
<Button
android:id="@+id/mapButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/mapBtn"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_gravity="center" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<ImageView
android:contentDescription="@string/metaLogo"
android:layout_gravity="right"
android:layout_marginRight="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo" />
Most likely it has to do with your layout constraints. The reason you are seeing a difference is because in one case (called directly from
onCreate()) the text is set before the views are laid out, so theTextViewheight is set to match the content inside. In the second case (from theThread), the text is set after layout is complete, andTextViewdoes not adjust its size to match new content unless itsLayoutParamsexplicitly tell it to do so.If you set the
layout_heightattribute of yourdescriptionTextViewtowrap_content, the resizing problem will go away.HTH