0

enter image description here

I have a listview. I want to differentiate title from descitption. I want to display title one color and discription is other colore. How can i do this? I have following code for create listview.

Activity:

public class Lisearch extends Activity {
    private ListView lv;
    private EditText et;
    private String listview_array[] = {"ONE:\n one is the first letter"};

private ArrayList<String> array_sort= new ArrayList<String>();
    int textlength=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lisearch);
        TextView tv=(TextView)findViewById(R.id.text);
        lv = (ListView) findViewById(R.id.ListView01);
        et = (EditText) findViewById(R.id.EditText01);
        //lv.setAdapter(new ArrayAdapter<String>(this,
        //R.layout.activity_lisearch, listview_array));

        et.addTextChangedListener(new TextWatcher()
        {
        public void afterTextChanged(Editable s)
        {
                                                                        // Abstract Method of TextWatcher Interface.
        }
        public void beforeTextChanged(CharSequence s,
        int start, int count, int after)
        {
        // Abstract Method of TextWatcher Interface.
        }
        public void onTextChanged(CharSequence s,
        int start, int before, int count)
        {
        textlength = et.getText().length();
        array_sort.clear();
        for (int i = 0; i < listview_array.length; i++)
        {
        if (textlength <= listview_array[i].length())
        {
        if(et.getText().toString().equalsIgnoreCase(
        (String)
        listview_array[i].subSequence(0,
        textlength)))
        {
                    array_sort.add(listview_array[i]);
                   }
                   }
        }
        lv.setAdapter(new ArrayAdapter<String>
        (Lisearch.this,
        R.layout.list,R.id.text, array_sort));

search.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
                android:layout_width="fill_parent"
android:layout_height="fill_parent"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:background="@drawable/aa"                
                >
<EditText android:id="@+id/EditText01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="Search"
android:padding="5dp">
</EditText>

<ListView android:id="@+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
     android:listSelector="@drawable/list_selector"
     android:layout_weight="1"
     >
</ListView>
</LinearLayout>

list.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="6dp"
    android:textSize="15sp"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:textColor="#FF4444"
    android:textStyle="bold"
    android:layout_marginLeft="10dp"


/>
Ram
  • 1,687
  • 3
  • 18
  • 28
  • use custom adapter, refer this link http://stackoverflow.com/questions/7361135/how-to-change-color-and-font-on-listview – rajeshwaran Sep 25 '12 at 10:20

2 Answers2

0

Use separate textview for title and description in list.xml .And use desired color for text.

public class CustomAdapter extends BaseAdapter {

    private Context mContext;
        String[] array =null;


    public CustomAdapter(Context c,String[] array) {
        this.mContext = c;
        this.array= array;

    }


    public int getCount() {
        return count;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.list, null);

        TextView textView1 = (TextView) view.findViewById(R.id.textView1);
                TextView textView2 = (TextView) view.findViewById(R.id.textView2);

        return view;
    }


}

Use this adapter for your listview.

Asha Soman
  • 302
  • 2
  • 6
  • 18
0

In your list.xml, Have 2 TextViews, one for title and one for description, Something like one below

<TextView android:id="@+id/title" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="@string/titleText"
    android:textSize="25dp" />
<TextView android:id="@+id/description"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:textSize="15dp"
    android:layout_below="@id/title" android:textColor="#00FF00" />

This way you can have seperate colors, add your adapter as follows

SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.list,new String[] {"Title","Description"},new int[] {R.id.title,R.id.description});
setListAdapter(adapter);
Royston Pinto
  • 6,681
  • 2
  • 28
  • 46
  • One ListView array should suffice. – Royston Pinto Sep 25 '12 at 10:24
  • Error this line: lv.setAdapter(new ArrayAdapter (Lisearch.this,R.layout.list,new String[] {"text","text1"},new int[] {R.id.text,R.id.text1}, array_sort)); – Ram Sep 25 '12 at 10:41
  • This piece of code works for me, i am extending ListActivity however. Also u need to pass a List as second argument, otherwise you will get an exception – Royston Pinto Sep 25 '12 at 10:50
  • This is my code to ennumerate List, final PackageManager pm = getPackageManager(); List packages = pm.getInstalledApplications(PackageManager.GET_META_DATA); for (ApplicationInfo packageInfo : packages) { for (workloadNumber = 0; workloadNumber < fileList.length; workloadNumber++) { if (packageInfo.packageName.compareToIgnoreCase(packageNames[workloadNumber]) == 0) { workload[workloadNumber].put("Status", "Installed"); } } } – Royston Pinto Sep 25 '12 at 10:56