I implemented a Material Design nav drawer using this example. I customize it according to my theme. Everything is working perfectly. Just the last thing I want to customize it's menu icon. I googled it, but have not find any good solution. I want to change its color if possible , or add a custom drawable. Any solution on idea. Thanks in advance
This is my Application theme which i set in menifest
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColorPrimary">#color/textColorPrimary</item>
<item name="android:windowBackground">#color/windowBackground</item>
</style>
And this is my toolbar style
<
style name="GalaxyZooThemeToolbarDarkOverflow" parent="Theme.AppCompat.NoActionBar">
<item name="android:textColorPrimary">#color/headerColor</item>
<item name="android:iconPreview">#drawable/ic_launcher</item>
<item name="actionMenuTextColor">#color/colorPrimaryDark</item>
<item name="android:textColorSecondary">#color/headerColor</item>
My Main activity code
public class Activity_Home extends ActionBarActivity implements FragmentDrawer.FragmentDrawerListener {
private static String TAG = Activity_Home.class.getSimpleName();
private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_drawer);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);
// display the first navigation drawer view on app launch
displayView(0);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity__main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
//
// if(id == R.id.action_search){
// Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
// return true;
// }
return super.onOptionsItemSelected(item);
}
#Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}
private void displayView(int position) {
}
}
Add:
getSupportActionBar().setHomeAsUpIndicator(resId);
or
getSupportActionBar().setHomeAsUpIndicator(drawable);
to your Activity's onCreate
Found a sloution myself. It was pretty easy indeed. All you have to do is set a toolbar style. In your style primary color act as a toolbar title and textScondary color act as a menu icon color.
style name="GalaxyZooThemeToolbarDarkOverflow" parent="Theme.AppCompat.NoActionBar">
<item name="android:textColorPrimary">#color/headerColor</item>
<item name="android:iconPreview">#drawable/ic_launcher</item>
<item name="actionMenuTextColor">#color/colorPrimaryDark</item>
<item name="android:textColorSecondary">#color/headerColor</item>
Related
I am trying to create a Nav Drawer Activity as a Base Activity. I did something like below.
When i ran it, I got below error.
E/AndroidRuntime: FATAL EXCEPTION: main Process:
com.bugmanagement.pankaj.androidexample, PID: 2297
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.bugmanagement.pankaj.androidexample/com.bugmanagement.pankaj.androidexample.Activities.UserManagement.Auth.LoginActivity}:
java.lang.IllegalStateException: This Activity already has an action
bar supplied by the window decor. Do not request
Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in
your theme to use a Toolbar instead. at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java) at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102) at
android.os.Looper.loop(Looper.java:154) at
android.app.ActivityThread.main(ActivityThread.java:6077) at
java.lang.reflect.Method.invoke(Native Method) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.IllegalStateException: This Activity already has
an action bar supplied by the window decor. Do not request
Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in
your theme to use a Toolbar instead. at
android.support.v7.app.AppCompatDelegateImplV9.setSupportActionBar(AppCompatDelegateImplV9.java:199)
at
android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:133)
at
com.bugmanagement.pankaj.androidexample.Activities.UserManagement.BaseActivity.onCreate(BaseActivity.java:31)
at
com.bugmanagement.pankaj.androidexample.Activities.UserManagement.Auth.LoginActivity.onCreate(LoginActivity.java:30)
at android.app.Activity.performCreate(Activity.java:6662) at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java) at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102) at
android.os.Looper.loop(Looper.java:154) at
android.app.ActivityThread.main(ActivityThread.java:6077) at
java.lang.reflect.Method.invoke(Native Method) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
style.xml is below
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
styles.xml(v21)
<resources>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
</resources>
Main Activity
public class MainActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Base Activity
public class BaseActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.base, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Here is the issue :
This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
You are using a theme with action bar and at the same time in your xml a Toolbar is use and set like actionbar.
Change the parent of your AppTheme with a theme without actionbar for exemple in your style.xml :
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar" >
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<!-- your item here -->
</style>
EDIT:
For the nav icon
in your activity be sure to have somethong like that:
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_closed);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
//your code
}
#Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
Hope this helps.
Sorry for my english.
Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set
windowActionBar to false in your theme
Here is the origin of your problem , and it is happened because you set <item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
in the activity you are setting toolbar as a actionBar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
remove this line <item name="windowActionBar">false</item> from your style.xml file
you can use this piece of code for no action bar theme styling . add this code in your style file
<style name="AppTheme.Dark" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
<item name="android:windowBackground">#color/primary</item>
<item name="colorControlNormal">#color/iron</item>
<item name="colorControlActivated">#color/white</item>
<item name="colorControlHighlight">#color/white</item>
<item name="android:textColorHint">#color/iron</item>
<item name="colorButtonNormal">#color/primary_darker</item>
<item name="android:colorButtonNormal" tools:targetApi="lollipop">#color/primary_darker</item>
<item name="android:typeface">monospace</item>
<item name="preferenceTheme">#style/PreferenceThemeOverlay</item>
</style>
I have read about every answer on this and still can't seem to make it work. When I call startSupportActionMode, the action mode menu is stacked on top of the toolbar instead of replacing it. This graphic is from another user that was having the same issue.
Most of the other questions were resolved by using windowActionModeOverlay in the styles. This doesn't seem to be working for me. I'm also sure I'm importing the correction action mode.
(import android.support.v7.view.ActionMode;
)
My situation is a little different. My main activity is a AppCompatActivity. I load different fragments based on choices made from a navigation drawer. The actionbar is changed based on the navigation item selected.
This code all worked before I changed over to appCompat-v7. I am trying to convert to the new material design and am therefor replacing the base actionbar with a toolbar. Here are some code snippets:
Manifest:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:name="com.acme.common.MINApplication"
android:largeHeap="true">
<activity android:name="com.acme.common.MINMainActivity"
android:theme="#style/AppTheme"
android:configChanges="orientation|screenSize|keyboardHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Styles:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="android:colorPrimary">#color/ColorPrimary</item>
<!-- Support library compatibility -->
<item name="colorPrimary">#color/ColorPrimary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="android:colorPrimaryDark">#color/ColorPrimaryDark</item>
<!-- Support library compatibility -->
<item name="colorPrimaryDark">#color/ColorPrimaryDark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:windowActionModeOverlay">true</item>
</style>
Main Activity:
public class MINMainActivity extends AppCompatActivity
{
private final String TAG = ((Object) this).getClass().getSimpleName();
private View mMainView;
// New Toolbar and Navigation View
public Toolbar toolbar;
public RecyclerView recyclerView;
private MINPageAdapter adapter;
private RecyclerView.LayoutManager layoutManager;
public DrawerLayout drawerLayout;
public android.support.v7.app.ActionBarDrawerToggle drawerToggle;
volatile public boolean isDrawerOpen = false;
private Menu mMenu;
#Override
public void onCreate(Bundle savedInstanceState)
{
supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS | Window.FEATURE_ACTION_BAR_OVERLAY);
super.onCreate(savedInstanceState);
// Inflate main view
mMainView = getLayoutInflater().inflate(R.layout.material_design_drawer_layout, null);
setContentView(mMainView);
sharedInstance = this;
// Setup New Toolbar implementation
setupToolbar();
// Setup Recycler Adapter
setupAdapter();
// Setup Navigation View
initNavigationDrawer();
// Show startup screen/fragment
showStartupScreen();
// Color components based on json settings
setCustomAttributes();
init();
}
Fragment:
public class MINPageTypeGridFragment extends Fragment
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate view
gridView = (StickyGridHeadersGridView)inflater.inflate(R.layout.page_gridview_fragment, container, false);
// Set up specific controls/views
initializeGridView(inflater, container);
...
return gridView;
}
#Override
public void onResume()
{
ActionBar actionBar = MINMainActivity.getSharedInstance().getSupportActionBar();
if(actionBar != null)
{
actionBar.show();
actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP |
ActionBar.DISPLAY_SHOW_HOME |
ActionBar.DISPLAY_USE_LOGO |
ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setTitle(pageDefinition.pageName);
}
...
super.onResume();
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int count = 0;
...
freezeGridView();
mActionMode = MINMainActivity.getSharedInstance().startSupportActionMode(mActionModeCallback);
...
return super.onOptionsItemSelected(item);
}
public ActionMode.Callback mActionModeCallback = new ActionMode.Callback()
{
#Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu)
{
MenuItem shareMenuItem;
...
actionMode.getMenuInflater().inflate(R.menu.gridview_edit_menu, menu);
shareMenuItem = menu.findItem(R.id.MenuItemShare);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareMenuItem);
if(mShareActionProvider != null)
{
mShareActionProvider.setShareIntent(Share(null));
mShareActionProvider.setOnShareTargetSelectedListener(new ShareActionProvider.OnShareTargetSelectedListener()
{
#Override
public boolean onShareTargetSelected(ShareActionProvider shareActionProvider, Intent intent)
{
if(mActionMode != null)
{
currentMode = MODE_STANDARD;
clearSelectedItems();
mActionMode.finish();
mActionMode = null;
}
return false;
}
});
...
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu)
{
return false;
}
#Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem)
{
switch(menuItem.getItemId())
{
case R.id.MenuItemEdit:
launchAlbumItemDetails(MINPageTypeGridFragment.this, currentAlbumItem, pageDefinition.pageConfigFileName);
return true;
case R.id.MenuItemDelete:
deleteItem(MINPageTypeGridFragment.this, currentAlbum, currentAlbumItem);
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode actionMode)
{
clearSelectedItems();
}
}
I'm sure that I'm missing something obvious but I'm out of ideas.
You should remove 'duplicate' definition: android:windowActionModeOverlay -> windowActionModeOverlay; android:colorPrimary -> colorPrimary and so on. This is an example that should work:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionModeOverlay">true</item>
<item name="colorControlNormal">#color/appColorControlNormal</item>
<item name="colorControlHighlight">#color/appColorControlHighlight</item>
<item name="colorControlActivated">#color/appColorControlActivated</item>
<item name="colorPrimary">#color/appColorPrimary</item>
<item name="colorPrimaryDark">#color/appColorPrimaryDark</item>
<item name="colorAccent">#color/appColorAccent</item>
</style>
Be sure to set the toolbar as current actionbar (maybe your method setupToolbar already do this):
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
After that, change your code and start the action mode directly from the toolbar reference:
toolbar.startActionMode(mActionModeCallback)
Let us know if this fix your problem.
I have set the theme of my application as NoActionBar and have implemented a appcompatv7 toolbar.
But, the toolbar doesn't show any icon for the items explicitly mentioned in the menu_main.xml file.
Even the overflow icon that is shown by default doesn't appear on the toolbar.
menu_main.xml file:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.codesters.materialdesign.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/action_navigate"
android:orderInCategory="200"
android:title="#string/next"
app:showAsAction="always"
android:icon="#drawable/ic_action_next_item"/>
</menu>
styles.xml file:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="CustomToolBarTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="android:textColorPrimary">#FFFF</item>
<item name="android:textColorSecondary">#FFFF</item>
</style>
</resources>
MainActivity file:
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Toast.makeText(this, "You have clicked" + item.getTitle(), Toast.LENGTH_SHORT);
}
if (id == R.id.action_navigate) {
Toast.makeText(this, "You have clicked" + item.getTitle(), Toast.LENGTH_SHORT);
}
return super.onOptionsItemSelected(item);
}
}
Write onCreateOptionsMenu(Menu menu) method and inflate the menu you've created in xml.
Then your code should look like.
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Toast.makeText(this, "You have clicked" + item.getTitle(), Toast.LENGTH_SHORT);
}
if (id == R.id.action_navigate) {
Toast.makeText(this, "You have clicked" + item.getTitle(), Toast.LENGTH_SHORT);
}
return super.onOptionsItemSelected(item);
}
I have made a custom theme for my app which uses action bar.Now when i run my app the actionbar is visible but button in it is not visible.I dont know what went wrong.
The button is visible when i click on menu button (the 3 hardware button) but not displaying on the action bar
Style.Xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="TripLoggerTheme" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">#style/TripLoggerActionBar</item>
<!--<item name="android:homeAsUpIndicator">#drawable/test</item>-->
</style>
<!--use to style the actionbar-->
<style name="TripLoggerActionBar" parent="android:Widget.ActionBar">
<item name="android:background">#6e784c</item>
<item name="android:showAsAction">ifRoom|always|collapseActionView|never|withText</item>
<item name="android:displayOptions">homeAsUp|showHome|showTitle</item>
</style>
Menu.Xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:icon="#drawable/images"
app:showAsAction="always" />
</menu>
Code
public class MainActivity extends Activity {
private int[] images = {R.drawable.images, R.drawable.images_2, R.drawable.images_1, R.drawable.images_4, R.drawable.images, R.drawable.images_2};
private GridView gridView;
private LinearLayout linearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base_activity);
linearLayout = (LinearLayout) findViewById(R.id.root);
getActionBar();
// gridView = (GridView) findViewById(R.id.grid);
// gridView.setAdapter(new CustomAdapter(MainActivity.this, images));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
linearLayout.setVisibility(View.VISIBLE);
break;
}
return true;
}
As the screenshots shows,when i click on the 3 dots from below test is visible but it should be visible on the top where i have marked a cross
I tried finding out what could be the problem but all in vain.
I believe what you are experiencing is the standard Android behaviour. You may want to read the following text on the official docs on Action Overflow:
The overflow icon only appears on phones that have no menu hardware keys. Phones with menu keys display the action overflow when the user presses the key.
First, from snapshot, it seems that you are testing in emulator. Check if same happens on the device.
In my project I am using search view of android.
It is working properly.
But I am having one problem i.e related to back image while searchview expands.
This is my action bar with icon and other search and drawable icon.
Now when I click on search Icon
Here Icon is changes. It's by default taking launcher image. but I want it to be same as its showing in image 1.
So can any one help me with this issue.
edit1
here is my action bar style xml.
<style name="Theme.Example" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarItemBackground">#drawable/selectable_background_example</item>
<item name="actionBarTabStyle">#style/ActionBarTabStyle.Example</item>
<item name="actionBarStyle">#style/ActionBar.Solid.Example</item>
<item name="actionModeBackground">#drawable/cab_background_top_example</item>
<item name="actionModeSplitBackground">#drawable/cab_background_bottom_example</item>
<item name="actionBarTabTextStyle">#style/ActionBarTabText.Example</item>
<!-- Light.DarkActionBar specific -->
<item name="android:textColorHighlight">#99c0c0c0</item>
</style>
edit2
substyle for actionbar
<style name="ActionBar.Solid.Example" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:icon">#drawable/ic_logo</item>
<item name="background">#drawable/ab_solid_example</item>
<item name="backgroundStacked">#drawable/ab_stacked_solid_example</item>
<item name="backgroundSplit">#drawable/ab_bottom_solid_example</item>
</style>
edit 3
This is my onCreateOptionsMenu method.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
try {
search = (MenuItem) menu.findItem(R.id.search);
MenuItemCompat.setOnActionExpandListener(search,
new OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
layoutList.setVisibility(View.INVISIBLE);
return true; // Return true to collapse action view
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
layoutList.setVisibility(View.VISIBLE);
return true; // Return true to expand action view
}
});
SearchView searchView = (SearchView) MenuItemCompat
.getActionView(search);
setSearchTextColour(searchView);
searchView.setQueryHint(getString(R.string.search_hint));
searchView.setOnQueryTextListener(this);
searchView.setOnCloseListener(new OnCloseListener() {
#Override
public boolean onClose() {
// TODO Auto-generated method stub
Debugger.debugE("on close");
layoutList.setVisibility(View.INVISIBLE);
return false;
}
});
} catch (Exception e) {
// TODO: handle exception
}
return super.onCreateOptionsMenu(menu);
}
and to show actionbar
ActionBar actionBar = getSupportActionBar();
SpannableString s = new SpannableString(getString(R.string.app_name));
s.setSpan(new TypeFaceSpan(getContext(),
getString(R.string.font_gothum)), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
actionBar.setTitle(s);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setBackgroundDrawable(new ColorDrawable(Color
.parseColor("#bb0404")));
There is nothing else that I had used.
note: In all other navigation from the main screen icon which is showing in first image is coming.
Issue is only with searchview.
Thanks in advance.
Bskania
Try changing your app theme on your styles.xml
You can do something like this:
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/ActionBar</item>
</style>
<style name="ActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:icon">#drawable/myicon</item>
</style>
create a custom action bar :
LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.custom_search_actionbar, null);
actionBar.setCustomView(v);
_searchView = (SearchView)v.findViewById(R.id.searchView);
....
....
....
and define ids of views and use .