revise ActionBar samples to include an action view using SearchView,
also modify the sub-menu item to something that makes sense and demonstrate how to invalidate the action bar and redraw it Change-Id: I84f5414c74ab47f7657c226daa2334e7e4449e91
This commit is contained in:
@@ -16,9 +16,14 @@
|
||||
package com.example.android.apis.app;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.widget.SearchView;
|
||||
import android.widget.SearchView.OnQueryChangeListener;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.example.android.apis.R;
|
||||
@@ -29,14 +34,35 @@ import com.example.android.apis.R;
|
||||
* menu data itself. If you'd like to see how these things work under the hood, see
|
||||
* ActionBarMechanics.
|
||||
*/
|
||||
public class ActionBarUsage extends Activity {
|
||||
public class ActionBarUsage extends Activity implements OnQueryChangeListener {
|
||||
TextView mSearchText;
|
||||
int mSortMode = -1;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
mSearchText = new TextView(this);
|
||||
setContentView(mSearchText);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
MenuInflater inflater = getMenuInflater();
|
||||
inflater.inflate(R.menu.actions, menu);
|
||||
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
|
||||
searchView.setOnQueryChangeListener(this);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPrepareOptionsMenu(Menu menu) {
|
||||
if (mSortMode != -1) {
|
||||
Drawable icon = menu.findItem(mSortMode).getIcon();
|
||||
menu.findItem(R.id.action_sort).setIcon(icon);
|
||||
}
|
||||
return super.onPrepareOptionsMenu(menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
Toast.makeText(this, "Selected Item: " + item.getTitle(), Toast.LENGTH_SHORT).show();
|
||||
@@ -46,7 +72,24 @@ public class ActionBarUsage extends Activity {
|
||||
// This method is specified as an onClick handler in the menu xml and will
|
||||
// take precedence over the Activity's onOptionsItemSelected method.
|
||||
// See res/menu/actions.xml for more info.
|
||||
public void onSearch(MenuItem item) {
|
||||
Toast.makeText(this, "Searching...", Toast.LENGTH_SHORT).show();
|
||||
public void onSort(MenuItem item) {
|
||||
mSortMode = item.getItemId();
|
||||
// Request a call to onPrepareOptionsMenu so we can change the sort icon
|
||||
invalidateOptionsMenu();
|
||||
}
|
||||
|
||||
// The following callbacks are called for the SearchView.OnQueryChangeListener
|
||||
// For more about using SearchView, see src/.../view/SearchView1.java and SearchView2.java
|
||||
@Override
|
||||
public boolean onQueryTextChanged(String newText) {
|
||||
newText = newText.isEmpty() ? "" : "Query so far: " + newText;
|
||||
mSearchText.setText(newText);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onSubmitQuery(String query) {
|
||||
Toast.makeText(this, "Searching for: " + query + "...", Toast.LENGTH_SHORT).show();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user