Set search hint dynamically

后端 未结 7 1442
悲哀的现实
悲哀的现实 2021-02-08 04:38

Does anybody knows how to set android search dialog hint dynamically? T have try to do something like:




        
相关标签:
7条回答
  • 2021-02-08 04:39

    I managed to do this with ABS using the OnActionExpandListener and a custom actionView, e.g.:

    menu.add("Search")
    .setActionView(R.layout.collapsible_edittext)
    .setOnActionExpandListener(new MenuItem.OnActionExpandListener()
    {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            ((EditText) item.getActionView()).setHint("Your custom text here");
            return true;
        }
    
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            return true;
        }
    });
    

    with collapsible_edittext.xml:

    <EditText
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/search_fg"
        android:background="@drawable/textfield_default_holo_light"
        android:hint="[will be replaced at runtime]"/>
    
    0 讨论(0)
  • 2021-02-08 04:41

    There is a much simpler answer than any of the above. It loads the default searchable.xml for your activity, and then uses java reflection to update the private mHintId field inside the SearchableInfo instance:

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        SearchManager searchManager = (SearchManager)getActivity().getSystemService(Context.SEARCH_SERVICE);
        SearchableInfo si = searchManager.getSearchableInfo( getActivity().getComponentName() );
    
        try {
            Field mHintId = si.getClass().getDeclaredField("mHintId");
            mHintId.setAccessible(true);
            mHintId.setInt(si, R.string.your_custom_hint);
        } catch (Exception e) {
        }
    
        MenuItem mi = menu.findItem(R.id.menu_search);
        SearchView searchView = (SearchView)mi.getActionView();
        searchView.setSearchableInfo( si );
    }
    
    0 讨论(0)
  • 2021-02-08 04:41

    Thanks Mister Murakami for the idea, i have implemented the same concept to perform toggle search and as per my view its not a hack but its beauty of OOPs. Below program having a toggle icon in the action bar to switch between search hint.

    1. RestaurantListingActivity.java

      public class RestaurantListingActivity extends BaseMainActivity implements
      TabListener {
      private boolean isRestaurantSearch = true;
      private SearchManager searchManager;
      private SearchView searchView;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          ActionBar aBar = getActionBar();
          aBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
          aBar.addTab(aBar.newTab().setText("Place Order").setTabListener(this));
          aBar.addTab(aBar.newTab().setText("My Account").setTabListener(this));
          aBar.addTab(aBar.newTab().setText("Favorite").setTabListener(this));
          aBar.addTab(aBar.newTab().setText("Vendor Portal").setTabListener(this));
      }
      
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.restaturant_listing_menu, menu);
          searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
          searchView = (SearchView) menu.findItem(R.id.rlm_search)
                  .getActionView();
          searchView.setSearchableInfo(searchManager
                  .getSearchableInfo(new ComponentName(this,
                          RestaurantListingActivity.class)));
          return super.onCreateOptionsMenu(menu);
      }
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
          case R.id.rlm_toggle:
      isRestaurantSearch = !isRestaurantSearch;
          if (isRestaurantSearch)
              searchView.setSearchableInfo(searchManager
                      .getSearchableInfo(new ComponentName(this,
                              RestaurantListingActivity.class)));
          else
              searchView.setSearchableInfo(searchManager
                      .getSearchableInfo(new ComponentName(this,
                              RestaurantFoodSwitcherActivity.class)));
      
      
          break;
      case R.id.rlm_change_loc:
      
          break;
      case R.id.rlm_filter_search:
      
          break;
      }
      
          return super.onOptionsItemSelected(item);
      }
      
      @Override
      public void onTabReselected(Tab tab, FragmentTransaction ft) {
          // TODO Auto-generated method stub
      
      }
      
      @Override
      public void onTabSelected(Tab tab, FragmentTransaction ft) {
      // TODO Auto-generated method stub
      }
      
      @Override
      public void onTabUnselected(Tab tab, FragmentTransaction ft) {
          // TODO Auto-generated method stub
      
      }
      
      @Override
      public void setScreenData(Object screenData, int event, long time) {
          // TODO Auto-generated method stub
      
      }
      
      @Override
      public Activity getMyActivityReference() {
          // TODO Auto-generated method stub
          return null;
      }
      
      @Override
      public void onClick(View v) {
          // TODO Auto-generated method stub
      
      }
      }
      
    2. restaurant_listing_menu

    <item
        android:id="@+id/rlm_toggle"
        android:showAsAction="always"
        android:title="Toggle Search"
        android:icon="@drawable/ic_action_refresh"
        >
    </item>
    <item
        android:id="@+id/rlm_search"
        android:showAsAction="always|collapseActionView"
        android:title="Search"
        android:icon="@drawable/ic_action_search"
        android:actionViewClass="android.widget.SearchView"
        >
    </item>
    <item
        android:id="@+id/rlm_filter_search"
        android:showAsAction="ifRoom"
        android:title="Filter Search"
        android:icon="@drawable/ic_action_settings"
        >
    </item>
    <item
        android:id="@+id/rlm_change_loc"
        android:showAsAction="ifRoom"
        android:title="Change Location"
        android:icon="@drawable/ic_action_location_off"
        >
    </item>
    

    1. searchablerestra.xml inside res>xml

      <?xml version="1.0" encoding="utf-8"?>
      <searchable xmlns:android="http://schemas.android.com/apk/res/android"
      android:hint="@string/search_hint_restra"
      android:label="@string/app_name">
      </searchable>
      
    2. RestaurantFoodSwitcherActivity.java

      public class RestaurantFoodSwitcherActivity extends RestaurantListingActivity {
      }
      
    3. searchablefood.xml

      <?xml version="1.0" encoding="utf-8"?>
      <searchable xmlns:android="http://schemas.android.com/apk/res/android"
          android:hint="@string/search_hint_food"
          android:label="@string/app_name">
      </searchable>
      
    4. manifest.xml

      <activity         android:name="com.example.app.ui.activity.RestaurantListingActivity" >
          <!-- This intent-filter identifies this activity as "searchable" -->
          <intent-filter>
              <action android:name="android.intent.action.SEARCH" />
      
              <category android:name="android.intent.category.DEFAULT" />
          </intent-filter>
          <!-- This metadata entry provides further configuration details for searches -->
          <!-- that are handled by this activity. -->
          <meta-data
              android:name="android.app.searchable"
              android:resource="@xml/searchablerestra"
      
               />
      </activity>
      <activity android:name="com.example.app.ui.activity.RestaurantFoodSwitcherActivity">
          <!-- This intent-filter identifies this activity as "searchable" -->
          <intent-filter>
              <action android:name="android.intent.action.SEARCH" />
      
              <category android:name="android.intent.category.DEFAULT" />
          </intent-filter>
          <!-- This metadata entry provides further configuration details for searches -->
          <!-- that are handled by this activity. -->
           <meta-data
              android:name="android.app.searchable"
              android:resource="@xml/searchablefood"
               />
      </activity>
      
    0 讨论(0)
  • 2021-02-08 04:44

    It looks like you're almost there.

    The SearchDialog source does this to get the auto complete editable.

    mSearchAutoComplete = (SearchAutoComplete) findViewById(com.android.internal.R.id.search_src_text);
    

    (note that the SearchAutoComplete class is a subclass of AutoCompleteTextView)

    0 讨论(0)
  • 2021-02-08 04:46

    This feels pretty hacky but worked for me - not truly dynamic but worked for alternating between the two search hints I needed:

    1. I created a second searchable.xml (as described in the Android search docs), called searchable2.xml, with my second search hint defined.
    2. I created a dummy activity extended from my original activity and overriding nothing.
    3. In the manifest the dummy activity was associated with the new searchable2.xml:

      <activity android:name=".MainActivity">
          <intent-filter>
              <action android:name="android.intent.action.VIEW" />
              <action android:name="android.intent.action.SEARCH" />
          </intent-filter>
          <meta-data android:name="android.app.searchable"
              android:resource="@xml/searchable"/>
      </activity>
      
      <activity android:name=".DummyActivity">
          <intent-filter>
              <action android:name="android.intent.action.VIEW" />
              <action android:name="android.intent.action.SEARCH" />
          </intent-filter>
          <meta-data android:name="android.app.searchable"
              android:resource="@xml/searchable2"/>
      </activity>
      
    4. In 'MainActivity' I overrode 'onSearchRequested()' to reference the appropriate searchable activity:

      
      public boolean onSearchRequested()
       {
             SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
      if(searchManager!=null) { // start the search with the appropriate searchable activity // so we get the correct search hint in the search dialog if(/* your condition here */) searchManager.startSearch(null, false,new ComponentName(this, MainActivity.class), null, false); else searchManager.startSearch(null, false,new ComponentName(this, DummyActivity.class), null, false);

      return true; } return false; }

    Nasty. But desperate times call for desperate measures...

    AFAIK, and from looking at the Android source the class is only used to look up the metadata, but if anybody knows differently then please let me know.

    0 讨论(0)
  • 2021-02-08 04:46

    If you are using the Toolbar and SearchView widgets, you can easily set the search query hint by calling:

    SearchView.setQueryHint(CharSequence hint)
    
    0 讨论(0)
提交回复
热议问题