5

I have to set some predefined values to MultiAutoCompleteTextView (before I select from suggestion drop down list). It's like, as if you selected 2-3 items from drop-down list. After setting these predefined values, again I want to continue with normal process of filtering from the list and add it to MultiAutoCompleteTextView.

It would look like this

Predefined1, Predefined2, Predefined3,  

If I do setText(...) as you do with TextView or EditText, with some comma separated values, its considering it as 1 whole word and adding a comma at the end.

Please suggest how can I implement it.

Edit :

Okay. Let me explain in detail. I am trying to achieve like in this and this. But, 1 extra requirement from this is that, along with selecting value from suggestion drop down, I also need to set some predefined values when View is displayed and when I write something and press enter/hide keyboard, whatever i wrote should be converted into button text. If it's not possible, any other alternative? Please suggest.

Edit :

enter image description here

These values should be there in view without selecting from drop down. Later, whichever users add from suggestion drop down, those also should be added to list.

It's like setting hint or some text to EditText before user starts typing.

Thanks.

Community
  • 1
  • 1
Braj
  • 2,164
  • 2
  • 26
  • 44
  • 2
    Please don't make my 50 reputation go waste consecutively for 2nd time :( – Braj Sep 02 '13 at 07:38
  • So, to make sure I understand correctly, you need the drop down Auto complete List view to be initialized with certain values and any other value the user types to be added as well? – Mohamed_AbdAllah Sep 04 '13 at 14:12
  • Along with this, those predefined values need to be in MultiAutoCompleteTextView when initialized. Attached a sample image. Please have a look at it – Braj Sep 05 '13 at 06:10

1 Answers1

7

Try the following:

   ArrayList<String> data=new ArrayList<String>();
    data.add("Predefined1");
    data.add("Predefined2");
    data.add("Predefined3");

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                     android.R.layout.simple_dropdown_item_1line, data.toArray());
             MultiAutoCompleteTextView textView = (MultiAutoCompleteTextView) findViewById(/*View id*/);

MultiAutoCompleteTextView.CommaTokenizer tokenizer=new MultiAutoCompleteTextView.CommaTokenizer();

             textView.setAdapter(adapter);
             textView.setTokenizer(tokenizer);

    textView.setOnEditorActionListener(new TextView.OnEditorActionListener(){
     @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                data.add(v.getText().toString());
                adapter.notifyDataSetChanged();
                return true;    
            }
            return false;
        }

    });

textView.setText("Predefined1,");
tokenizer.terminateToken("Predefined1");


textView.setText(textView.getText().toString()+"Predefined2,");
tokenizer.terminateToken("Predefined2");

textView.setText(textView.getText().toString()+"Predefined3,");
tokenizer.terminateToken("Predefined3");
Mohamed_AbdAllah
  • 5,311
  • 3
  • 28
  • 47
  • I have edited my question. Please have a look at it. – Braj Sep 05 '13 at 06:50
  • Try the modified code. – Mohamed_AbdAllah Sep 05 '13 at 08:13
  • Edited solution worked!. Its been a long time i struck in this. Thanks a lot. Some modification are there. I will take care of them. Here u take bounty :) – Braj Sep 05 '13 at 09:07
  • Hi Mohamed, setting text is working fine. I have 1 more issue. As in that link, wrapping that text with border n cross button is not working while adding predefined text. If u could hv a look, it would b very helpful for me – Braj Oct 29 '13 at 13:24
  • how to add the clicklistener to the (x) image in the textview to delete the text. – Nambi Nov 30 '13 at 13:54