activity_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sample_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<SearchView
android:id="@+id/searchView"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
SearchView search;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
search = (SearchView) findViewById(R.id.searchView);
search.setQueryHint("Search...");
search.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
Toast.makeText(getBaseContext(), String.valueOf(hasFocus),
Toast.LENGTH_SHORT).show();
}
});
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(getBaseContext(), query,
Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
Toast.makeText(getBaseContext(), newText,
Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
No comments:
Post a Comment