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?