Action provider back icon:
How do i change the back icon generated by the action provider.
I have already changed the back icon in all my activities with HomeAsUpIndicator.
But this generated back icon still with the default black arrow.
The image above show which icon i talk about, when i click on the search icon for example.
In my activity :
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_equipment);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_chevron_left_white_48dp);
[...]
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.equipments_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchActionProvider searchActionProvider = new SearchActionProvider(this);
MenuItemCompat.setActionProvider(searchItem, searchActionProvider);
return super.onCreateOptionsMenu(menu);
}
It works with this line :
app:collapseIcon="#drawable/ic_chevron_left_white_48dp"
Put it here :
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_equipment"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:title="#string/equipments"
app:titleTextColor="#color/white"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:collapseIcon="#drawable/ic_chevron_left_white_48dp" />
Edit your styles.xml and add the following:
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/CustomActionBar</item>
</style>
<style name="CustomActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:icon">#drawable/ic_close</item>
</style>
get the SupportActionbar and specfiy the id of the icon
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close);
Related
I'm using the Android component navigation for a DrawerLayout with NavigationView.
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
LoginActivity.toClose.finish();
LoginViewModel viewModel = ViewModelProviders.of(this).get(LoginViewModel.class);
Toolbar toolbar = findViewById(R.id.toolbar_menu);
setSupportActionBar(toolbar);
ActionBar actionbar = getSupportActionBar();
assert actionbar != null;
drawerLayout = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view_menu);
navigationView.inflateMenu(viewModel.setUserInterface());
NavController navController = Navigation.findNavController(this, R.id.fragment_main);
NavigationUI.setupWithNavController(navigationView, navController);
NavigationUI.setupActionBarWithNavController(this,navController,drawerLayout);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Everything's ok, except that the "Up button" are in a different color that my title in The action bar.
The XML for the Toolbar is:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:textColor="#FFF"
android:textColorPrimary="#FFF"
android:theme="#style/Toolbar"
app:layout_constraintTop_toTopOf="parent"
app:titleTextColor="#FFF"
app:title="#string/app_name"/>
The title is white, but the icon is black.
My question is: how can I change the color of this icon?
Even if I change the primary color to white and the theme editor show me the icon in white, when the app is running, the color is still black.
The app that I'm building has minSdkVersion 15
and I run it in a phone with API 7 SDK 24.
I didn't run in the emulator with SDK 15 yet.
use this style
<style name="ToolbarTheme" parent="#style/ThemeOverlay.AppCompat.ActionBar">
<!-- Customize color of Toolbar -->
<item name="colorControlNormal">#color/WhiteColor</item>
</style>
and then use it in app:theme="#style/ToolbarTheme" in your Toolbar XML :
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:textColor="#FFF"
android:textColorPrimary="#FFF"
app:theme="#style/ToolbarTheme"
app:layout_constraintTop_toTopOf="parent"
app:titleTextColor="#FFF"
app:title="#string/app_name"/>
Another option, with new Themes (Light/Dark modes) is to use styles and themes.
First approach.
This works if you use default icons and in a fragment (e.g.) you use following: toolbar.setupWithNavController(findNavController()), so then in layout you must do following:
In your app theme in xml:
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
...
<item name="drawerArrowStyle">#style/MyDrawerArrowToggleStyle</item>
Note: This solution is for your own toolbars, which you placed in your layout by yourself, that's why my main app theme is Theme.MaterialComponents.DayNight.NoActionBar. However, I haven't tried with default ActionBar, so maybe it will work as well.
Your drawer arrow/toggle style:
<style name="MyDrawerArrowToggleStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="color">#color/your_color_here</item>
</style>
And now all default "<-" icons in toolbars will be in your color. It should work for Hamburger icons too. But I didn't check, as I don't have navigation drawer.
Second approach.
When you want your own icon or action in NavigationOnClickListener.
In the fragment:
toolbar.setNavigationOnClickListener {
findNavController().navigateUp()
//plus another actions if required
}
toolbar.setNavigationIcon(R.drawable.your_icon_here)
In main theme:
<item name="toolbarNavigationButtonStyle">#style/MyAppToolbarNavButtonStyle</item>
My style:
<style name="MyAppToolbarNavButtonStyle" parent="Widget.AppCompat.Toolbar.Button.Navigation">
<item name="tint">#color/your_color_here</item>
</style>
I am developing a web app, and here I have a problem.
I have a tool bar(android widget toolbar) with logo and search button
As you can see I have the login page(webview). I want to user to see this search button after users login to the webpage. How should I do that?
Edited:
In my toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/my_toolbar"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#000000"
android:elevation="3dp"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
In my menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<item
android:id="#+id/action_search"
android:orderInCategory="200"
android:title="#string/action_search"
android:icon="#drawable/ic_action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
And in MainActivity.java onCreate,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
//toolbar.setNavigationIcon(R.mipmap.logo9);
toolbar.setTitle("");
toolbar.setSubtitle("");
......
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
And finally in activity_main.xml, I just include the toolbar,
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
></include>
If the search button is an Android View, you can hide it by calling View.setVisibility(View.GONE).
If you have instead defined it as a menu XML resource and loaded it in this Activity, then just don't load the menu resource unless the user is logged in. That is done in public boolean onCreateOptionsMenu(Menu menu) of the Activity.
If you go to another Activity after logging in, then just use a separate Toolbar there, with the icon like here, but without one in the login Activity.
Please provide more information on how you added the search icon to the Toolbar in the first place, then I will be able to help you more.
Edit:
You can use the WebView.getUrl() method to get the URL of the displayed website, if your login page is on login.php, you can look if the URL is that, and then not show the search icon. For example you can check the URL with String.contains(), like: .contains("login.php"). And if that is true don't show the button.
In your styles for this activity use this attribute in your style for this activity
<item name="android:windowNoTitle">true</item>
I'm trying to add share action to my toolbar. Toolbar is supposed to be orange (or transparent like in this case) with white text and icons, so I'm using this view as Toolbar:
<android.support.v7.widget.Toolbar 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"
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
tools:ignore="UnusedAttribute" />
Also, this is how my app theme declaration looks like:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
No matter how I change the style this is what I get:
How do I convince ShareActionProvider to get Light theme?
This is what i did and it worked. i wanted a white background in ShareActionProvider with it text being black
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:theme="#style/MainTheme"
app:layout_collapseMode="pin"/>
My theme
<style name="MainTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/md_white_1000</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColorPrimary">#ffffff</item>
<item name="android:textColorSecondary">#ffffff</item>
<item name="android:textColor">#color/md_black_1000</item>
<item name="listPopupWindowStyle">#style/PopupListStyle</item>
</style>
<style name="PopupListStyle" parent="#style/Widget.AppCompat.Light.ListPopupWindow">
<item name="android:popupBackground">#ffffff</item>
</style>
my solution is based on support library v7 , toolBar , ActionBarActivity , Android Studio
1- remove app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
2- make sure your base theme is Theme.AppCompat.Light.NoActionBar
3- go to original code of ShareActionProvider by typing "ShareActionProvider" anywhere in your code then import the v7 one then aim your mouse on it then (ctrl + left click)
4- copy the code in it and paste it in a new java file in you project directory
5- go to your own ShareActionProvider and remove this import if you have it import android.support.v7.appcompat.R
6- provide your own share icon because the default one is black
Drawable myDrawable = mContext.getResources().getDrawable(R.drawable.ic_share);
activityChooserView.setExpandActivityOverflowButtonDrawable(myDrawable);
7- go to your activity and remove the import you made in step 3 ( to use your own file )
8- go to your onCreateOptionsMenu it should be like this :
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = new ShareActionProvider(MainActivity.this);
MenuItemCompat.setActionProvider(item , mShareActionProvider);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello World");
shareIntent.setType("text/plain");
mShareActionProvider.setShareIntent(shareIntent);
return true;
}
9- the last step is don't forget to edit your menu.xml
app:actionProviderClass=
"com.yourPackageName.ShareActionProvider" />
I am using the ActionMode via:
#Override
public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu_list_context, menu);
this.mActionMode = mode;
return true;
}
And my resource file menu_list_context is:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/discard_button"
android:icon="#drawable/ic_action_discard"
android:title="delete"
android:showAsAction="ifRoom" />
The style for my ActionBar is:
<style name="MyActionBar"
parent="#android:style/Widget.Holo.Light.ActionBar.Solid">
<item name="android:background">#color/background_holo_light</item>
</style>
where <color name="background_holo_light">#dddddd</color>.
With these settings, my ActionBar under ActionMode looks like this:
The icon has the background of my ActionBar (holo light), but the ActionMode background seems white. How can I fix the icon background to have the ActionMode background (including the blue bottom line)?
You could try using the Action Bar Style Generator. This is the only way I know of to get exact ActionBar styles that actually works.
I wanna change default ActionBar homeAsUp indicator (drawable) in my AppCompat Toolbar. How to achieve that?
Only default arrow shows up.
styles (same for other API's level):
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- API 14 theme customizations can go here. -->
<item name="android:windowActionBarOverlay">true</item>
<item name="android:homeAsUpIndicator">#drawable/home</item>
<!-- Support library compatibility -->
<item name="windowActionBarOverlay">true</item>
<item name="homeAsUpIndicator">#drawable/home</item>
</style>
Toolbar in my fragment layout:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:homeAsUpIndicator="#drawable/home"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/AppTheme" />
In Fragment:
toolbar = (Toolbar) rootView.findViewById(R.id.my_awesome_toolbar);
activity.setSupportActionBar(toolbar);
ActionBar actionBar = activity.getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
To change icon just call at runtime:
toolbar.setNavigationIcon(R.drawable.home);
Trick with styles/themes not working.
How to enable homeAsUp or call setDisplayHomeAsUpEnabled() on standalone toolbar with appcompat v21
Just add this line to your styles.xml
<item name="navigationIcon">#drawable/ic_back_arrow</item>
I tried setHomeAsUpIndicator(int resId) ,it works.
private void initToolbar ( Toolbar mToolbar ) {
mToolbar.setTitleTextColor ( getResources ().getColor ( R.color.main_title ) );
setSupportActionBar ( mToolbar );
ActionBar actionbar = getSupportActionBar ();
actionbar.setDisplayHomeAsUpEnabled ( true );
actionbar.setHomeAsUpIndicator ( R.drawable.ic_action_back );
}
But mToolbar.setNavigationIcon(R.drawable.ic_action_back) doesn't work :(
If android.support.v7.app.ActionBarDrawerToggle used together with DrawerLayout and Toolbar you can change homeAsUp icon with the following code:
//set home as up indicator
mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_up_indicator);
//remove home as up indicator
mDrawerToggle.setHomeAsUpIndicator(null);
to show homeAsUpIndicator indicator instead of home indicator do following:
mDrawerToggle.setDrawerIndicatorEnabled(false);
Docs:
ActionBarDrawerToggle#setHomeAsUpIndicator
ActionBarDrawerToggle#setDrawerIndicatorEnabled
i just found out that it actually works
toolbar.setNavigationIcon(R.drawable.ic_action_back)
and here is the code to make it work.
#Override
protected void onCreate(Bundle savedInstanceState) {
.....
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_action_back);
Post the following code in onOptionsItemSelected(MenuItem item)
#Override
public boolean onOptionsItemSelected(MenuItem item) {
......
if (id == android.R.id.home) {
startActivity(new Intent(this, MainActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
Note Mainactivity in this code is the current activity i want my back or home button to go to. I hope this helps
Unfortunately, if this style line is used in a commonly shared AppCompat ToolBar/App bar with other activities in the App, including the main activity, the "navigationIcon" specified will be automatically shown, unlike the standard default HomeAsUpIndicator, which is not shown unless explicitly enabled as desired in an Activity, typically as follows: (in the onCreate())
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
With the above style line, since it behaves in an opposite manner, in a similar fashion, the indicator needs to be explicitly disabled if it is not desired to be shown, as on the main activity, as follows: (in the onCreate())
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
(as observed on Android 4.0.4, 4.3, and 4.4.2 phones)