I want to create an add button in the action bar but it doesn't seem to appear when i run thee code.
Here is my main_activity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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_activity_actions, 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_add) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
This is my main_activity_actions.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_add"
android:icon="#drawable/ic_add"
android:title="Add"
android:showAsAction="always"
/>
</menu>
Please Help
I cant figure out what's wrong !
Since your Activity extends ActionBarActivity you would be using AppCompat from support library.
So change to
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_add"
android:icon="#drawable/ic_add"
android:title="Add"
yourapp:showAsAction="always"
/>
...
</menu>
Quoting docs
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.
Change your menu_xml to this :-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_add"
android:clickable="true"
android:icon="#drawable/ic_add"
android:showAsAction="always"
android:title="action_location_found">
</menu>
Replace your menu file with below menu file
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="#style/Theme.AppCompat.Light"
xmlns:tools="http://schemas.android.com/tools"
tools:context="yourpackagename.Mainactivity" >
<item android:id="#+id/action_add"
android:icon="#drawable/add_plush" app:showAsAction="always"></item>
</menu>
thats it...
Related
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);
}
This question already has answers here:
showAsAction = "always" is ignored in Toolbar
(2 answers)
Android 4.3 menu item showAsAction="always" ignored
(13 answers)
Closed 5 years ago.
I'm building an app with AndroidStudio. I want to display an ActionBar in my application.
I want to add a menu into my AppCompactActivity. This is the menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_save"
android:showAsAction="always"
android:title="Salva"/>
</menu>
This is the layout that I can see from Android Studio preview.
This is my AppCompactActivity:
public class setting extends AppCompatActivity{
public Program program;
public TextView textTargetUri;
public TextView labelUrl;
public ImageView targetImage;
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ImageView img;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
img = (ImageView)findViewById(R.id.ImageView01);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_setting, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
getSchermataHome();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
But this is the layou that I can see:
How can I fixed it?
Try using the following code
<?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_search"
android:icon="#android:drawable/ic_menu_save"
android:title="Salva"
app:showAsAction="ifRoom|always"/>
</menu>
change always to ifRoom|always with app attribute
Change your android:showAsAction="always" to app:showAsAction="always"
So now your code would be
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_save"
app:showAsAction="always"
android:title="Salva"/>
</menu>
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.
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"
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 create base activity with actionbar menu functional and extended another activities from it.
public class BaseActivity extends SherlockFragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
return true;
}
}
Now I want to add some buttons to the actionbar in some activities. How can i add elements to actionbar and use element from base activity?
You can do that as simple as in BaseActivity just don't forget to call super.onCreateOptionsMenu().
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_help"
android:icon="#drawable/ic_action_help"
android:title="#string/action_help" />
</menu>
home.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_new"
android:icon="#drawable/ic_action_new"
android:title="#string/action_new" />
</menu>
In BaseActivity you are using base menu config main.xml.
public class BaseActivity extends SherlockFragmentActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
In child activity you are using another config from home.xml only with additional menu items, no duplicates. But don't forget to call super.onCreateOptionsMenu(menu) with the same menu instance to add elements from BaseActivity (parent menu items would be added after child menu items if you call super's method after inflate, and before otherwise).
public class HomeActivity extends BaseActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.home, menu);
return super.onCreateOptionsMenu(menu);
}
}
You can specify your actions for the actionbar in the main.xml
example :
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"/>
<item android:id="#+id/action_compose"
android:icon="#drawable/ic_action_compose"
android:title="#string/action_compose" />
And call from your subclass
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
Make different xml files containing the buttons you need specific to those Activity. Say for Activity1 you have two buttons and for Activity2 you have one, then you will create 2 xml files like below.
action_activity1.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"/>
<item android:id="#+id/action_compose"
android:icon="#drawable/ic_action_compose"
android:title="#string/action_compose" />
</menu>
action_activity2.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"/>
</menu>
Then in the onCreateOptionsMenu(Menu menu) method inflate the desired xml file. Like,
Activity1:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.action_activity1, menu);
return true;
}
Activity2:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.action_activity2, menu);
return true;
}
Please be care full with the syntax (as I used appcompat actionbar). :)
This is what I use. Hope you find it helpful. And, I will be happy to see an easier way than this. :)