Ejemplo n.º 1
0
 /**
  * * Set whether the menu button should be shown. Particularly useful for apps that adapt to
  * screen sizes
  *
  * @param visibility Whether to show
  */
 public void setMenuVisibility(int visibility) {
   materialMenu.setVisibility(visibility);
 }
Ejemplo n.º 2
0
  /**
   * Create a searchbox with params and a style
   *
   * @param context Context
   * @param attrs Attributes
   * @param defStyle Style
   */
  public SearchBox(final Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    inflate(context, R.layout.searchbox, this);
    this.searchOpen = false;
    this.isMic = true;
    this.materialMenu = (MaterialMenuView) findViewById(R.id.material_menu_button);
    this.logo = (TextView) findViewById(R.id.logo);
    this.search = (EditText) findViewById(R.id.search);
    this.results = (ListView) findViewById(R.id.results);
    this.context = context;
    this.pb = (ProgressBar) findViewById(R.id.pb);
    this.mic = (ImageView) findViewById(R.id.mic);
    this.overflow = (ImageView) findViewById(R.id.overflow);
    this.drawerLogo = (ImageView) findViewById(R.id.drawer_logo);
    materialMenu.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            if (searchOpen) {

              toggleSearch();
            } else {
              if (menuListener != null) menuListener.onMenuClick();
            }
          }
        });
    resultList = new ArrayList<SearchResult>();
    setAdapter(new SearchAdapter(context, resultList, search));
    animate = true;
    isVoiceRecognitionIntentSupported =
        isIntentAvailable(context, new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));
    logo.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            toggleSearch();
          }
        });
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
      RelativeLayout searchRoot = (RelativeLayout) findViewById(R.id.search_root);
      LayoutTransition lt = new LayoutTransition();
      lt.setDuration(100);
      searchRoot.setLayoutTransition(lt);
    }
    searchables = new ArrayList<SearchResult>();
    search.setOnEditorActionListener(
        new OnEditorActionListener() {
          public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
              search(getSearchText());
              return true;
            }
            return false;
          }
        });
    search.setOnKeyListener(
        new OnKeyListener() {
          public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER) {
              if (TextUtils.isEmpty(getSearchText())) {
                toggleSearch();
              } else {
                search(getSearchText());
              }
              return true;
            }
            return false;
          }
        });
    logoText = "";
    micStateChanged();
    mic.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            if (voiceRecognitionListener != null) {
              voiceRecognitionListener.onClick();
            } else {
              micClick();
            }
          }
        });

    overflow.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            popupMenu.show();
          }
        });

    search.addTextChangedListener(
        new TextWatcher() {

          @Override
          public void afterTextChanged(Editable s) {
            if (s.length() > 0) {
              micStateChanged(false);
              mic.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_clear));
              updateResults();
            } else {
              micStateChanged(true);
              mic.setImageDrawable(
                  getContext().getResources().getDrawable(R.drawable.ic_action_mic));
              if (initialResults != null) {
                setInitialResults();
              } else {
                updateResults();
              }
            }

            if (listener != null) listener.onSearchTermChanged(s.toString());
          }

          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {}
        });
    // Default search Algorithm
    mSearchFilter =
        new SearchFilter() {
          @Override
          public boolean onFilter(SearchResult searchResult, String searchTerm) {
            return searchResult.title.toLowerCase().startsWith(searchTerm.toLowerCase());
          }
        };
  }