simPod
simPod

Reputation: 13456

Android Listview format text

is it possible to format an item text in the Listview so it would look for example like this:

part of string is bold and part is not

Thanks

Upvotes: 0

Views: 2299

Answers (2)

Yashwanth Kumar
Yashwanth Kumar

Reputation: 29121

Here is a method to format your strings..

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="part_bold"><b>%1$s</b> %2$s</string>
</resources>

Resources res = getResources();
String part_bold_text = String.format(res.getString(R.string.part_bold), bold_string, normal_string);

the string in the xml has 2 arguments, one is bold , the other is normal. use this to get a string(part_bold_text) of part bold and a part normal.

HTH.

Upvotes: 1

Kurtis Nusbaum
Kurtis Nusbaum

Reputation: 30825

Yea, you'd just have to have two different textviews, one for the bold text, and one for the unbolded text. So you'd have a layout for each row that looks 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="horizontal"
>
   <TextView android:id="@+id/bold_field"
     android:layout_width="wrap_content"
     android:layout_height="fill_parent"
     android:textStyle="bold"
   />
   <TextView android:id="@+id/non_bold_field"
     android:layout_width="wrap_content"
     android:layout_height="fill_parent"
   />
</LinearLayout>

Then you'd use a SimpleAdapter to map the appropriate pieces of the string to each TextView. If you want the string to appear seamless, you'll probably have to mess around with the gravity of the TextViews.

Upvotes: 2

Related Questions