Add action to action bar panel - android

I want to add an action to the action bar, but it appears in my action bar as a drop down list.
How can I add button to the action bar?
My code is:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="Add"
showAsAction="ifRoom"/>
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}

If you are using android.app.Activity simply change showAsAction="always" to "android:showAsAction="always".
If you are using android.support.v7.app.Activity change the code as follows:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="Add"
app:showAsAction="ifRoom"/>
</menu>

You just simply add the one more item into menu and inflate that menu into your activity ....
like below you will add your button and access that button into your activity...
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/refresh"
android:icon="#android:drawable/stat_notify_sync"
showAsAction="ifRoom"/>
<item
android:id="#+id/action_bar_button_cancel"
android:focusableInTouchMode="true"
android:icon="#android:drawable/ic_menu_close_clear_cancel"
showAsAction="ifRoom"/>
</menu>
Inflating the menu like below.....
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
Accessing method in action bar item is....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
//logic
return true;
case R.id.action_bar_button_cancel:
//logic
return true;
default:
return super.onOptionsItemSelected(item);
}
}
that's if you have any doubt comment

Related

Get the SearchView from Toolbar

Is it possible to get the SearchView in the ActionBar by just having the Toolbar object? .
I know that you can get it through the onCreateOptionsMenu(), but in this case I am just trying to see if via a toolbar.findViewById I can get it
Yes.It possible to show the SearchView.For this you need to add menu feature such as :
#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 menuItem) {
switch (menuItem.getItemId()) {
default:
return super.onOptionsItemSelected(menuItem);
}
}
You need to implement the below code into menu_main menu(it must be stay into menu folder).
<?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:icon="#drawable/abc_ic_search_api_mtrl_alpha"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView"/></menu>
Hope it can help to you thanks.

Action Item is not displaying on action bar in all other activity except main activity (Android)

This is my menu xml file...
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action"
android:icon="#drawable/ic_action_search_dark"
yourapp:showAsAction="always" /></menu>
here is my java code...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search, menu);
return true;
}

android action bar doesnt appear

I am not sure why the action bar does not appear on my main menu, i already cleaned my project but it still doesn't work.
the main xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item android:id="#+id/mainmenu"
android:title="Main Menu"
android:orderInCategory="1"
android:showAsAction="always"/>
<item android:id="#+id/play_actionbar"
android:title="Play"
android:orderInCategory="2"
android:showAsAction="always"/>
<item android:id="#+id/admin_actionbar"
android:title="Admin"
android:orderInCategory="3"
android:showAsAction="always"/>
<item android:id="#+id/video_actiobar"
android:title="Video"
android:orderInCategory="4"
android:showAsAction="always"/>
the java code
#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;
}
try this
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// TODO Auto-generated method stub
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, (Menu) menu);
return super.onCreateOptionsMenu(menu);
}
change the code to this way and check it works
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
check the link for more how it works (http://www.androidhive.info/2013/11/android-working-with-action-bar/)
In your Manifest, for your activity, make sure you declare it has Actionbar:
<activity
android:name="com.test.activities.MyActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar" >
You can use ActionBarsherlock or AppCompat for supporting actionbar for android versions below 3.0 .
For using ABS(actionbarsherlock)
You can extend your activity by SherlockActivity
and change your method to
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// TODO Auto-generated method stub
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, (Menu) menu);
return super.onCreateOptionsMenu(menu);
}
as answered by #Amer Hadi
check here for more details ActionBar
try 100% working on 2.2 to 4.4. just add support libraries.
activity_main_actions.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" >
<!-- Refresh -->
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:title="#string/action_refresh"
app:showAsAction="always"/>
<!-- Help -->
<item
android:id="#+id/action_help"
android:icon="#drawable/ic_action_help"
android:title="#string/action_help"
app:showAsAction="never"/>
<!-- Check updates -->
<item
android:id="#+id/action_check_updates"
android:icon="#drawable/ic_action_refresh"
android:title="#string/action_check_updates"
app:showAsAction="never"/>
code in activity:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}

Adding items to actionbar

I want to use action bar in my application. So far I was able to add the action bar from the support library. Now I want to add items to my action bar. I want the icons of the items to be displayed in my action bar, so I did the following:
first I created menu.xml file
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_locate"
android:icon="#drawable/ic_action_location_found"
android:title="#string/locate"
android:showAsAction="always" />
<!-- Settings, should always be in the overflow -->
and I added these functions to my mainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_locate:
Toast.makeText(this,"locate is selected",Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
When I run my application I only see the title of the action bar, the locate icon is not there. When I touch the options button on my phone a list with only locate in it appears. What I need is to have locate icon appearing on the right corner of the action bar. Can anybody please tell me what I'm doing wrong and why its not appearing on the right corner of the action bar??
You need to add a namespace
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
Then
yourapp:showAsAction="always"
Edit:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_locate"
android:icon="#drawable/ic_action_location_found"
android:title="#string/locate"
yourapp:showAsAction="always" />
Quoting docs
Using XML attributes from the support library
Notice that the showAsAction attribute above uses a custom namespace
defined in the tag. This is necessary when using any XML
attributes defined by the support library, because these attributes do
not exist in the Android framework on older devices. So you must use
your own namespace as a prefix for all attributes defined by the
support library.
Try This :-
Menu Xml:
<item
android:id="#+id/action_location_found"
android:clickable="true"
android:icon="#drawable/more_btn"
android:showAsAction="always"
android:title="action_location_found">
<menu>
<item
android:id="#+id/action_user_profile"
android:orderInCategory="1"
android:showAsAction="never"
android:title="User Profile">
</item>
<item
android:id="#+id/action_settings"
android:orderInCategory="2"
android:showAsAction="never"
android:title="Settings">
</item>
</menu>
</item>
In your Activity:-
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action buttons
switch (item.getItemId()) {
case R.id.action_user_profile:
Toast.makeText(getApplicationContext(), "User Profile",
Toast.LENGTH_LONG).show();
break;
case R.id.action_settings:
Toast.makeText(getApplicationContext(), "Setting",
Toast.LENGTH_LONG).show();
break;
default:
return true;
}
return super.onOptionsItemSelected(item);
}

I want to add buttons next to the option button in Tablets

I want to add some buttons next to the system option menu , how can I do that?
try
action bar
Icon of Action bar
#Override
public boolean onCreateOptionsMenu(Menu menu) {
android.view.MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
Menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/save_button"
android:title="i"
android:showAsAction="always" />
</menu>

Categories

Resources