Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The Android ActionBar Tips
// How customize the SearchView in the ActionBar. In your activity
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.SearchView.SearchAutoComplete;
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_home, menu);
MenuItem searchItem = menu.findItem(R.id.item_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
mSearchView.setOnQueryTextListener(this);
mSearchView.setQueryHint(getString(R.string.text));
SearchAutoComplete searchAutoComplete = (SearchAutoComplete) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchAutoComplete.setHintTextColor(mRes.getColor(android.R.color.white));
searchAutoComplete.setTextSize(14);
return super.onCreateOptionsMenu(menu);
}
// How make the ActionBar title clickable on Android under API 11 (1/2)
private static final boolean API_LEVEL_UNDER_11 = android.os.Build.VERSION.SDK_INT < 11;
private boolean navigationDrawerEnabled = true; // Useful if your app need navigation drawer
@Override
protected void onCreate(Bundle savedInstanceState) {
if (API_LEVEL_UNDER_11) {
getSupportActionBar().setCustomView(R.layout.partial_actionbar);
getSupportActionBar().setDisplayShowCustomEnabled(true);
((TextView) getSupportActionBar().getCustomView().findViewById(R.id.action_bar_text)).setText(title);
} else {
getSupportActionBar().setTitle(title);
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
...
public void onClickTitle(View v) {
if (navigationDrawerEnabled) {
toggleDrawer();
} else {
finish();
}
}
// How make the ActionBar title clickable on Android under API 11 (2/2)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/action_bar_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/action_bar"
android:clickable="true"
android:gravity="center_vertical"
android:onClick="onClickTitle"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="@string/app_name"
android:textSize="21sp" />
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment