2

Is it possible to the list items in listview to be underlined when selected? I use custom layout (textview) for list item

This is what i want to achieve: enter image description here

 myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            Cursor cursor = (Cursor) myList.getItemAtPosition
                    (position);
            String title = cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_NAME));

            s.append(title);
           // Toast.makeText(getBaseContext(), s, Toast.LENGTH_LONG).show();

            TextView shopName = (TextView)findViewById(R.id.item_version);
            SpannableString content = new SpannableString(shopName.getText());
            content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
            shopName.setText(content);
        }
    });

1 Answers1

0

If you want to underline a text in a TextView, you could use the html tags <u> </u> as just changing the text of the selected textView like this for example:

String underlined = "<u>" + textView.getText() + "</u>";
textView.setText(Html.fromHtml(underlined));

Or do it "like a boss" :D :

SpannableString content = new SpannableString(textView.getText());
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
textView.setText(content);

Or another way:

textview.setPaintFlags(textview.getPaintFlags()|Paint.UNDERLINE_TEXT_FLAG);

In all cases textView is your TextView. I think that the second approach could be better than the other two.

Hopefully this helps you :)

Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30