1

I do not understand how I can highlight the search results.

I tried to do it with the help of SpannableString like here but I do not know how to implement it in my case. Write error in line mTextView.setText(spannable)

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);

    // Listview Data
    final String[] products = new String[] {
        getResources().getString(R.string.t1_2),
        getResources().getString(R.string.t1_3),
        getResources().getString(R.string.t1_4),
    };

    lv = (ListView) findViewById(R.id.list_view);
    inputSearch = (EditText) findViewById(R.id.inputSearch);
    mTextView = (TextView) findViewById(R.id.product_name);

    // Adding items to listview
    adapter = new ArrayAdapter<String>(this, R.layout.list_item,R.id.product_name, products);
    lv.setAdapter(adapter);
    lv.setVisibility(View.GONE);

    // Once user enters a new data in EditText we need to get the text from
    // it and passing it to array adapter filter.
    inputSearch.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
    // When user changed the Text

        Search.this.adapter.getFilter().filter(cs);

        textlength = inputSearch.getText().length();
        // adapter.clear();
        if (textlength != 0) {
            lv.setVisibility(View.VISIBLE);
            for (int i = 0; i < products.length; i++) {
                if (textlength <= products[i].length()) {
                    Spannable spannable = new SpannableString(inputSearch.getText().toString());
                    ColorStateList blueColor = new ColorStateList(new int[][] { new int[] {}}, new int[] { Color.BLUE });
                    TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, blueColor, null);
                    spannable.setSpan(highlightSpan, 0,5 , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    mTextView.setText(spannable);
                }
            }
        }
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable arg0) {
    // TODO Auto-generated method stub

    }
});
}
}
Community
  • 1
  • 1
Igor Demo
  • 11
  • 3
  • What has mTextView to do with your listview? And what sense does it make to set its text three times with a new spannable? Which write error exactly? – greenapps Nov 12 '14 at 19:51
  • Please reread your link and realise that as stated there you should set the text in another style in getView(). – greenapps Nov 12 '14 at 19:54
  • You should also realise that is you want to highlight search results that you should not first filter them out. – greenapps Nov 12 '14 at 19:55
  • @greenapps mTextView it's my Layout file for ListView there is only TextView id=product_name...What do I need? create new Adapter and in getView() write Spannable? – Igor Demo Nov 12 '14 at 20:18

0 Answers0