Remove back button from SearchView in toolbar AppCompat - android

How can I remove the back button icon when search view shows up in Toolbar (AppCompat)?
toolbar = (Toolbar) findViewById(R.id.tool_bar);
// Set an OnMenuItemClickListener to handle menu item clicks
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// Handle the menu item
return true;
}
});
// Inflate a menu to be displayed in the toolbar
toolbar.inflateMenu(R.menu.menu_main);
toolbar.setTitle("");
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
// this works for normal back button but not for one appears on tapping SearchView
actionBar.setDisplayHomeAsUpEnabled(false);

Use:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
toolbar.setNavigationIcon(null);
OR
toolbar.setNavigationIcon(getResources().getColor(android.R.color.transparent));

getSupportActionBar().setDisplayHomeAsUpEnabled(false);

There is no way to remove the back arrow from a SearchView.
you can try finding it yourself
in the class (of the support.v7 lib or the main Android project) it looks like this:
mCloseButton = (ImageView) findViewById(R.id.search_close_btn);
But it's a private member and the visibility changes upon text input

Related

Add and Remove views from Toolbar depending on Fragment Displayed

I have an application, that at the moment only has 2 fragments. Fragment 1, this has the nav drawer and the title.
Fragment 2 requires a custom view as adding menu items won't work as I need alignment. So I add the view as follows:
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT,
Gravity.LEFT);
//Remove nav drawer "hamburger"
mMainActivity.mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);
//Remove title from Toolbar
bar.setDisplayShowTitleEnabled(false);
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View postToolBar = layoutInflater.inflate(R.layout.upload_content_toolbar, null);
mMainActivity.mToolBar.addView(postToolBar, params);
That is fine and it displays correctly. However, when I want to return to the previous fragment Fragment 1, I then call mMainActivity.mToolBar.removeView(postToolBar); I call this on return to Fragment 1 as the user can navigate either by the back button or by button in the postToolBar. However, the view is still in place. I can't get rid of it. I have now tried setting the visibility to GONE but that won't work either.
This was pretty simple with the Action Bar, however things seem to have gotten a bit complicated with the Tool bar.
I must add that in each of my two fragments I extend a BaseFragment in which I declare the toolbar view.
Can any one help or send me in the direction of a tutorial?
This is how i achieved it at this time:
in activity onCreateView();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowCustomEnabled(true); // enable overriding the default toolbar_home layout
getSupportActionBar().setDisplayShowTitleEnabled(false); // disable the default title element here (for centered title)
getSupportActionBar().setDisplayShowHomeEnabled(false);
cutomToolbarView=getLayoutInflater().inflate(R.layout.custom_toolbar_home, null);
getSupportActionBar().setCustomView(cutomToolbarView);
}
and here is two simple method to do the magic
public void setToolbarTitleEnabled(String title) {
getSupportActionBar().setDisplayShowCustomEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle(title);
}
public void setCustomToolbarEnabled() {
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
then simply when you can change actionbar like these:
when to select HomeFragment.java or whatever in your case
setToolbarTitleEnabled(CURRENT_TAG);
and when to select other fragment:
setCustomToolbarEnabled();

How to open Side bar on icon click in Android?

I have implemented Hamburger bar with App toolbar and both of them are working fine. Following is the snapshot of toolbar and hamburgerbar:
Hamburger bar
I can open this bar by sliding it but I also want to make it open by clicking on drawable icon (right top corner icon). How can i do that?
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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), toolbar);
drawerFragment.setDrawerListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_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();
return super.onOptionsItemSelected(item);
}
I don't think so that I need to do some changes in layout files. What do I have to add in MainActivity file to make it possible?
I am newbie in Android code. Any help will be appreciable.
Using the Toolbar component should be fairly easy to achieve this by using a similar code to this:
Toolbar toolbar = (Toolbar) findViewById(R.id.home_toolbar);
toolbar.inflateMenu(R.menu.menu_home);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.action_settings) {
mDrawerLayout.openDrawer(Gravity.RIGHT); // where mDrawerLayout is your android.support.v4.widget.DrawerLayout in your activity's xml layout.
}
return false;
}
});
EDIT:
The key component here is the menu_home.xml file which goes to your res/menu folder. You can add your desired menu item there, customize it's icon and even more, add as many items as you'd like to have on the right side of the toolbar(Obviously handle the openDrawer() method on whichever menu item you need - the recommended one is the rightmost though).
Use the openDrawer() method.
private DrawerLayout mDrawerLayout;
...
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
...
mDrawerLayout.openDrawer(Gravity.END); // or whatever gravity the of the drawer you want to open
Use Activity's onOptionsItemSelected(MenuItem menuItem) method:
First of all, keep the reference to your DrawerLayout in a class field:
DrawerLayout drawerLayout;
Somewhere in onCreate put this:
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout)
And implement the method:
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
// if you want the default back/home hamburger menu item just put android.R.id.home instead
if (menuItem.getItemId() == R.drawable.icon_navigation) {
drawerLayout.openDrawer(GravityCompat.END);
}
return super.onOptionsItemSelected(menuItem);
}

How do I get the default Android home button in my toolbar?

On the top left hand side of my toolbar I want a default Android back arrow (in white) that takes you to the previous page. For some reason it doesn't show. Here is what I'm doing
Toolbar toolbar = (Toolbar) findViewById(R.id.sign_up_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#3f9845")));
getSupportActionBar().setDisplayShowTitleEnabled(false);
Am I missing a line or something? Thanks for any help.
Please make sure you have extends ActionBarActivity.
basically given code are used to show back arrow and it running perfectly into my phone
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
and below code is used to perform the action on through back button
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}

Gmail tablet like Actionbar items

I'm trying to build an app with a split actionbar/toolbar like in the Gmail app.
Is there any view element for this behaviour or do I have to write such a toolbar myself?
The search icon is moving with the master fragment when opening the slidingDrawer.
To accomplish this you can add one of the new Toolbar widgets to each of your fragments layouts. The new Toolbar class was designed to be much more flexible than a traditional Actionbar and will work well in this split design. This post is a good overview for implementing a standalone Toolbar. For posterity's sake I've included the sample code for it below.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blah);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
// Set an OnMenuItemClickListener to handle menu item clicks
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// Handle the menu item
return true;
}
});
// Inflate a menu to be displayed in the toolbar
toolbar.inflateMenu(R.menu.your_toolbar_menu);
}

Toolbar.inflateMenu seems to do nothing

Im currently messing arround with the new AppCompat library bringing material design to older devices.
Setting a toolbar as actionbar works fine for me, but the toolbar seems to not do anything on calling inflateMenu(int resId). From the docs, i thought this is to replace getMenuInflater().inflate(int resId) called from onCreateOptionsMenu.
If I do the latter, the menu items are correctly inflated and added to the toolbar, but inflateMenu seems to to nothing.
What am I missing?
Activity Code:
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.main); // this does nothing at all
setSupportActionBar(toolbar);
}
// this works
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Thanks in advance!
If you are calling setSupportActionBar() you don't need to use toolbar.inflateMenu() because the Toolbar is acting as your ActionBar. All menu related callbacks are via the default ones. The only time you need to call toolbar.inflateMenu() is when you are using the Toolbar as a standalone widget. In this case you will also have to handle menu item click events via
toolbar.setOnMenuItemClickListener(
new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// Handle menu item click event
return true;
}
});

Categories

Resources