Friday, April 3, 2020

My Quick Find Gallery (Part 1 - Search Option in Action Bar)

1) res/values/styles.xml
must be change to Base.Theme otherwise no tools will show in layout

2) Splash Screen: Code for redirect one screen to another screen after few seconds

SplashScreen.java

 Handler handler=new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent=new Intent(SplashScreen.this,SearchActivity.class);
                startActivity(intent);
                finish();
            }
        },1500);

AndroidMainfest.xml

<activity android:name=".SplashScreen">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:name=".SearchActivity" />

3) Search option in Action Bar:
    sub tasks: 
i) search option in expand state, not in icon
ii) Focus on search option, don't need to click search box
iii) when i click on search button then searching should be done, not on text change.
iv) When i enter text in search box then suggestion list should be open: For this feature, we will use android.support.v7.widget.SearchView in place of android.widget.SearchView. We can do easily way.
v) Suggestion list background is showing  in gray color, want to change in white color
vi) When i click on suggestion list item then that item should be come into search box
vii) When i click suggestion list item then in search Function comparison done by string.matches and if i enter random text then in search Function comparison done by string.contains

res/menu/menu_search.xml

<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item 
android:id="@+id/action_search"   
android:title="search"   
app:showAsAction="always"   
app:actionViewClass="android.support.v7.widget.SearchView" />
<!--
The showAsAction attribute allows you to define how the action is displayed.
ifRoom attribute defines that the action is only displayed in the action bar if there is sufficient screen space available.
-->
</menu>

 SearchActivity.java

private Menu menu;
SearchView search;
SearchView.SearchAutoComplete searchAutoComplete;
ArrayList<String> uniqueFileName;
int isSearchAction = 0;

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// The MenuInflator class allows to inflate actions defined in an XML file and adds them to the action bar. 
// MenuInflator can get accessed via the getMenuInflator() method from your activity. 
// While you can define the actions also in your source code, it is good practice to do this via XML files, as this results in less boilerplate code. 
 
    getMenuInflater().inflate(R.menu.menu_search, menu);
    this.menu = menu;
    search = (SearchView) menu.findItem(R.id.action_search).getActionView();
//i)
    search.setIconifiedByDefault(false);
//ii)
    search.requestFocus();
//iii)
    search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override     
        public boolean onQueryTextSubmit(String query) {
            isSearchAction=3;
            if(getFilePathArray(isSearchAction,query)>0) {
                  showImageInGrid(searchFilesPath);//ByFilePathArrayStringAndImageCustomAdapter
                  search.clearFocus();
            }
            else {
                  mImageGrid.setAdapter(null);
                  emptyResult();
            }
            return true;
        }
        @Override     
        public boolean onQueryTextChange(String query) {
            return false;
        }
    });
//iv)
    searchAutoComplete = search.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, uniqueFileName);
    searchAutoComplete.setAdapter(adapter);
    SearchManager searchManager =
            (SearchManager) getSystemService(this.SEARCH_SERVICE);
    search.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));
//v)
    searchAutoComplete.setDropDownBackgroundResource(R.color.White);
//vi)
    searchAutoComplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override   
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            // TODO Auto-generated method stub
            String searchString=(String)parent.getItemAtPosition(position);
            //searchAutoComplete.setText(searchString);
//vii)         
           isSearchAction=2;//AutoCompleteItemSelect
           if(getFilePathArray(isSearchAction,searchString)>0) {
                    showImageInGrid(searchFilesPath);//ByFilePathArrayStringAndImageCustomAdapter
                    search.clearFocus();
           }
           else {
                   mImageGrid.setAdapter(null);
                   emptyResult();
           }
            //Toast.makeText(this, "you clicked "+searchString, Toast.LENGTH_LONG).show();
        }
    });
    return true;
}
contd...

No comments:

Post a Comment