How to add multiple icons on action bar in android? - android

I want to add 2 or 3 icons on action Bar in android app. I already took the empty activity and added the toolbar. I also set the Icon at left side. Now i want to add another two icons on it. But there is no Menu folder in my project directory structure. So any one tell me how i can do this all with proper guidelines?
My code is here :
My activity file
public class ActionBarActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_action_bar);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.drawable.left_nav);
getSupportActionBar().setTitle("");
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
my .xml file
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:fitsSystemWindows="true"
tools:context="firstapp.vaibhav.com.firstapp.ActionBarActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
Screen shot of my project directory structure

1. Create a menu folder in your existing resource res folder. (Ex. .../res/menu)
2. Create a main.xml file in menu folder. (Ex. .../res/menu/main.xml)
main.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">
<item
android:id="#+id/action_item_one"
android:title="Camera"
android:icon="#drawable/ic_menu_camera"
app:showAsAction="always" />
<item
android:id="#+id/action_item_two"
android:title="Send"
android:icon="#drawable/ic_menu_send"
app:showAsAction="always" />
</menu>
3. In your activity, Override onCreateOptionsMenu() and onOptionsItemSelected() to work with option menus.
ActionBarActivity.java
#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;
}
#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_camera) {
// Do something
return true;
}
if (id == R.id.action_send) {
// Do something
return true;
}
return super.onOptionsItemSelected(item);
}
OUTPUT
Hope this will help~

create menu.xml with item like this
<?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" />-->
<item
android:id="#+id/action_refresh"
android:orderInCategory="100"
app:showAsAction="always"
android:icon="#drawable/ic_action_autorenew"
android:title="Search"/>
<item
android:id="#+id/action_search"
android:orderInCategory="100"
app:showAsAction="always"
android:icon="#drawable/ic_action_search"
android:title="Search"/>
</menu>
and use it in activity
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// MenuInflater inflater1 = getActivity().getMenuInflater();
inflater.inflate(R.menu.cartmenu, menu);
return ;
}

in your res/menu/menu_main.xml:
add
<?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/icon_id"
android:visible="true"
android:title="#string/icon_name"
android:icon="#drawable/your_image"
app:showAsAction="always">
</item>
</menu>
in your activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
// return true so that the menu pop up is opened
return true;
}
To access your menu item in activity add:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.your_item_id) {
// your code
return true;
}
return super.onOptionsItemSelected(item);
}

You can use showAsAction option found at item in menu resource file.
1) If you want to add popup menu then write app:showAsAction="never"
2) If you want to add icons as an action (multiple icons in actionbar) then write app:showAsAction="always"

Related

Add right margin to the options menu sub-items

Currently, my options menu is aligned to the end of the screen. I want to give it a margin, something like android:layout_marginEnd = "20dp". How can I achieve this?
My options menu:
<?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=".MainActivity">
<item
android:id="#+id/menu_items"
android:icon="#drawable/menu_icon"
app:showAsAction="always">
<menu>
<group android:id="#+id/item1">
<item
android:id="#+id/download"
android:icon="#drawable/download_icon"
android:title="Download"></item>
</group>
<group android:id="#+id/item2">
<item
android:id="#+id/invite"
android:icon="#drawable/invite_icon"
android:title="Invite"></item>
</group>
</menu>
</item>
</menu>
My Custom Toolbar:
<androidx.appcompat.widget.Toolbar
android:id="#+id/menu_toolbar"
app:popupTheme="#style/ThemeOverlay.MyTheme"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_below="#id/greetText"> <!--assume it comes somewhere in the centre due to this-->
</androidx.appcompat.widget.Toolbar>
My Theme for menu items:
<style name="ThemeOverlay.MyTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="android:textColor">#color/dimGray</item>
<item name="android:textSize">14sp</item>
<item name="android:layout_marginEnd">20dp</item> <!-- doesn't work-->
<item name="android:fontFamily">#font/quicksand_medium</item>
<item name="android:background">#drawable/options_menu_background</item>
</style>
Here is the image of my problem. It shows how the end of the menu and the screen are aligned. I want space/margin between these two.
This is an interesting problem. I tried with many things found on the internet, but nothing works. I tried setting the actionOverflowMenuStyle with dropDownHorizontalOffset attribute which does not work either. Finally, I ended up getting a PopupMenu which works just fine. Here is the implementation.
Get your initial menu shorter with a single item which will open up the popup menu as a submenu.
<?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/menu_items"
android:icon="#drawable/ic_close_swipe"
android:title="#string/app_name"
app:showAsAction="always" />
</menu>
Then create another menu, in your /res/menu/ folder, named popup_menu.xml like the following.
<?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/menu_items"
android:icon="#drawable/ic_close_swipe"
android:title="#string/app_name"
app:showAsAction="always" />
</menu>
Now in your MainActivity, just add the following functions to handle the click action of the menu button and the popup menu.
public class MainActivity extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = findViewById(R.id.menu_toolbar);
setSupportActionBar(myToolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_items:
showPopupMenu();
return true;
default:
return false;
}
}
public void showPopupMenu() {
PopupMenu popup = new PopupMenu(this, findViewById(R.id.menu_items));
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.popup_menu);
popup.setGravity(Gravity.END);
Object menuHelper;
Class[] argTypes;
try {
Field fMenuHelper = PopupMenu.class.getDeclaredField("mPopup");
fMenuHelper.setAccessible(true);
menuHelper = fMenuHelper.get(popup);
argTypes = new Class[]{boolean.class};
menuHelper.getClass().getDeclaredMethod("setForceShowIcon", argTypes).invoke(menuHelper, true);
} catch (Exception e) {
e.printStackTrace();
}
popup.show();
}
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.invite:
Toast.makeText(this, "Invite", Toast.LENGTH_LONG).show();
return true;
case R.id.download:
Toast.makeText(this, "Download", Toast.LENGTH_LONG).show();
return true;
default:
return false;
}
}
}
I put the working code in this Github Branch. You might consider cloning from that branch and run the application to check if that suffices your expectation.
Hope that helps!

I want to call a function of MainActivity from Drawer button

Is it possible to call a function of Activity from drawer?
Let say: Main Activity is opened. There is a function displayImage() in it. I want to call it from drawer.
**Please go through the bellow code snippet**
drawer.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/u_name"
android:icon="#drawable/lead_leader"
android:title="User name" />
<item
android:id="#+id/logout"
android:icon="#drawable/logout"
android:title="Logout" />
</group>
</menu>
**after that add bellow code in your Mainactivity.java file**
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.lead_profile, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.logout) {
//call your other method according navigation drawer item with other id in a different if block.
///call your function here
}
return super.onOptionsItemSelected(item);
}

How to add overflow menu to Toolbar?

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

Android : Search Icon not showing on Action Bar

As the title says I am trying to add an icon (for search purposes) in my Action Bar but all I get is the item's title in the three dot menu. Here is the code I use.
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=".MainActivity">
<item android:id="#+id/action_search"
android:icon="#drawable/search"
android:orderInCategory="100"
android:title="action_search"
app:showAsAction="always"/>
</menu>
The way I inflate the menu :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#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();
if (id == R.id.action_search) {
return true;
}
return super.onOptionsItemSelected(item);
}
Do you guys know why this happens ?
PS: I have holo.light as default theme for my app.
You are using some things for the native action bar (e.g., inheriting from FragmentActivity) and some things for the appcompat-v7 action bar backport (e.g., app:showAsAction).
Change menu_main.xml to this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/action_search"
android:icon="#drawable/search"
android:orderInCategory="100"
android:showAsAction="always"
android:title="action_search" />
</menu>
Use the following code this will work:
<item
android:id="#+id/action_search"
android:icon="#drawable/search"
android:showAsAction="always"
android:title="action_search" />
in your code you are using app:showAsAction="always" this is wrong use android:showAsAction="always"

actionbar menu item onclick?

I have an action bar that puts everything in a menu in the top right, which the user clicks and the menu options open up.
I inflate the action bar menu with this on each activity I use it:
#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;
}
And my xml for main2.xml is:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_searchHome"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Seach"/>
</menu>
My question is do I put an onclick in the item in the xml and if so where do I put the onclick method it calls? Do I need to put it in every activity I launch this action bar in?
If you add an onClick attribute on your menu item like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_searchHome"
android:orderInCategory="100"
android:showAsAction="never"
android:onClick="doThis"
android:title="Seach"/>
</menu>
Then in your activity:
public void doThis(MenuItem item){
Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
}
Note:
ActionBarSherlock is deprecated. Unless you are developing an app for Android 4.0 or older, please don't use it. But if you are using the library, you will have to import
import com.actionbarsherlock.view.MenuItem;
and not
import com.android.view.MenuItem;
In addition, you could do something like this: ActionBar Sherlock Menu Item OnClick
which #adneal mentions.
In my opinion
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onCreateDialog(getTaskId());
}
});
}
<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=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/add_text_id" android:title="Add"
android:icon="#drawable/ic_add_btn"
android:orderInCategory="100" app:showAsAction="ifRoom" />

Categories

Resources