Search Dialog


Source link: https://github.com/mirrajabi/search-dialog

search-dialog

An awesome and customizable search dialog with built-in search options.

Usage

First add jitpack to your projects build.gradle file

allprojects {

  repositories {

...

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

  
}
 
}

Then add the dependency in modules build.gradle file

dependencies {

  compile 'com.github.mirrajabi:search-dialog:1.2' 
}

Simple usage

if you just want to use the simple search dialog first you need to provide searchable items. to achieve this you should implement Searchable in your model.

you can see the SampleSearchModel for example :

public class SampleSearchModel implements Searchable {

  private String mTitle;

public SampleSearchModel(String title) {

mTitle = title;
  
}

@Override
  public String getTitle() {

return mTitle;
  
}

public SampleSearchModel setTitle(String title) {

mTitle = title;

return this;
  
}
 
}

now generate some search options in your activity :

 private ArrayList<SampleSearchModel> createSampleData(){

ArrayList<SampleSearchModel> items = new ArrayList<>();

items.add(new SampleSearchModel("First item"));

items.add(new SampleSearchModel("Second item"));

items.add(new SampleSearchModel("Third item"));

items.add(new SampleSearchModel("The ultimate item"));

items.add(new SampleSearchModel("Last item"));

items.add(new SampleSearchModel("Lorem ipsum"));

items.add(new SampleSearchModel("Dolor sit"));

items.add(new SampleSearchModel("Some random word"));

items.add(new SampleSearchModel("guess who's back"));

return items;
  
}

then you just need to add the below lines where you want to show the dialog :

new SimpleSearchDialogCompat(MainActivity.this, "Search...",

 "What are you looking for...?", null, createSampleData(),

 new SearchResultListener<SampleSearchModel>() {

  @Override

  public void onSelected(BaseSearchDialogCompat dialog,

 SampleSearchModel item, int position) {

Toast.makeText(MainActivity.this, item.getTitle(),

  Toast.LENGTH_SHORT).show();

dialog.dismiss();

  
}

 
}
).show();

The constructor parameters are

SimpleSearchDialogCompat(Context context, String title, String searchHint,

 @Nullable Filter filter, ArrayList<T> items,

 SearchResultListener<T> searchResultListener)

Loading view(added to SimpleSearchDialogCompat in v1.1)

Just use setLoading(true) for showing and setLoading(false) for hiding it on an instance of SimpleSearchDialogCompat

Changing default adapters text colors(added in v1.2.1)

If you want to change the default colors just override these colors in your colors.xml or wherever you want like this.

 <color name="searchDialogResultColor"/>
  <color name="searchDialogResultHighlightColor"/>

Advanced usage

The layout

I used this layout for simple search dialog but you can use anything else. Of course your layout should have thse two views :

  • An EditText to use as search key input
  • A RecyclerView for showing the results in it

The search dialog

You can use your custom layouts, adapters and search options by creating a class inheriting the BaseSearchDialogCompat take a look at SimpleSearchDialogCompat to see an example of how it can be done You should implement the BaseSearchDialogCompat methods :

 // handle your view with this one
  protected abstract void getView(View view);

  // Id of your custom layout
  @LayoutRes protected abstract int getLayoutResId();

  // Id of the search edittext you used in your custom layout
  @IdRes protected abstract int getSearchBoxId();

  // Id of the recyclerview you used in your custom layout
  @IdRes protected abstract int getRecyclerViewId();
 

The search filter

You can use your custom filters for text searching. The one used in SimpleSearchDialogCompat is SimpleSearchFilter. It checks the search key and if an item and the key had partially exact same letters it will add that item to results and also if the CheckLCS was set to true, it will check if the amount of matching letters was bigger than the given AccuracyPercentage the item will be added to results

The adapter

the one used in SimpleSearchDialogCompat is so simple despite its too long. the main functionality is in initializeViews method. you can create your custom adapters and use it instead of this one

The StringsHelper

it has two methods which you can use for highlighting the results.

/*  * Returns a SpannableString with   * highlighted LCS(Longest Common Subsequence)  * of two strings with the givven color  */ SpannableStringBuilder highlightLCS(String text1, String text2, int highlightColor);
  // Returns the LCS(Longest Common Subsequence) of two strings String lcs(String text1, String text2) 

See the sample app to get a better understanding of the advanced usage

Used in sample app

Changelog

1.2.1 - Adds an option for changing text color and highlight color of default adapter.

1.2 - Adds getter for views in simple search dialog and an option to turn off the auto filtering on search edittext.

1.1.1 - Fixes drawable overriding issue.

1.1 - Adds loading feature.

Resources

Lollipop ViewAnimationUtils.createCircularReveal for everyone 2.3+

Android specific bindings for RxJava.

This module adds a number of classes to RxJava that make writing reactive components in Android applications easy and hassle free.

String operators for RxJava.

advanced-textview is a companion app for Advanced Android TextView talk, demonstrating:

  • Animated CompoundDrawable
  • Text shadow
  • Custom font
  • Gradient text
  • Patterned text
  • HTML.fromHtml()
  • setFontFeatureSettings()
  • Styled string
  • AlignmentSpan
  • ClickableSpan
  • Rainbow span, animated
  • Lined paper
  • Emoji

Views and Drawable for animated GIFs in Android.

Bundled GIFLib via JNI is used to render frames. This way should be more efficient than WebView or Movie classes.
Animation starts automatically and run only if View with attached GifDrawable is visible.

Gradle plugin for Android which lets you use Java generics in your XML layouts.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes