Set search hint dynamically

后端 未结 7 1441
悲哀的现实
悲哀的现实 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: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

    
    
    
    
    
    
    
    
    

    1. searchablerestra.xml inside res>xml

      
      
      
      
    2. RestaurantFoodSwitcherActivity.java

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

      
      
      
      
    4. manifest.xml

      
          
          
              
      
              
          
          
          
          
      
      
          
          
              
      
              
          
          
          
           
      
      

提交回复
热议问题