0

I'm making a simple android app and I reached a problem I can't solve myself. I searched for a solution quite thoroughly but nothing that I have found seems to work.

In my Main Activity I have a ListView which stores data provided by the user.

I want to add a search function to this ListView. When I do it statically (add EditText in xml and connect with method addTextChangeListener) everything works fine. However it's not what I want to achieve.

I want the EditText to appear on screen (above the ListView) only if user clicks on the particular button (which in my app is an action bar item) and then connect it with search function in ListView. I tried this:

LinearLayout mainLayout;
EditText inputSearch;

@Override
protected void onCreate(Bundle savedInstance){

    (...)

    mainLayout = (LinearLayout)findViewById(R.layout.activity_main);

    inputSearch = new EditText(this);

    inputSearch.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

    inputSearch.setText("Search...");

    (...)

}

@Override
public boolean onOptionsItemSelected(MenuItem item){

    (...)

    case R.id.action_search:

    mainLayout.addView(inputSearch);

    (...)
}

Any ideas what am I doing wrong?

MarmiK
  • 5,639
  • 6
  • 40
  • 49
Michał
  • 655
  • 6
  • 19
  • Don't re-create something that's already been made for you. Use the [search widget](http://developer.android.com/guide/topics/search/search-dialog.html#UsingSearchWidget) which is accessible through the ActionBar, or a [search dialog](http://developer.android.com/guide/topics/search/search-dialog.html#SearchDialog). Also try to make your questions a little bit more specific – Simas Aug 14 '14 at 10:13
  • Ok, thanks, I'll try to use search widget :) – Michał Aug 14 '14 at 10:21

0 Answers0