I have a toolbar that has only the menu icon, and it is setted programmatically, like this:
myToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(myToolbar);
myToolbar.setTitleTextColor(Color.BLACK);
ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.menu);
ab.setDisplayHomeAsUpEnabled(true);
Now, i would like to have another icon in the toolbar, and associate to it my src image and onClick Method. As you can see I don't have a xml menu file, and i also don't want to set a default android icon, so how can I perform this?
The main purpose of Toolbar is using custom layout!
You can add whatever UI elements, like button, to your Toolbar and find them in code.
You can set a CustomView to the ActionBar by supplying a layout file or View object. In that custom view you can do whatever you want.
Here is how you can set custom view to the action bar
protected void setCustomActionBarTitle(String title) {
ActionBar actionBar = getActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.layout_action_bar_title);
TextView titleView = (TextView) actionBar.getCustomView().findViewById(R.id.action_bar_title);
titleView.setText(title);
ImageView customIconImageView = (ImageView) actionBar.getCustomView().findViewById(R.id.custom_icon);
customIconImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// call my method on the activity
}
});
ImageView imageView = (ImageView) actionBar.getCustomView().findViewById(R.id.up_icon);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
}
You're going to have to use a separate xml file to setup your menu and define any extra buttons (item) in there. Here is sample menu file:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- "Mark Favorite", should appear as action button if possible -->
<item
android:id="#+id/action_favorite"
android:icon="#drawable/ic_favorite_black_48dp"
android:title="#string/action_favorite"
app:showAsAction="ifRoom"/>
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
Add the icon which you want in your menu --> menu_main.xml
<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=".ui.MainActivity">
<item
android:id="#+id/action_scan"
android:icon="#drawable/camera_icon"
android:orderInCategory="100"
android:title="#string/action_scan"
android:visible="true"
app:showAsAction="always" />
and in your activity
#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;
}
This code is work for me,
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(R.string.app_name);
toolbar.setTitleTextColor(getResources().getColor(R.color.colorWhite));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
Use this to add your icons into your tool bar. Override these methods and use the code in your project..
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
getMenuInflater().inflate(R.menu.menu_dashboard, menu);
menu.getItem(0).setIcon(getResources().getDrawable(R.drawable.ic_about));
menu.getItem(1).setIcon(getResources().getDrawable(R.drawable.street_view_icon));
return true;
}
//Notification Icon
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_notify_found:
Intent aboutIntent=new Intent(DashboardActivity.this, HomeAboutActivity.class);
startActivity(aboutIntent);
case R.id.action_notify_found1:
Intent ARStreetviewintent = new Intent(DashboardActivity.this, ARStreetviewActivity.class);
startActivity(new Intent(DashboardActivity.this, StreetviewActivity.class));
default:
return super.onOptionsItemSelected(item);
}
}
Menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Notification Found -->
<item android:id="#+id/action_notify_found"
android:icon="#drawable/about"
android:title="About"
app:showAsAction="ifRoom"
/>
<!-- Notification Found -->
<item android:id="#+id/action_notify_found1"
android:icon="#drawable/street_view_icon"
android:title="StreetView"
app:showAsAction="ifRoom"
/>
Related
I'm trying to use Android ActionBar in my app, and have an option that's hidden away in the overflow menu.
There's a lot of documentation out there, but it's confusing because most of it is only relevant to very old versions of Android, and when you try applying the same concepts, they don't work anymore or work differently.
This is in my Activity layout
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:titleTextColor="#android:color/white"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
And this is in my Activity's onCreate() method
// sets up activity toolbar
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
myToolbar.showOverflowMenu();
myToolbar.setTitleTextColor(R.color.lightPrimaryText);
I've also tried inflating a menu xml file from the onCreateOptionsMenu(), but that also didn't give me the results I wanted.
Define a Menu for your Toolbar in the res/menu resource folder, for example:
toolbar_menu.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=".activity.MainActivity">
<item
android:id="#+id/action_sign_out"
android:title="#string/toolbar_sign_out"
app:showAsAction="never"/>
</menu>
Setting app:showAsAction="never" ensures that this MenuItem will not be shown in the Toolbar, but placed in the overflow menu instead.
The theme of your Activity should be (or derive from) one of the NoActionBar themes (Theme.AppCompat.NoActionBar for example, or Theme.MaterialComponents.NoActionBar if you're using Material Components).
In your Activity, set up your Toolbar:
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
And override onCreateOptionsMenu() to inflate your previously defined menu resource:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return true;
}
You can override onOptionsItemSelected() to define the onClick behaviour of your MenuItem(s):
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_sign_out: {
// do your sign-out stuff
break;
}
// case blocks for other MenuItems (if any)
}
return true;
}
in manifest file declare
android:theme="#style/AppTheme.NoActionBar"
like this :
<activity
android:name=".ActivityName"
android:label="#string/label"
android:theme="#style/AppTheme.NoActionBar" />
and add this to your style :
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
and call this in Activity onCreate() :
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
override this method in activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.product_list, menu);
//U can find item set icon and stuff...
MenuItem item= menu.findItem(R.id.action_search);
return true;
}
and declare your menu like this for overflow menu:
<?xml version="1.0" encoding="utf-8"?>
<menu >
<group>
<item
android:id="#+id/sign_out"
android:title="#string/sign_out" />
<item
android:id="#+id/about"
android:title="#string/about" />
</group>
</menu>
and for handle item selection call this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.sign_out:
//do stuff
break;
}
return super.onOptionsItemSelected(item);
}
done :)
Simple do This copy this code on your MainActivet`
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main2, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Group gp=(Group)findViewById(R.id.order);
return true;
}
return super.onOptionsItemSelected(item);
}`
Now Make Directory file for menu name for this go on Android_Studio->app Folder->Right_Click->New->Directory-> Enter name menu now Create a xml file in there with menu2.xml name
and past this code on menu2.xml file
<?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">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"
android:icon="#android:drawable/ic_input_add"
/>
</menu>
if any Query Please text me
I have simple question.
How I can change Toolbar's color/or theme to white when clicked on Search icon?
I want Toolbar to be White(with back arrow colored black/grey) whenever search icon is clicked. I have added SearcView (android.support.v7.widget.SearchView) in MenuItem
This
To
And then back to original Toolbar color when Back button is clicked.
In Activity/Fragment code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_menu, menu);
MenuItem searchMenuItem = menu.findItem(R.id.search);
if (searchMenuItem == null) {
return true;
}
searchView = (SearchView) searchMenuItem.getActionView();
MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Set styles for expanded state here
if (getSupportActionBar() != null) {
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.RED));
}
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Set styles for collapsed state here
if (getSupportActionBar() != null) {
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.BLUE));
}
return true;
}
});
return true;
}
And actual menu's xml is:
<?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">
<item android:id="#+id/search"
android:title="#string/search_title"
android:icon="#drawable/ic_menu_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
To change the color of back/X-buttons for expanded view add this into your styles:
<item name="colorControlNormal">#color/my_great_color</item>
To make change of the color smoother - you can animate it: Animate change of view background color on Android
I hope, it helps
I'm using badge layout in one menu item
here is some menuItems
<item
android:id="#+id/action_rdvmed"
android:title="#string/rdv"
android:icon="#drawable/ic_action_action_event"
android:showAsAction="always" />
<item
android:id="#+id/action_msg"
android:title="#string/msg"
android:actionLayout="#layout/badge_layout" <!--this one causes the problem-->
android:showAsAction="always" />
<item
android:id="#+id/action_actualiser"
android:title="#string/actualiser"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="always" />
i'm getting a null pointer exception in my onCreateOptionMenu
#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_activity_med, menu);
this.MenuMed = menu;
RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.action_msg).getActionView();
mCounter = (TextView) badgeLayout.findViewById(R.id.hotlist_hot); //the error in this line
imgMessage = (ImageView) badgeLayout.findViewById(R.id.hotlist_bell);
badgeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
listMessages.clear();
msg();
}
});
return true;
}
and here is the part of muy onCreate methode whare i'm using toolbar
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_med);
Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
thanks in advance.
I believe you're facing the same problem as one user faced here.
The trick is you need to change your android:actionLayout to app:actionLayout.
So, change your
<item
android:id="#+id/action_msg"
android:title="#string/msg"
android:actionLayout="#layout/badge_layout"
android:showAsAction="always" />
to,
<item
android:id="#+id/action_msg"
android:title="#string/msg"
app:actionLayout="#layout/badge_layout"
android:showAsAction="always" />
I´m trying to add in my ActionBar my app icon, but I was reading on Google Developers and I can get the solution. I´m doing this on my ActivityMain:
actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setIcon(R.drawable.ic_launcher);
But it doesn´t work.
This is my first day with Android and I just want make an ActionBar with the main icon on the left and an icon search.
Thank You.
With API21 you should use the new Toolbar class.
Put the Toolbar in your layout:
<android.support.v7.widget.Toolbar
android:id=”#+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="attr/actionBarSize"
android:background="?attr/colorPrimary" />
Then in your code (onCreate in your Activity for example):
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_myNavigationIcon);
You can find more info in the official post.
Add:
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setLogo(R.drawable.ic_launcher)
You should be changing the logo, which is by default the same as the launcher icon.
You should do the following steps to implement ActionBar :
1 - Extend ActionBarActivity like this :
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Notice to import this :
import android.support.v7.app.ActionBarActivity;
2 - Add lines below into onCreate :
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
3- Create your xml menu under res/menu/your_menu.xml
somthing like this :
your_menu.xml
<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">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="ifRoom"/>
</menu>
4- Inflate the menu to the action bar, and handle the action bar item clicks :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.your_menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Hope this help you!!!
I have seen this question asked many times, however, none of those solutions have helped me. My problem is that I have an ActionBar menu and I want its items to always be displayed on the ActionBar instead of the drop down menu.
I have tried actions suchs as "ifRoom", "always", etc. and they still show only with text on the drop down menu.
Menu: devotional_fragment_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_previous"
android:icon="#drawable/ic_action_previous_item"
android:title="#string/previous_action"
app:showAsAction="always"/>
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_go_to_today"
android:title="#string/date_action"
app:showAsAction="always"/>
<item android:id="#+id/action_next"
android:icon="#drawable/ic_action_next_item"
android:title="#string/next_action"
app:showAsAction="always"/>
</menu>
On my fragment I have:
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// action bar
inflater.inflate(R.menu.devotional_fragment_actions, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection of action bar
switch (item.getItemId()) {
case R.id.action_search:
showDatePickerDialog();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The menu works fine but is not displaying as I want it. Thanks
In each item use android namespace instead of app. You can even remove this namespace declaration xmlns:app="http://schemas.android.com/apk/res-auto".
Try set it like in code below:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_previous"
android:icon="#drawable/ic_action_previous_item"
android:title="#string/previous_action"
android:showAsAction="always"/>
</menu>