I am adding icons to ActionBar, but instead of getting them on ActionBar, I get them in the menu option..
My Java Code-
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.dashboard, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.report) {
Toast.makeText(getApplicationContext(),"Report",Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
My dashboard.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/share"
android:orderInCategory="100"
android:title="Share on Whatsapp"
app:showAsAction="never"/>
<item
android:id="#+id/medication"
android:title="My Medications"
android:icon="#drawable/medication"
app:showAsAction="always"/>
<item
android:id="#+id/coc"
android:title="Add Circle of care"
android:icon="#drawable/add_icon"
app:showAsAction="always"/>
<item
android:id="#+id/report"
android:title="Report"
android:icon="#drawable/report"
app:showAsAction="always"/>
</menu>
Please tell what am I missing..
I have appcompat as library.
Thank you
Extended ActionBar or AppCompactActivity
First menu item has app:showAsAction="never", so others menu items will always be shown only in overflow menu.
Related
I want to remove that 3 dots from toolbar and also want to add an image with textview on that position.That image and textviews are different for each fragment activity.
Also can anyone tell me how to make the toolbar transparent.
In your MainActivity you will have optionmenu, just make it false
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}
Remove this item from menu.
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
visibility=false
android:title="#string/action_settings"/>
1)your First Question about removing three dots (Aditya -Vyas )solved your prob
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}
2) Now to put image view
Crate Menu Resource
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_submit"
android:title="#string/submit"
android:icon="#drawable/ask_price_back"
app:showAsAction="always" />
</menu>
Then declare onCrateOptionsMenu and onOptionsImtemSelected as below
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_submit, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_submit) {
//Your code to handle click
return true;
}
return super.onOptionsItemSelected(item);
}
3) Handling The menu from the fragment loaded in activity try this
override this method in your fragment
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
getActivity().getMenuInflater().inflate(R.menu.menu_of_fragment, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_fragment) {
// Your code to handle Click
}
return super.onOptionsItemSelected(item);
}
1) From your menu.xml remove all items with tag .
This will remove three dots from your menu area.
2) Add a new item in your xml and give properties
android:icon="#drawable/image_you_want to show in the menu area"
app:showAsAction="always"
set your style like
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:actionOverflowButtonStyle">#style/MyActionButtonOverflow</item>
</style>
<!-- Style to replace actionbar overflow icon. set item 'android:actionOverflowButtonStyle' in AppTheme -->
<style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
<item name="android:src">#drawable/ic_launcher</item>
</style>
check this post : post
> for adding imageview and text
` <item android:id="#+id/action_cart"
android:icon="#drawable/cart2"
android:title="image"
app:showAsAction="always" />
<item
android:id="#+id/badge"
app:actionLayout="#layout/counter"
android:title="image"
android:icon="#drawable/shape"
app:showAsAction="always">
</item>`
In Layout counter
`<TextView
android:id="#+id/notif_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="20dp"
android:minHeight="20dp"
android:background="#drawable/shape"
android:text="0"
android:textSize="14sp"
android:textColor="#fff"
android:gravity="center"
android:padding="2dp"
android:singleLine="true">
</TextView>`
Just add this to you theme:
<item name="actionOverflowButtonStyle">#id/invisible</item>
I want icons on action bar, but I am getting them in the menu options...
Here is my xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/share"
android:orderInCategory="100"
android:title="Share on Whatsapp"
app:showAsAction="always" />
<item
android:id="#+id/medication"
android:title="My Medications"
android:icon="#drawable/medication"
app:showAsAction="always" />
<item
android:id="#+id/coc"
android:title="Add Circle of care"
android:icon="#drawable/add_icon"
app:showAsAction="always" />
<item
android:id="#+id/report"
android:title="Report"
android:icon="#drawable/report"
app:showAsAction="always" />
</menu>
and here is the java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.dashboard, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
menu.toggle(true);
return true;
}
if(id == R.id.coc){
startActivity(new Intent(Dashboard.this,CircleOfCare.class));
}
return super.onOptionsItemSelected(item);
}
What am I missing.. I am extending Activity.. If I extend ActionBarActivity, it says, use theme.appcompat or descendant even when I am using that theme only. Please help, thanx in advance
If you aren't using ActionBarActivity then try using
android:showAsAction="always"
instead of
app:showAsAction="always"
For some reason, I cannot get my button to appear on the Action Bar. I have defined it in an XML file in /res/menu, along with inflating it and listening for an action. The icon is present in /res/drawable-hdpi. And nothing of interest shows in logcat. :(
XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/logout"
android:icon="#drawable/logout"
android:title="Logout"
android:orderInCategory="100"
android:showAsAction="always" />
</menu>
Code in main activity:
public class MainActivity extends ActionBarActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.logout:
//logout code
return true;
default:
return super.onOptionsItemSelected(item);
}
}
//rest of app
}
I followed this question for my initial problem, and it didn't help. How to add button in ActionBar(Android)?
Try with this change:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/logout"
android:icon="#drawable/logout"
android:title="Logout"
android:orderInCategory="100"
yourapp:showAsAction="always" />
</menu>
I would like to create a menu to appear on the screen when the Menu button is pressed.I've implemented it and it appears in the bottom of the page but not the one i would like... Hereunder my code and my goal picture to achieve
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) {
switch (item.getItemId()) {
case R.id.collapseall:
// Single menu item is selected do something
collapseAll();
return true;
case R.id.expandall:
expandAll();
return true;
}
}
My desired Menu:
My current Menu:
Edit:
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/expandall"
android:orderInCategory="100"
android:showAsAction="always"
android:title="Expand All"/>
<item
android:id="#+id/collapseall"
android:orderInCategory="100"
android:showAsAction="withText"
android:title="Collapse All"/>
<item
android:id="#+id/myprofile"
android:orderInCategory="100"
android:showAsAction="withText"
android:title="My Profile"/>
<item
android:id="#+id/signout"
android:orderInCategory="100"
android:showAsAction="always"
android:title="Sign Out"/>
</menu>
I can't get the Action Bar to display my action items. They all show up in the overflow menu. I have pasted all the relevant code below. Can anyone see my problem?
From Activity:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.viewer_menu, menu);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_download:
return true;
case R.id.menu_star:
return true;
case R.id.menu_report:
return true;
case android.R.id.home:
// app icon in action bar clicked; go home
finish();
return true;
}
return false;
}
From Manifest:
<activity android:name=".CustomActivity"
android:label="">
From values-v11 folder (themes.xml)
<resources>
<style name="MyTheme" parent="#android:style/Theme.Holo">
</style>
from menu folder (viewer_menu.xml)
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_download"
android:title="Download" showAsAction="withText"
android:orderInCategory="2"/>
<item android:id="#+id/menu_star"
android:icon="#android:drawable/ic_menu_upload"
android:title="Star"
showAsAction="always"
android:orderInCategory="1"/>
<item android:id="#+id/menu_report"
android:title="Report Problem" showAsAction="always"
android:orderInCategory="0"/>
</menu>
It is android:showAsAction, not just showAsAction.
If you are using support package (android.support.v7.app.ActionBarActivity), you have to use something like this:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_download"
android:title="Download"
app:showAsAction="withText"
android:orderInCategory="2"/>
<item android:id="#+id/menu_star"
android:icon="#android:drawable/ic_menu_upload"
android:title="Star"
app:showAsAction="always"
android:orderInCategory="1"/>
<item android:id="#+id/menu_report"
android:title="Report Problem"
app:showAsAction="always"
android:orderInCategory="0"/>
</menu>
what version is the android emulator you are running?
Also, have you tried the http://actionbarsherlock.com/ version yet?