In my activity I use the following code for my two toolbars.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creating The Toolbar and setting it as the Toolbar for the activity
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("My title");
toolbar2 = (Toolbar) findViewById(R.id.tool_bar_bottom);
setSupportActionBar(toolbar2);
...
}
#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;
}
I want it to use menu_main.xml for the top toolbar and menu_bottom for bottom toolbar but for both top and bottom toolbar it uses menu_main.xml.
Can somebody explain how to do it correctly?
As you are using two ToolBars set the menu like this
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("My title");
The above toolbar inflate menu from onCreateOptionsMenu, menu CallBack listener will be onOptionsItemSelected
Now Second ToolBar
toolbar2 = (Toolbar) findViewById(R.id.tool_bar_bottom);
toolbar2.inflateMenu(R.menu.bottom_menu);//changed
//toolbar2 menu items CallBack listener
toolbar2.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem arg0) {
if(arg0.getItemId() == R.id.item_id){
}
return false;
}
});
Related
i want to add items in toolbar but any item that adding, not showing in my app.
please help me to solve this
my app screenshot:
enter image description here
menu_map.xml is here:
<?xml version="1.0"?>
<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.map.myzing.mapzing.MapActivity">
<item android:id="#+id/add_alarm"
android:title="#string/action_addAlarm"
android:orderInCategory="100"
app:showAsAction="never"/>
</menu>
a part of MapActivity:
public class MapActivity extends AppCompatActivity
implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
if (servicesOK()) {
setContentView(R.layout.activity_map);
if (initMap()) {
mLocationClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mLocationClient.connect();
} else {
Toast.makeText(this, "can not be connected!!", Toast.LENGTH_SHORT).show();
}
} else {
setContentView(R.layout.activity_main);
}
}
#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_map, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return true;
}
}
in other activity this toolbar is correct but this activity is not correct and show toolbar without any items or title!!!
please help me
Not sure what you are trying to do but you should get the toolbar view id after setContentView not before it.
Like this:
setContentView(R.layout.my_layout);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
This question already has an answer here:
How can i change Menu Item icon programmatically?
(1 answer)
Closed 7 months ago.
I want to clarify that I talk about a navigation drawer menu and not an action button icon.
I need to change an menu icon programmatically. For this I done next:
private Menu menu;
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
this.menu = menu;
getMenuInflater().inflate(R.menu.navigation_menu, menu);
return super.onCreateOptionsMenu(menu);
}
.... and I try to change icon item with :
menu.getItem(6).setIcon(getResources().getDrawable(R.mipmap.ic_flash_on_black_24dp));
Unfortunately the item remain unchanged. Can help me to solve problem please?
I have observed that getDrawable is deprecated but no ideea how to use the new one.
The answer was very simple:
private Menu menu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);
navigationView.setNavigationItemSelectedListener(this);
menu = navigationView.getMenu();
...
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_light) {
if (canTorch) {
try {
if (isTorchOn) {
turnOffFlashLight();
isTorchOn = false;
menu.getItem(6).setIcon(getResources().getDrawable(R.mipmap.ic_flash_on_black_24dp));
} else {
turnOnFlashLight();
isTorchOn = true;
menu.getItem(6).setIcon(getResources().getDrawable(R.mipmap.ic_flash_off_black_24dp));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
All was about : menu = navigationView.getMenu();
Try to place icon modification code in onPrepareOptionsMenu method:
Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.
I want to set programmatically button back to my toolbar. How can I get a default drawable icon?
What I should write bar.setNavigationIcon( HERE );
If I set app:navigationIcon="?attr/homeAsUpIndicator" to my toolbar, button is shown. But how to do it in code or even maybe how to get this attr in code?
P.S:
I don't want use setSupportActionBar(..).
Tanks.
Try it:
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_launcher);
toolbar.setTitle("");
Thanks
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
You can use SetupToolbar:
private void SetupToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
You have to override the onCreateOptionsMenu()
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
setToolbar();
}
In setToolbar() method you have to set the back icon in the tool bar
private void setToolbar() {
Toolbar ftoolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
if (ftoolbar != null) {
ftoolbar.setNavigationIcon(R.drawable.icon_back);
ftoolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Provide the Action
}
});
}
}
I have a diferent fragments but I want to change the options menu. I want that only the "Solicitudes" have it
The fragment that I want to have this option
This fragment shouldn't have it
I have a menu folder with the main.xml and actually I create another main2.xml that doesn't have this settings option but I don't know how to change this
Here's my code so far:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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);
mTitle = mDrawerTitle = getTitle();
fragment = new History();
The oncreate:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
And the code for the History fragment:
public class History extends Fragment {
public static final String ARG_HISTORY = "arg_history";
public History() {
// Empty constructor required for fragment subclasses
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.history, container, false);
return rootView;
}
In your Fragment you need to say that this Fragment controls the menu.
In your Fragment's onCreate.
setHasOptionsMenu(true);
Now you can implement the following in your Fragment to hide the MenuItem you don't want.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.findItem(R.id.unwanted_item).setVisible(false);
}
Make sure you do the reverse in the Fragment you do want the MenuItem in.
If you want to add a MenuItem that is not in the Menu loaded by your Activity do the following in the onCreateOptionsMenu in your Fragment.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_to_add_to_original, menu);
super.onCreateOptionsMenu(menu, inflater);
}
I'm using the new toolbar from the Appcompat V7 library and I'm making an application with navigation drawer and with fragments.
In some fragments I don't want to show the hamburger icon but the arrow instead... That is fine I did this in this way:
mDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
My question is that: How or where i need to set up the home button lisener or what i need to listen for the "back" button ?
I want to call the main backpressed method and to set back the navigation drawer icon with the hamburger icon..
Add this method in onCreate():
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Then override the onOptionItemSelected() as below:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You can do it like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
toolbar = (Toolbar)findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
setUpNavigationDrawer();
getFragmentManager().addOnBackStackChangedListener(backStackListener); // listen to the backstack of the fragment manager
}
Define the onBackSTackChangedListener:
private FragmentManager.OnBackStackChangedListener backStackListener = new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
setNavIcon();
};
}
Set the icon according to your fragment's backstack:
protected void setNavIcon() {
int backStackEntryCount = getFragmentManager().getBackStackEntryCount();
drawerToggle.setDrawerIndicatorEnabled(backStackEntryCount == 0);
}
Detect when the drawer icon is pressed:
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.isDrawerIndicatorEnabled() && drawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case x:
return true;
default:
return false;
}
}
And handle the up button:
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
This works for me. Good luck.
Not sure if this works in OP's case, but in many cases this is probably the simplest option to implement Back button with the AppCompat Toolbar.
Skip all the setHomeButtonEnabled, setDisplayHomeAsUpEnabled and onOptionsItemSelected stuff, and related issues.
Instead, when initialising the Toolbar, simply set 1) navigation icon and 2) navigation OnClickListener for it:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (enableBackNavigation) {
toolbar.setNavigationIcon(R.drawable.ic_back);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
1- Create Toolbar layout;
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/dark_blue"
android:minHeight="?attr/actionBarSize"
local:popupTheme="#style/ThemeOverlay.AppCompat.Light"
local:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
2- Include this in your layout at the place where you want the toolbar to be.
3- Paste the following code in your activity.(extends ActionBarActivity)
private Toolbar mToolbar;
//For Toolbar (Action bar) start
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mToolbar.setNavigationIcon(R.drawable.ic_back_arrow);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
getSupportActionBar().setTitle("Event Details");
//For Toolbar (Action bar) end
4- change the back click icon to whatever you want.
activate the back button:
getActionBar().setDisplayHomeAsUpEnabled(enable);
and listen for clicks in onBackPressed()
Obviously your activity must extend ActionBarActivity
Simply you can set Navigation icon and make sure you are setting setNavigationOnClickListener() after setting setSupportActionBar(toolbar)
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
in manifest add these lines under the activity you want the back arrow working
android:parentActivityName="Your parent activity name"
Add setDisplayHomeAsUpEnabled(true)
Toolbar toolbar = findViewById(R.id.toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
Handle the back button
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}