Giter Club home page Giter Club logo

search-dialog's Introduction

search-dialog

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

search_dialog

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 {
    implementation 'com.github.mirrajabi:search-dialog:1.2.4'
}

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) {
                                // If filtering is enabled, [position] is the index of the item in the filtered result, not in the unfiltered source
                                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 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

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.4 - Added an option to SimpleSearchDialogCompat so that the dialog cancellation on touching outside the dialog can be customized.

1.2.3 - Changed minSdkVersion to 14. Added getter for the title textview of simple search dialog. Improved results sorting.

1.2.2 - Gradle tools version and dependencies were updated.

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

1.2 - Added 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 - Added loading feature.

search-dialog's People

Contributors

alashow avatar irsalshabirin avatar mirrajabi avatar nikunjparadva avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

search-dialog's Issues

How do I change list item(Arraylist) specific text color

Hello,

First, thank you for this good library.

I populate the list from the database(sqlite). I just want to change the color of some items value. For example; one of value from database is "RED" at the same time I should change that index text color to red. How can i do this.

change the search dialog item's view/layout

Hi

I am new to android , Recently I used your search dialog and it is really amazing. but the problem is less documentation. the top pictures describes it can be done in many views. But i cant understand how to do it. Ex: adding images to the layout.
And also cant make the position of the dialog

Can you help me.

Thanks

Get typed string

I need to get the typed string for online queries so as to add items to suggestions list. Is there a method to do so?

Title is under search bar

The issue is that the title is not on top of the search bar like it should. I don't know why it occurs, but this issue is present in the latest version, but in version 1.1, it works correctly.

On Version 1.2.4:

Version_1_2_4

Whereas on Version 1.1:

Version_1_1

Increase search dialog size

Is there anyway to increase the size of the search dialog? It would help a lot in landscape mode as it only takes up a fraction of the screen.

Highlighting Position Issue

There is a highlighting position issue. If you search for a whole word, it will start highlighting where it can find the first letter, doesn't matter where the word.
In this example, I'm searching for dengue, it will mark the first d in the first word, the e in the second word, and the rest of the letters are found in the correct word:
Screenshot_20210619-144022

Get real position of selected value

How to get selected item real potion. when search some item and select it then it return the selected chunk position not the real position.

Optional parameter to define whether touching the title closes the dialog or not

Hello, this function would be perfect if you make it optional since it is uncomfortable for some users with large fingers, many times they close the dialogue by accident

view.findViewById(R.id.dummy_background) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dismiss(); } });

Proguard

i have implement this library in my entire project. but i have also added proguard in app.

When i used sign apk it's not working...

Can you please share proguard rules for that .

Waiting for your quick replay.

Thanks

Not dismis when i selected item

When displaying searchdialog from a fragment. When the user selects item, it will not dismis the dialog immediately. It only hides after 3 presses

The library overrides the search icon

Hey.

Thanks for your library!

I have a search icon in my app with name "ic_search". When I use your library, my search icon is being replaced with the icon included in the library.
I guess you just need to add a prefix, like: search_dialog_ic_search.png.

not getting filtered

Hi

I implemented your library in my app which have a activity and tow tabs using tab layout view pager and fragment but everything is working right but the list not getting filtered while typing in search box I why this happen any clue

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.