I have made a custom theme for my app which uses action bar.Now when i run my app the actionbar is visible but button in it is not visible.I dont know what went wrong.
The button is visible when i click on menu button (the 3 hardware button) but not displaying on the action bar
Style.Xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="TripLoggerTheme" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">#style/TripLoggerActionBar</item>
<!--<item name="android:homeAsUpIndicator">#drawable/test</item>-->
</style>
<!--use to style the actionbar-->
<style name="TripLoggerActionBar" parent="android:Widget.ActionBar">
<item name="android:background">#6e784c</item>
<item name="android:showAsAction">ifRoom|always|collapseActionView|never|withText</item>
<item name="android:displayOptions">homeAsUp|showHome|showTitle</item>
</style>
Menu.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_settings"
android:title="#string/action_settings"
android:icon="#drawable/images"
app:showAsAction="always" />
</menu>
Code
public class MainActivity extends Activity {
private int[] images = {R.drawable.images, R.drawable.images_2, R.drawable.images_1, R.drawable.images_4, R.drawable.images, R.drawable.images_2};
private GridView gridView;
private LinearLayout linearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base_activity);
linearLayout = (LinearLayout) findViewById(R.id.root);
getActionBar();
// gridView = (GridView) findViewById(R.id.grid);
// gridView.setAdapter(new CustomAdapter(MainActivity.this, images));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
linearLayout.setVisibility(View.VISIBLE);
break;
}
return true;
}
As the screenshots shows,when i click on the 3 dots from below test is visible but it should be visible on the top where i have marked a cross
I tried finding out what could be the problem but all in vain.
I believe what you are experiencing is the standard Android behaviour. You may want to read the following text on the official docs on Action Overflow:
The overflow icon only appears on phones that have no menu hardware keys. Phones with menu keys display the action overflow when the user presses the key.
First, from snapshot, it seems that you are testing in emulator. Check if same happens on the device.
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 have used the following code:
listview.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
final int checkedCount = listview.getCheckedItemCount();
mode.setTitle(checkedCount + " Selected");
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.mymenu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.delete:
//my delete code
mode.finish();
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode mode) {
//
}
});
mymenu.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/delete"
android:icon="#android:drawable/ic_menu_delete"
android:title="Delete"
app:showAsAction="always"
/>
</menu>
styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:actionModeBackground">#android:color/holo_blue_dark</item>
<item name="android:actionModeStyle">#style/TestActionModeStyle</item>
</style>
<style name="TestActionModeStyle">
<item name="android:titleTextStyle">#style/TestTitleStyle</item>
<item name="android:subtitleTextStyle">#style/TestSubTitleStyle</item>
</style>
<style name="TestTitleStyle">
<item name="android:textColor">#android:color/black</item>
<item name="android:background">#android:color/holo_red_dark</item>
</style>
<style name="TestSubTitleStyle">
<item name="android:textColor">#android:color/black</item>
<item name="android:background">#android:color/holo_green_dark</item>
</style>
When i longpress on listview then item is selected and menu is coming with delete button. But i am not able to set the color of that whole menu and event the selected listview item is also not reflected.
Menu background color occus in white and selected listview item is also occurs white color.
So i want to change the menu color and selected item from listview color.
From above code it is not working.
How can i solved this?
Check this answer for your menu part of the question: How to change the background color of Action Bar's Option Menu in Android 4.2?
And this answer Change background color of selected item on a ListView, for changing your selected listview item.
Just for completeness here the parts of those questions that are relevant for you:
For the menu (note they speak about the Actionbar, but the menu is also included):
There is an easy way to change the colors in Actionbar Use ActionBar
Generator..
but since you already have a style you only want to update the menu like a bit further in the linked question:
In the style generator, the relevant setting is Popup color, which
affects "Overflow menu, submenu and spinner panel background".
Go on and generate the zip, but out of all the files generated, you
only really need one image, menu_dropdown_panel_example.9.png, which
looks something like this:
So, add the different resolution versions of it to res/drawable-*.
(And perhaps rename them to menu_dropdown_panel.9.png.)
Then, as an example, in res/values/themes.xml you would have the
following, with android:popupMenuStyle and android:popupBackground
being the key settings.
<style name="MyAppActionBarTheme" parent="android:Theme.Holo.Light">
<item name="android:popupMenuStyle">#style/MyApp.PopupMenu</item>
<item name="android:actionBarStyle">#style/MyApp.ActionBar</item>
</style>
<!-- The beef: background color for Action Bar overflow menu -->
<style name="MyApp.PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
<item name="android:popupBackground">#drawable/menu_dropdown_panel</item>
</style>
<!-- Bonus: if you want to style whole Action Bar, not just the menu -->
<style name="MyApp.ActionBar" parent="android:Widget.Holo.Light.ActionBar.Solid">
<!-- Blue background color & black bottom border -->
<item name="android:background">#drawable/blue_action_bar_background</item>
</style>
For the listview:
You can keep track the position of the current selected element:
OnItemClickListener listViewOnItemClick = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
mSelectedItem = position;
mAdapter.notifyDataSetChanged();
}
};
And override the getView method of your adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view = View.inflate(context, R.layout.item_list, null);
if (position == mSelectedItem) {
// set your color
}
return view;
}
I need to show the submenu below the bar, not on top of the bar itself.
Copying my actionbar xml below
<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_pages"
android:orderInCategory="1"
android:showAsAction="withText|always"
android:icon="#drawable/ic_action_pages"
android:title="">
<menu>
<item android:id="#+id/item1" android:title="Placeholder"></item>
</menu>
</item>
</menu>
In the activity (App also has a navigation drawer)
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
Simply.
<style name="AppTheme" parent="AppBaseTheme">
<item name="actionOverflowMenuStyle">#style/OverflowMenu</item>
</style>
<style name="OverflowMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
<!-- Required for pre-Lollipop. -->
<item name="overlapAnchor">false</item>
<!-- Required for Lollipop. -->
<item name="android:overlapAnchor">false</item>
</style>
Preamble
As usual, I faced a strange problem while developing an Android app, tried to find a solution and landed to this question. As it was in many cases before, there is no an answer. So I was compelled to solve the problem from scratch and now posting the answer with my workaround.
Input
I have an Android app with action bar and some menu items, which have to be extended with dropdown submenu. First attempt was to implement it as suggested by Android documentation. So I added new menu item menu_sort into existing action bar menu and sub-menu container into it:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/id1" android:icon="#drawable/ic_1"
android:title="#string/id1" android:showAsAction="withText|always"/>
...
<item
android:id="#+id/menu_sort"
android:icon="#drawable/ic_menu_sort_selector"
android:title="▾"
android:titleCondensed="▾"
android:showAsAction="withText|always">
<menu>
<item
android:id="#+id/menu_sort_by_name"
android:showAsAction="never"
android:checkable="true"
android:checked="true"
android:title="#string/sort_by_name"/>
<item
android:id="#+id/menu_sort_by_priority"
android:showAsAction="never"
android:checkable="true"
android:checked="false"
android:title="#string/sort_by_priority"/>
<item
android:id="#+id/menu_sort_by_memory"
android:showAsAction="never"
android:checkable="true"
android:checked="false"
android:title="#string/sort_by_memory"/>
</menu>
</item>
</menu>
Result
The effect was exactly as described in the question: the submenu is displayed on top of the action bar. Here is the screenshot taken on Android 5.1.1:
I played with many options and code snippets - nothing helped. Finally I came to the following
Solution
First, move all the submenu into a separate menu layout, say, menu/sorting.xml, and remove it from menu_sort item of the main menu (shown above).
Second, modify or create onPrepareOptionsMenu event handler with the following code:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
// as solution utilizes PopupMenu,
// take care about older Android versions if necessry
// if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
// here goes most crazy part: we use menu id
// to retrieve corresponding view, automatically created by OS;
// imho, this is a hack, and menu item should have getView() method or similar;
View menuItemView = findViewById(R.id.menu_sort);
// by the way, menuItemView could probably be null under some circumstances
// create a popup anchored to the view (menu item)
final PopupMenu popupMenu = new PopupMenu(this, menuItemView);
// API 14
// popupMenu.inflate(R.menu.sorting);
// API 11 (HONEYCOMB)
popupMenu.getMenuInflater().inflate(R.menu.sorting, popupMenu.getMenu());
// process popup clicks as appropriate
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
switch(item.getItemId())
{
// ... place some code
}
return true;
}
});
// bind the popup to the item menu
menu.findItem(R.id.menu_sort).setOnMenuItemClickListener(new OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
popupMenu.show();
return true;
}
});
return super.onPrepareOptionsMenu(menu);
}
Here is the result:
Now the dropdown is displayed as expected from very beginning.
#Stan's solution doesn't work for me, so here's my way to implement sub-menu on top of ActionBar (but below the main-menu of course):
I've created 2 xml files: menu_main.xml and menu_more.xml located in res/menu directory
The first one 'menu_main.xml' contain the menu:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- our addMenu doesn't have sub-items-->
<item
android:id="#+id/action_add"
android:icon="#drawable/ic_note_add_white_24dp"
android:title="#string/action_add"
app:showAsAction="ifRoom"/>
<!-- our moreMenu which show drop-down menu when clicked-->
<item
android:id="#+id/action_more"
android:icon="#drawable/ic_more_vert_white_24dp"
android:title="#string/action_more" <!--in text: "more"-->
app:showAsAction="always"/>
</menu>
And the second one 'menu_more.xml' contain the drop-down menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- This menu will be hidden by default-->
<!-- But will be visible when moreMenu with '#+id/action_more' is clicked-->
<item
android:id="#+id/action_settings"
app:showAsAction="ifRoom|withText"
android:title="#string/action_settings" <!-- In text: "Settings"-->
android:visible="true"/>
</menu>
Here is what previous menus look like:
result-after-add-2-xmls (i have not enough 10 reputation to display image)
In the activity, i've overridden this method:
public boolean onPrepareOptionsMenu(Menu menu)
In the previous method, i get reference to the main menuItem (in this case is menu with #+id/action_more located in menu_main.xml file), then set setOnMenuItemClickListener on it and finally, declare and set up a PopupMenu instance to manage and display sub-menu items:
// show popup menu when menuMore clicked
menu.findItem(R.id.action_more).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
// get reference to menuMore item
View menuMore = findViewById(item.getItemId());
// create a popup anchored to the view (menuMore)
// notes: if declare and set up PopupMenu Outside of this onMenuItemClick()
// then it'll not work!
// Because: the view you put into PopupMenu() could be null
final PopupMenu popupMenu = new PopupMenu(getApplicationContext(), menuMore);
// inflate 'menu_more.xml' layout file
// which contain all sub-items of menu
popupMenu.getMenuInflater().inflate(R.menu.menu_more, popupMenu.getMenu());
// process popup clicks on sub-items
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
switch(item.getItemId()){
case R.id.action_settings:
Toast.makeText(getApplicationContext(), "showing SettingsActivity..",
Toast.LENGTH_SHORT).show();
break;
// more items go here
}
return true;
}
});
popupMenu.show();
return true;
}
});
return super.onPrepareOptionsMenu(menu);
And here is final result:
final-look-drop-down-menu
Im building my top menu using the following code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_view, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.map_view:
Toast.makeText(getApplicationContext(), "Map view pressed",
Toast.LENGTH_LONG).show();
return true;
case R.id.camera_only:
Toast.makeText(getApplicationContext(), "Camera only pressed",
Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
menu_view.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/dots_menu"
android:icon="#drawable/ic_action_overflow"
android:showAsAction="always"
android:title="#string/dots_menu">
<menu>
<item
android:id="#+id/map_view"
android:icon="#drawable/ic_action_map"
android:showAsAction="never"
android:title="#string/map" />
<item
android:id="#+id/camera_only"
android:showAsAction="never"
android:title="#string/camera_only"/>
</menu>
</item>
</menu>
It happens that in my main activity i want to use this menu on top, but the menu appers transparent. Im using a fragment to display a full-screen map (google maps api v2), but then i can see the map under the top menu. I would like this fragment to "auto-resize" under the menu.
How can i set menu always on top of every activity?
So, i found out what my problem was!
in styles.xml file:
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo">
<item name="android:windowActionBarOverlay">true</item>
</style> </resources>
i just needed to change the attribute windowActionBarOverlay to false, and it makes automatically the resize for the activity content.
For more details, here is the documentation
I wan to have one action bar with one "menu" button. (Until here I know how to do) But Now I want to do that, when the users Clicks on any subitem of the menu, don't close this menu.
Said in other words, I want to enable the multiselect menu in action bar sherlock, but I dont know how to do it.
Can someone explain me some way to achive it?
Thats what I have
public class MainActivity extends SherlockActivity{
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Sherlock___Theme_Light);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getSupportActionBar();
actionBar.setTitle("Testing");
actionBar.setSubtitle("test");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
And this is the XML Menu file:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu"
android:orderInCategory="0"
android:title="Menu"
android:showAsAction="ifRoom">
<menu>
<item
android:id="#+id/subitem1"
android:title="SubMenu1"/>
<item
android:id="#+id/subitem2"
android:title="SubMenu2"/>
<item
android:id="#+id/subitem3"
android:title="SubMenu3"/>
<item
android:id="#+id/subitem4"
android:title="SubMenu4"/>
</menu>
</item>
</menu>