0

Is it possible to use the methods defined in the EditText class to draw this widget on the action bar? I've tried looking at the android docs as well as other similar questions but all of them seem to be inflating using an instance of MenuInflater which I don't have access to in my function. This is for open source development if it matters.

KPERI
  • 233
  • 3
  • 9
  • Have you looked at searchview http://developer.android.com/training/search/setup.html – Raghunandan Aug 27 '14 at 17:19
  • Correct me if I am wrong but isn't searchview only used for entering a query which gets sent to a search provider (like google.com)? If so, this isn't what I want. I wanted a text field that a user can edit with the keyboard and this information is sent to my app, not to a search provider. Furthermore, it seems that SearchView still requires a MenuInflater and an xml resource to display.. – KPERI Aug 27 '14 at 17:29
  • you could try customview fro actionbar http://stackoverflow.com/questions/12883732/how-to-display-custom-view-in-actionbar. You can also use searchview to filter list items http://stackoverflow.com/questions/14038331/android-searchview-filter-listview – Raghunandan Aug 27 '14 at 17:33
  • **but all of them seem to be inflating using an instance of MenuInflater which I don't have access to in my function** what do you mean by this? What function are you talking about. pls post your code also – Raghunandan Aug 27 '14 at 17:36
  • It is a little hard to post my code since it is quite large but I'll try an describe it as best I can. Also, in my previous comment, I meant to say class instead of function so sorry about that. So in the link in the first comment that you made, there are the following two lines in the onCreateOptionsMenu method which inflate the xml resource: MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.options_menu, menu); – KPERI Aug 27 '14 at 17:52
  • getMenuInflater() is a method in the Activity class and my class is in its own file that does not contain any instance of Activity so I cannot call the getMenuInflater method. The only thing that my class has is an empty Menu object which I am populating with MenuItem objects such as a next and previous button and I now want to add a text field that is editable. – KPERI Aug 27 '14 at 17:52
  • From what I understand, there are ways to get an instance of EditText to display itself on the action bar without using a menuinflater and instead using some of the methods defined in the EditText class but I am not sure how to do this. Hopefully this makes a bit more sense... – KPERI Aug 27 '14 at 17:53
  • No i am not sure what you meant. Any way good luck – Raghunandan Aug 27 '14 at 17:54

1 Answers1

2

In your onCreateOptionsMenu you can create a SearchView and add it to the menu like this:

    SearchView searchView = new SearchView(mActivity.getSupportActionBar().getThemedContext());
    searchView.setQueryHint(getText(R.string.query_hint));
    searchView.setOnQueryTextListener(new OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String text) {
            doSomething(text)
            return true;
        }
    });
    menu.add(R.string.action_search)
   .setIcon(R.drawable.ic_search)
   .setActionView(searchView)
   .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

I hope this help you ;)

MartinCR
  • 658
  • 6
  • 7
  • Thank you for this! This does indeed look like what I needed! I'll try it when I get home tonight and I'll mark this as correct if it works out :) – KPERI Aug 27 '14 at 18:05
  • So this seems to be working but the text field is kind of small. How can I make it bigger so that it takes up as much space as is available in the action bar? – KPERI Aug 28 '14 at 04:36