user2852926 user2852926 - 16 days ago 5
Android Question

Bold & normal text in ListView items

I am looking to create a list with a format looking something like this:

Sensor 1

var1=4, var2=4




Sensor 2

var1=2, var2=-12




So I would like to have a bold header, and then some other info on a separate line in each item in the list. This is how I am currently adding things to my list.

View sensorView = inflater.inflate(R.layout.fragment_sensor, container, false);
ListView sensorList = (ListView)sensorView.findViewById(R.id.sensorList);
String [] sensors = {"Sensor2\nAlpha=2", "Sensor3\nAlpha=2"};
ListAdapter sensorListAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, sensors);
sensorList.setAdapter(sensorListAdapter);


I've tried using Html.fromhtml() but it didn't work. Any ideas?

Answer

You could create a custom adapter view to replace android.R.layout.simple_list_item_1 that has two TextView views, one for the header and one for the content. Then just override the ArrayAdapter class to set the text to each view.

Example: Custom Adapter for List View