Disable navigation icon riple effect in toolbar - android

Not sure what you call the round effect when you click to go back to previous activity in my case (arrow back icon) but i like to disable that.
This is the code that puts it on the toolbar, but i cant seem to find (if there is) an option to disable that round effect when clicked.
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mToolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back));
Like this round effect buttons produce in toolbar's when pressed. I presume a Material design effect.
Thanks

By changing in your theme you can achieve that. Remove <item name="android:background">#color/toolbar</item> from your theme defined in style.
Follow this stack-overflow link.

Use this in your layout
<android.support.v7.widget.Toolbar
android:id="#+id/yourToolbarId"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/yourId"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_width="wrap_content"
app:layout_height="wrap_content"
app:minHeight="?attr/actionBarHeight"
app:minWidth="?attr/actionBarHeight"
android:src="#drawable/ic_arrow_back"/>
</android.support.v7.widget.Toolbar>
and getSupportActionBar().setDisplayShowHomeEnabled(false); in conjunction
and dont use this
mToolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back));
Also you have to manually set your click listener to perform specific action you need when it is pressed.

Use false instead of true
getSupportActionBar().setDisplayShowHomeEnabled(false);

Related

Toolbar doesn't flip the custom navigation icon arrow when switching to right-to-left layouts(Arabic)

I am using right to left support for my app. I have a custom arrow set to toolbar as the navigation icon. When I switch to arabic language, everything aligns to right including the toolbar custom navigation arrow. But the problem is the custom navigation icon is not flipping, that is it should be pointing to the right instead of left(default). This works fine with the default indicator if I dont specify any navigation icon. Have anyone encountered this?
Any solution?
Here is my code for the toolbar
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:elevation="0dp"
app:navigationIcon="#drawable/ic_back"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:logo="#drawable/ic_logo"
android:gravity="center_vertical|start"
>
in Activity
Toolbar toolbar = (Toolbar) findViewById(R.id.tb_venue_filter);
setSupportActionBar(toolbar);
setDisplayHomeAsUpEnabled(true);
If you are on android API 21+, you can just flip your icon by adding android:autoMirrored="true" to your vector icon or layer-list XML icon, this will alleviate the hasle of adding it to every toolbar manually, but if you want to support 21- versions you will have to go with the accepted answer.
This is a feature of the Drawable, not so much the Toolbar.
Try with:
toolbar.getNavigationIcon().setAutoMirrored(true);

Toolbar logo taking up too much space

Im dealing with a strange toolbar behavior in which if I set a logo with
getSupportActionBar().setLogo(R.drawable.logo_toolbar);
The Toolbar components (in my case the logo itself and the title) get disaligned from the standard gravity start behavior.
This is my toolbar:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/grey_900"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_scrollFlags="scroll|enterAlways"/>
The toolbar has a SearchView at the right and no "home button", only title.
Graphic explanation:
1) Behavior without calling setLogo(...)
2) Behavior when calling setLogo(...)
3) What I'm trying to achieve
I've already tried playing with gravity but nothing happened.
Also, please, I know that Toolbar is a ViewGroup and I can customize it, but I'm looking for a clean code, also this should be a default behavior and but it isn't working so I would like to know if I'm doing something wrong.
How can I fix this? Thanks in advance!
getSupportActionBar().setTitle("Home");
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
If you use getSupportActionBar , No need to use toolbar again.

How to create two SearchViews in the Toolbar in Android

I would like to have two search views inside a toolbar in one of my activities: the first for looking up the place of interest and the second for filtering by location.
The searchViews would be on top of each other always expanded. The top one would say "Search" as the hint. The bottom one would say "Nearby" as the hint. To the left would be the home button.
I have come up with two ways that could potentially work but I have encountered problems in both and I don't know how to resolve them.
First Solution (Current)
Here I have a linear layout and inside is a android.support.v7.widget.Toolbar, a view, and followed by a second Toolbar.
This is essentially what I want it to look like but the problem is that changing the hint text in onCreateOptionsMenu changes BOTH hints. I would like the top to say "Search" and the bottom to say "Nearby". It seems that because there are two toolbars, onCreateOptionsMenu affects both of them.
Code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/search_container"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/search_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar">
</android.support.v7.widget.Toolbar>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="72dp"
android:background="#90909090"/>
<android.support.v7.widget.Toolbar
android:id="#+id/search_toolbar2"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
app:contentInsetStart="56dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar">
</android.support.v7.widget.Toolbar>
<include
layout="#layout/toolbar_action_bar_shadow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"/>
</LinearLayout>`
onCreate:
Toolbar toolbar2 = (Toolbar) findViewById(R.id.search_toolbar2);
setSupportActionBar(toolbar2);
Toolbar toolbar = (Toolbar) findViewById(R.id.search_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
OnCreateOptionsMenu:
SearchView searchViewTop = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id
.search_top));
SearchManager searchManager1 = (SearchManager) getActivity().getSystemService(Context
.SEARCH_SERVICE);
searchViewTop.setSearchableInfo(searchManager1.getSearchableInfo(getActivity()
.getComponentName()));
searchViewTop.setIconified(false);
searchViewTop.onActionViewExpanded();
searchViewTop.setQueryHint("Nearby");
Second Solution (Old)
The second method involves putting a searchView instead of a second toolbar in the xml file. Although the second searchview isn't inside the toolbar, it can be made to look like it is. The problem I encountered with this is that the searchView not inside the toolbar looks different from the searchView inside the toolbar and how I would like it. When not inside the toolbar, the hint text is aligned further to the right and not directly underneath the top hint text. Any new text entered inside the searchview would be aligned correctly however. I tried customizing the style of the searchview to align it properly but was unable to find a correct method.
I was wondering if there is a way to correct either of my methods to make it work or if there is a new way to setup these two searchViews. Thanks.
I realized that when you call setSupportActionBar() for more than one toolbar, changes in onCreateOptionsMenu affect all the toolbars added. To solve this problem, I just called setSupportActionBar only on my top toolbar. For the bottom toolbar, I called in onCreate
toolbar2.inflateMenu(R.menu.search2);
SearchView searchViewBottom = (SearchView)findViewById(R.id.search_bottom);
searchViewBottom.onActionViewExpanded();
Then handle all actions inside with setOnMenuItemClickListener.

Is it possible to automatically include Toolbar in window decor?

I have just started migration to material theme using support library 23.1.
I have looked up the guides and all revolve around the following procedure..
1.Use theme without ActionBar provided by decor. I.e.
<style name="MyTheme" parent="Theme.AppCompat">
...
</style>
2.Put a Toolbar widget somewhere in activity layout xml:
<android.support.v7.widget.Toolbar
android:id=”#+id/myToolbar”
android:layout_height=”wrap_content”
android:layout_width=”match_parent”
android:minHeight=”?attr/actionBarSize”
android:background=”?attr/colorPrimary” />
3.In code set this toolbar as an action bar:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blah);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
}
The problem is that some of my activities use FrameLayout as a root layout and as a consequence a toolbar is placed on top of the screen and overlays existing content and therefore hides it.
One workaround would be to create vertically oriented LinearLayout and put Toolbar and FrameLayout as it's children. But, this ads complexity, another level of Views and affects performance.
My first question would be how does mechanism of setSupportActionBar(toolbar) works? Does it takes the toolbar out of its place as set in xml layout and reinserts it at the top of containing layout. Or it just leaves it there where it is?
And the main point, is it possible to use the new Toolbar as decor provided ActionBar? Leave my code unchanged and it will automagically use Toolbar instead of ActionBar? Is it possible to achieve this?
EDIT - UPDATE:
If I leave my code unchanged as if I want to leave the old ActionBar the menu in not instantiated.
EDIT - UPDATE
As far as I could gather there is no way to persuade android to create Toolbar instead of action bar. This sadly means refactoring of all activities and adding another outer enclosing Layout thereby needlessly increasing complexity.
First question: How does the setSupportActionBar(toolbar) works?
What the above statement does is make the toolbar act like an ActionBar. By default the toolbar does not have action bar capabilities, and to make the toolbar act like an action bar, you need this statement. The followin is the official documentation of this method.
Set a Toolbar to act as the ActionBar for this Activity window.
When set to a non-null value the getActionBar() method will return an
ActionBar object that can be used to control the given toolbar as if
it were a traditional window decor action bar. The toolbar's menu will
be populated with the Activity's options menu and the navigation
button will be wired through the standard home menu select action.
setSupportActionBar replaces the decor action bar with the toolbar that you are supplying as a view in the activity. This means that unlike action bar, toolbar will live as a view in your activity. It doesn't take the toolbar out of the layout and reinserts it at the top of containing layout. This means that you can have the toolbar anywhere you want in the screen, not necessarily at the top.
Second Question: Leave my code unchanged and it will automagically use Toolbar instead of ActionBar?
No, you can't do this. Toolbar has to be present in your layout, (either via xml or inflated via code). This is the whole purpose of the toolbar. With action bar you don't have this control. Toolbar allows you to modify it as much as possible and you can a different one in different activity. So how do you do it? What's the best practice?
Well, it's recommended that all activities have an accompanying fragment. So you could have a common layout for all activities (like the one below). Now, you could just inflate the fragment in the FrameLayout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<FrameLayout
android:id="#+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
of course you don't have to add the toolBar if you don't want to (but you will lost some of the cool navigation patterns the toolbar is involved at).
Just make your activity extend a theme with toolbar and you will find it there where its supposed to be.
<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>
Then in your activity you will be able to get ActionBar instance as usual:
ActionBar actionBar = getSupportActionBar();

Rotate icon ic_drawer in NavigationDrawer

Does anyone know how to turn 180 degrees icon ic_drawer in NavigationDrawer after clicking on the drop-down menu?
Currently, after expanding the Menu icon animates to the left - I would like to animating to the right (or the other way around than it is).
Sorry for my English.
you have to use Toolbar which is introduced in support 21+
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mtoolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primary" />
also add dependencies in your project .
The last thing to use ActionBarDrawerToggle in your class.
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(MainActivity.this,
mDrawerLayout,mToolbar,"Drawer opentitle","Drawer closetitle");
Constructor's parameter need toolbar object ,so you have to use Toolbar.
check this out ToolBar

Categories

Resources