Last active
February 28, 2016 19:30
-
-
Save faogustavo/08acf753e86c74da0522 to your computer and use it in GitHub Desktop.
Uma forma de facilitar o uso do SearchView com a Toolbar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<android.support.v7.widget.Toolbar | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:id="@+id/toolbar" | |
android:layout_height="?attr/actionBarSize" | |
android:layout_width="match_parent" | |
android:minHeight="?attr/actionBarSize" | |
android:background="?attr/colorPrimary" | |
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" | |
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.support.v4.view.MenuItemCompat; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.support.v7.widget.Toolbar; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.widget.Toast; | |
public class MainActivity extends AppCompatActivity { | |
private SearchView searchView; | |
private Toolbar toolbar; | |
private static final String TAG = "tag"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
toolbar = (Toolbar) findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.main_menu, menu); | |
searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search)); | |
searchView.setSearchableInfo(searchView.getManager().getSearchableInfo(getComponentName())); | |
searchView.setIconified(true); | |
searchView.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener() { | |
@Override | |
public boolean onQueryTextSubmit(String query) { | |
Log.d(TAG, "Submit: " + query); | |
return false; | |
} | |
@Override | |
public boolean onQueryTextChange(String newText) { | |
Log.d(TAG, "Change: " + newText); | |
return false; | |
} | |
}); | |
searchView.setOnCloseListener(new android.support.v7.widget.SearchView.OnCloseListener() { | |
@Override | |
public boolean onClose() { | |
Toast.makeText(MainActivity.this, "Search View Closed", Toast.LENGTH_SHORT).show(); | |
return false; | |
} | |
}); | |
return true; | |
} | |
@Override | |
public void onBackPressed() { | |
if (searchView.isShown()) | |
searchView.hide(); | |
else | |
super.onBackPressed(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.SearchManager; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.widget.ImageView; | |
/** | |
* Created by Gustavo Fão Valvassori on 28/02/2016. | |
* Propósito: Facilitar o uso do seachview | |
*/ | |
public class SearchView extends android.support.v7.widget.SearchView { | |
private SearchManager manager; | |
private SearchAutoComplete mSearchSrcTextView; | |
private ImageView mCloseButton; | |
public SearchView(Context context) { | |
super(context); | |
__init__(); | |
} | |
public SearchView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
__init__(); | |
} | |
public SearchView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
__init__(); | |
} | |
private void __init__() { | |
mSearchSrcTextView = (SearchAutoComplete) findViewById(android.support.v7.appcompat.R.id.search_src_text); | |
mCloseButton = (ImageView) findViewById(android.support.v7.appcompat.R.id.search_close_btn); | |
manager = (SearchManager) getContext().getSystemService(Context.SEARCH_SERVICE); | |
} | |
public SearchManager getManager() { | |
return manager; | |
} | |
public boolean isShown() { | |
return mSearchSrcTextView.isShown(); | |
} | |
public void hide() { | |
mSearchSrcTextView.setText(""); | |
mCloseButton.performClick(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<!-- Base application theme. --> | |
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> | |
<!-- Customize your theme here. --> | |
<item name="colorPrimary">@color/colorPrimary</item> | |
<item name="colorPrimaryDark">@color/colorPrimaryDark</item> | |
<item name="colorAccent">@color/colorAccent</item> | |
</style> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment