I want to add an element to the label bar in the top right corner.element like the green square in the top right corner
Purpose of this element would be to indicate some app status by changing color (green, yellow or red). It should persist through all the app activities. If anyone has other suggestion on how I should go on about it, I would gladly accept it. Only condition is that it is in the position marked in the picture. the element can be an image (where i would load one of the 3 images) or a textview where I would just change background color, or some other solution.
this is the code where label is defined in AndroidManifest.xml:
<activity android:name=".activities.application.Limiter" android:label="#string/LIMITER_ACTION_BAR_TITLE" />
You can add the android toolbar widget in your main activity layout, below code will give your desired result
<android.support.v7.widget.Toolbar
android:id="#+id/home_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/logoXmarks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/common_full_open_on_phone" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
I created a menu item in a new .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"
android:layout_width="40dp"
android:layout_height="40dp">
<item
android:id="#+id/menu_item"
android:icon="#drawable/circle_ok"
app:showAsAction="always"
android:title=""/>
</menu>
and then added this code to activity:
private MenuItem menuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_item, menu);
menuItem = menu.findItem(R.id.menu_item);
return true;
}
to change the icon I used:
public boolean onPrepareOptionsMenu(Menu menu) {
switch(status) {
case 0:
menu.findItem(R.id.menu_item).setIcon(R.drawable.circle_ok);
break;
case 1:
menu.findItem(R.id.menu_item).setIcon(R.drawable.circle_warning);
break;
case 2:
menu.findItem(R.id.menu_item).setIcon(R.drawable.circle_alarm);
break;
}
return super.onPrepareOptionsMenu(menu);
}
after changing status value invalidateOptionsMenu(); should be called.
This is the result:
color changes every second
thx everyone for help, I hope someone can find this useful!
Related
I am creating an app in which I created a bottom navigation bar. Now what I want is to set separate colors for selected/unselected state of nav bar items. So I created an xml file and set it for item tint and text color for navigation bar as shown:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#android:color/holo_blue_dark" />
<item android:color="#android:color/darker_gray" />
</selector>
and in main menu adding navigation bar as:
<android.support.design.widget.BottomNavigationView
android:id="#+id/mainNav"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#android:color/white"
android:elevation="8dp"
app:itemIconTint="#color/nav_bar_item_state"
app:itemTextColor="#color/nav_bar_item_state"
app:menu="#menu/nav_items"></android.support.design.widget.BottomNavigationView>
I got this solution from this link:
Selected tab's color in Bottom Navigation View
But when I run the app it crashes with error "unable to inflate navigation bar". What am I doing wrong? Any help would be appreciated.
Update:
I tried the same thing by creating selector file in drawable file but unfortunately it also doesn't work.
Try this:-
Set item.setCheckable(true) inside onNavigationItemSelected()
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
item.setCheckable(true);
mTextMessage.setText(R.string.title_home);
return true;
}
return false;
}
I have a basic question and I'm surprised I didn't find a topic that already treats that matter: I have trouble adding elements to my navigation drawer menu.
I kept a reference of the menu :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_pager, menu);
mMenu = menu;
return true;
}
And in the callback of an AsyncTask, I try to add items to my menu this way :
mMenu.add("title").setIcon(R.drawable.ic_group);
Idealy I'd like to add it in the "menu_group" group, but for now I try it that way.
layout/activity_main_pager
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main_pager"
app:menu="#menu/activity_main_pager_drawer" />
</android.support.v4.widget.DrawerLayout>
menu/activity_main_pager_drawer
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="#string/menu_title_groups">
<menu>
<group android:id="#+id/menu_groups">
<item
android:id="#+id/menu_add_group"
android:icon="#drawable/ic_group_add"
android:title="#string/menu_add_group" />
</group>
</menu>
</item>
</menu>
Bonus question : The icon of "menu_add_group" item of my menu doesn't display correctly, I have a gray square instead of it.
Thanks, don't hesitate to link me an other topic if I didn't search correclty.
Ok one of the problems was the way I got my mMenu. I added all the corrections listed by my savior plus this one :
#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_pager, menu);
return true;
}
and on my activity init :
NavigationView nav = (NavigationView) mDrawer.findViewById(R.id.nav_view);
mMenu = nav.getMenu();
when I add a menu item :
menu.add("title").setIcon(R.drawable.ic_group);
supportInvalidateOptionsMenu();
After changing the menu items you need to call invalidateOptionsMenu() (or supportInvalidateOptionsMenu if you're using the support library). Android will then know that it needs to recreate it and will call onCreateOptionsMenu(Menu) again. You'll need to inflate the new menu there.
The grey icon is due to the item tint. You can remove it by simply calling mNavigationView.setItemIconTintList(null).
Firstly, my code for setting up the actionBar. (Using default system actionBar, android 4.2+)
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.listing_group, menu);
MenuItem listItem = menu.findItem(R.id.action_list);
ToggleButton customActionIcon = (ToggleButton) getLayoutInflater().inflate(R.layout.custom_action_bar_icon_view, null);
listItem.setActionView(customActionIcon);
customActionIcon.setTextOff("LIST");
customActionIcon.setTextOn("LIST");
customActionIcon.setTypeface(OswaldRegular());
customActionIcon= (ToggleButton)getLayoutInflater().inflate(R.layout.custom_action_bar_icon_view, null);
customActionIcon.setTextOff("MAP");
customActionIcon.setTextOn("MAP");
customActionIcon.setTypeface(OswaldRegular());
MenuItem mapItem = menu.findItem(R.id.action_map);
mapItem.setActionView(customActionIcon);
return super.onCreateOptionsMenu(menu);
}
Layout xml for Menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_list"
android:showAsAction="always"
/>
<item
android:id="#+id/action_map"
android:showAsAction="always"/>
</menu>
Layout xml for custom actionBar button:
<?xml version="1.0" encoding="utf-8"?>
<ToggleButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/action_bar_toggle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:background="#color/transparent"
android:textColor="#drawable/listing_group_activity_actionbar_text_color_selector">
</ToggleButton>
The Problem:
When the screen with this action bar first loads, both the buttons show up in the deselected color, but they both show the default text of "Off". When I click one of them, they correctly change color AND they change the text to the one I set in onCreateOptionsMenu(). i.e. , one becomes LIST and one becomes MAP . And then they continue to stay that way and function as normal ToggleButtons would. I would like them to start with the correct text showing, and after that is fixed I want one of them to be selected by default.
Any help on the matter is much appreciated! Thanks!
Fixed. it. Had to do customActionIcon.setChecked(true) for the enabled one and setChecked(false) for the disabled one and they start with the right text and right state
I have a working SearchView which expands in my OptionsMenu when the user taps on the search icon. However it only expands within the available space among the other OptionsMenu icons. On a wide screen this is fine, but with a narrow space there is only room to show 5-10 charaters in the search box. I want it to overlay the other icons such as it does for the Android Contacts app. Currently, I'm building with targetSdkVersion = 17. Hopefully I'm missing something simple :)
(Note added later: the only solution I've found workable so far is to hide all the menu icons when I want to expand the search icon. This is conceptually simple. But it is messy because when restoring hidden icons, one has to go through a bunch of logic to figure out which ones to restore, or keep state variables around, etc.)
Here's my item xml in for the OptionsMenu:
<item
android:id="#+id/menu_search_shallow"
android:title="Search Current Folder"
android:icon="#drawable/ic_btn_search"
android:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
I also have in my main activity code:
#Override
public boolean onCreateOptionsMenu (Menu menu)
{
getMenuInflater().inflate(R.menu.nav_menu, menu);
this.optionsMenu = menu;
MenuItem searchItem = menu.findItem (R.id.menu_search_shallow);
searchItem.setOnActionExpandListener (this);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint (getString (R.string.search_shallow_hint));
searchItem = menu.findItem (R.id.menu_search_deep);
searchItem.setOnActionExpandListener (this);
searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint (getString (R.string.search_deep_hint));
}
and
#Override
public boolean onMenuItemActionExpand(MenuItem item)
{
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener (this);
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item)
{
SearchView searchView = (SearchView) item.getActionView();
searchView.setQuery ("", false);
return true;
}
Instead of creating a new layout I set the MaxWidth programmatically in onCreateOptionsMenu():
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
SearchView searchView = (SearchView)menu.findItem(R.id.menu_search).getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
....
For some reason jjung's solution didn't work for me. I had to add android:maxWidth="10000dp", just a really high maxWidth, to get it to expand to the full available width.
Also, just to add, I'm using the support library (v7), and so my layout file looks like this:
<android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxWidth="10000dp" />
And in my item, I have:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto">
<item
...
myapp:actionLayout="#layout/searchview" />
...
Where myapp is just some arbitrary namespace for the attributes added by the support library (see Adding Action Items for more details).
Maybe I am late but thought it might be helpful for someone who faced the same issue as me.
final SearchView searchView = (SearchView) myActionMenuItem.getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);//set search menu as full width
in onCreateOptionsMenu
of your class may solve this problem but one thing we need to understand.
If you just want to show search menu (on clicking of search button) and not any other menu items in actionbar then you need to keep the search item as a first item in menu file. Here is an example
<?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">
<!--keeping searach as first item-->
<item
android:id="#+id/action_search"
android:icon="#drawable/search_ab"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom" />
<item
android:id="#+id/itemFilter"
android:icon="#drawable/filter"
android:title="#string/filter"
app:showAsAction="ifRoom" />
</menu>
And it will be displayed as
on clicking search it will be shown as full width
And if you keep search as second or last item in menu then it will be show like this.
and on clicking search
As crazy as it might seem, in my case problem was this line:
app:actionViewClass="android.widget.SearchView" in menu.xml:
<item
android:id="#+id/action_search"
app:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_search"
android:title="#string/title"
app:actionLayout="#layout/layout_search"
app:showAsAction="always" />
When I removed app:actionViewClass, it fully expanded as defined in my layout_search file.
<android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxWidth="10000dp"
android:layout_centerInParent="true"
android:background="#color/colorAccent"
android:queryHint="Search me" />
I had a similar issue, that the search bar did not fill the whole width,( but I had no other icons).
I solved it by adding in item:
android:actionLayout="#layout/my_search_view"
and in layout/my_search_view.xml :
<?xml version="1.0" encoding="utf-8"?>
<SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
There are so many ways to solve this issue - I'll just add the one I've used in several apps.
I have my custom SearchView (with some other customization) which extends android.support.v7.widget.SearchView and in order to stretch to full width:
#Override
public void setLayoutParams(final ViewGroup.LayoutParams params) {
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
super.setLayoutParams(params);
}
I want to have webview displayed fullscreen with action bar on top of it and standard menu bar on bottom. It's displayed correctly without action bar. When I am turning it on, bottom part of webview is displayed under bottom menu bar and I want to fit it between both menu bars.
Its probably easy fix but can't find answer on that.
Thanks!
Code:
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layerType="software"
/>
</RelativeLayout>
settings.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="#+id/group1">
<item android:id="#+id/opt" android:android:showAsAction="ifRoom|withText" android:enabled="true" android:title="Option 1" android:visible="true"></item>
<item android:id="#+id/opt2" android:showAsAction="ifRoom|withText" android:enabled="true" android:title="Option 2" android:visible="true"></item>
</group>
</menu>
activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.settings, menu);
this.menu = menu;
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
return true;
}
android:windowActionBarOverlay Declares whether the action bar should
overlay the activity layout rather than offset the activity's layout
position This is false by default.
When overlay mode is enabled, your activity layout has no awareness of
the action bar laying on top of it.
More about Actionbars can be read here: http://developer.android.com/guide/topics/ui/actionbar.html
Use Relative Layout in order to fit things in the right way.
Show us more code in order to obtain for a better answer :D
MOD
Options menu is not made usually for fit into your layout , but overlapping it . I tell you to consider creating a fixed real menu which hides whenever you want with some action.
Also think that menu can't have a fixed size and it may change with orientation , mobile phone , android version . Then if you could made what you need , you'll have more problems with your layout than making a custom one.