How to change HomeAsUp indicator in new AppCompat Toolbar? - android

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)

Related

How to change toolbar back button icon in android material components

I would like to change the default navigate up icon (back button icon) to my custom icon. I not using a drawer, just a simple toolbar and material components
Is this possible?
If you are using a Toolbar to change the icon just use:
Toolbar toolbar = findViewById(R.id.xxx);
toolbar.setNavigationIcon(R.drawable.xxxx4);
setSupportActionBar(toolbar);
If you are using the ActionBar you can use:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.xxx);
You can also change it overriding in your app theme the homeAsUpIndicator attribute:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="homeAsUpIndicator">#drawable/...</item>
</style>
If you are using the Navigation Components, currently there isn't a way to customize the HomeAsUpIndicator icon, and it is an expected behavior with the Up button displayed when you are on a non-root destination.
There is a workaround adding an addOnDestinationChangedListener after your setup method and checking the destination.
Something like:
navController.addOnDestinationChangedListener(
new NavController.OnDestinationChangedListener() {
#Override
public void onDestinationChanged(#NonNull NavController controller, #NonNull NavDestination destination, #Nullable Bundle arguments) {
if (destination.getId() == R.id.nav_xxx) {
//With ActionBar
//getSupportActionBar().setHomeAsUpIndicator(R.drawable.xxxx);
//With a Toolbar
toolbar.setNavigationIcon(R.drawable.xxxx);
}
}
});
If you have created a toolbar and set it as an ActionBar like below
toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
You have two options in setting a custom icon:
Option 1
toolbar?.setNavigationIcon(R.drawable.homeNavigationIcon)
Option 2
supportActionBar?.setHomeAsUpIndicator(R.drawable.homeNavigationIcon)

Change back icon color in toolbar

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>

Is it possible remove home button from actionbar

On my main activity, where there is no way to go back, I'd like to remove the app "home" button in the actionbar.
I think it is confusing for the user, who still can quit the app with the OS back button.
Browsing stackoverflow, I saw a whole lot of people asking this, and not a single answer worked for me. Here is the list :
Removing left arrow from the actionbar in android?
To remove the back arrow button from Action Bar
Remove default home/up button in action mode
how to hide up button in actionbar
How to remove v7 toolbar back button
Setting HomeAsUpEnabled(true) but hide back button actionbar
The answers were usually redondant and could be summarized to following :
ActionBar supportActionBar = getSupportActionBar(); //I target some low API
if(supportActionBar != null) {
supportActionBar.setDisplayShowHomeEnabled(false);
supportActionBar.setDisplayHomeAsUpEnabled(false);
supportActionBar.setHomeButtonEnabled(false);
supportActionBar.setHomeAsUpIndicator(null);
}
I tried every combination of those and nothing worked, whether I try with the default ActionBar or with an XML-declared compat-v7 ToolBar with setSupportActionBar().
I also couldn't see any question adapted for the now recommanded App Bar, and an answer with it would be even greater.
Here is my activity manifest :
<activity
android:name=".activity.WelcomeActivity"
android:logo="#drawable/ic_launcher"
android:label="#string/long_app_name"
android:launchMode="singleTop"/>
So, is it even possible to remove this annoying useless arrow nowadays ?
Create a new style in styles.xml:
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Set this style to your activity in Manifest file. To add the Toolbar, add this to your layout file:
<android.support.v7.widget.Toolbar
android:background="#color/colorPrimary"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
and then in your Java class:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
This will remove the top left arrow.
simply put getSupportActionBar().setDisplayHomeAsUpEnabled(false)in onCreate of the activity.

Android API 15. I have toolbars in my activites but they are not shown in XML

Very Novice Android Programmer here. I have this small program where i have my main activity and whenever the user clicks an option, a new activity will open. I am trying to change the color of the top toolbar/actionbar for each different activity. I have tried changing the color through Java code within the activity class in the onCreate() method,
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getActionBar().setBackgroundDrawable(
getResources().getDrawable(R.drawable.gradient));
but the program would always crash whenever I would switch to my activity.
I have looked in the XML file of my activity and the toolbar/action code does not show up anywhere, but it does in my main activity, app_bar_main.xml. I'm wondering why the actionbar shows up in activies if it does not show up in the XML file for the activity. How to change the color of the actionbar for newely added non main activities?
I'm guessing you're using app theme with action bar. In res/styles.xml change your theme to NoActionBar version:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
Now ActionBar shouldn't be visible. Add your Toolbar XML code to other activity layouts.
Try this, it will work:
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR"));

How can i remove icon and title from ActionBar in Android on startup?

In my android project, I wish to disable icon and the title in ActionBar.
Here is the code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setDisplayShowHomeEnabled(false);
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
But always on startup they appear and after a while they disappear but i don't want them at all. How can i remove them on startup?
call setDisplayShowHomeEnabled(false) and setDisplayShowTitleEnabled(false) on your ActionBar.
actionBar.setDisplayUseLogoEnabled(false); should do it.
Or
For Splashscreen you should use this line in manifest and don't use getActionBar()
<item name="android:windowActionBar">false</item>
and once when Splash Activity is finished in the main Activity use below or nothing
<item name="android:windowActionBar">true</item>
Reffer this ----> click here
You can use below code for that -
<activity android:name="YourActivityname" android:theme="#android:style/Theme.NoTitleBar" />
If you want the full screen of your device you can use below code -
<activity android:name="YourActivityname" android:theme="#android:style/Theme.NoTitleBar.Fullscreen" />
And, also refer Developer's Site
Another method
Do this in your onCreate() method.
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//set content view AFTER ABOVE sequence (to avoid crash)
this.setContentView(R.layout.your_layout_name_here);
this refers to the Activity.
Maybe this answer can help you. It helped me.
<quote>
Best method is to set the display options to useLogo in your theme. E.g.
<style name="AppTheme" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">#style/AppTheme.ActionBar</item>
</style>
<style name="AppTheme.ActionBar" parent="android:Widget.Holo.Light.ActionBar.Solid">
<item name="android:displayOptions">useLogo</item>
</style>
This won't actually show the logo (if set) because showHome is not included.
</quote>
try this:
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
setContentView(R.layout.activity_main);
}
NOTE: The getActionBar() method was added in Honeycomb (API: 11) and later

Categories

Resources