GithubHelp home page GithubHelp logo

neil-orzzh / materialsearchbar Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mancj/materialsearchbar

0.0 1.0 0.0 1.46 MB

Material Design Search Bar for Android

License: MIT License

Java 100.00%

materialsearchbar's Introduction

#Material SearchBar Android Material Design Search Bar for Android


This beautiful and easy to use library will help to add Lollipop Material Desing SearchView in your project.

Material Design SearchBar Android gif Gif


#How to use

to include SearchBar to your project:

add this to the the project level build.gradle file

allprojects {
	repositories {
		...
		maven { url "https://jitpack.io" }
	}
}

add the dependency to the the app level build.gradle file

'com.github.mancj:MaterialSearchBar:0.1.0'

then add SearchBar to your activity:

<com.mancj.materialsearchbar.MaterialSearchBar
    app:speechMode="true"
    app:hint="Custom hint"
    app:maxSuggestionsCount="10"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/searchBar" />

MaterialSarchBar has the following xml attributes:

attribute description
hint Set custom prompt in the search box
speechMode If set to true, microphone icon will display instead of the search icon.
maxSuggestionsCount Specifies the maximum number of search queries stored until the activity is destroyed
iconLeft Change left icon
iconRight Change right icon

public methods:

  • disableSearch()
  • enableSearch()
  • isSearchEnabled()
  • setHint(CharSequence hint)
  • setSpeechMode(boolean speechMode)
  • setOnSearchActionListener(OnSearchActionListener onSearchActionListener)
  • setLastSuggestions(List<String> suggestions)
  • getLastSuggestions()
  • setMaxSuggestionCount(int maxQuery)
  • setIconLeft(int iconLefttResId)
  • setIconRight(int iconRightResId)

To save search queries when the activity is destroyed, use the method searchBar.getLastSuggestions() and then, to restore them use searchBar.setLastSuggestions(List<String>); as shown in the example below

#Example

Here is a simple example of using MaterialSearchBar

private List<String> lastSearches;
private MaterialSearchBar searchBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    searchBar = (MaterialSearchBar) findViewById(R.id.searchBar);
    searchBar.setHint("Custom hint");
    searchBar.setSpeechMode(true);
    //enable searchbar callbacks
    searchBar.setOnSearchActionListener(this);
    //restore last queries from disk
    lastSearches = loadSearchSuggestionFromDiks();
    searchBar.setLastSuggestions(list);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    //save last queries to disk
    saveSearchSuggestionToDisk(searchBar.getLastSuggestions());
}

//called when searchbar enabled or disabled
@Override
public void onSearchStateChanged(boolean enabled) {
    String s = enabled ? "enabled" : "disabled";
    Toast.makeText(MainActivity.this, "Search " + s, Toast.LENGTH_SHORT).show();
}

//called when user confirms request
@Override
public void onSearchConfirmed(CharSequence text) {
    startSearch(text.toString(), true, null, true);
}

//called when microphone icon clicked
@Override
public void onSpeechIconSelected() {
    openVoiceRecognizer();
}

#TODO

  • first of all I'll add background dim behing SearchBar

materialsearchbar's People

Watchers

Neil avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.