I have tried almost all of the things suggested by SO in other questions..still menu item is not showing in action bar.
Before it used to show, but when i used action layout for menu to change the text size, it has stopped showing it.Any help will be appreciated...
This is my menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/mnuRecentLookUp" android:title="#string/mnuRecentLookUp" android:icon="#drawable/clock" />
<item android:id="#+id/mnuBookmarks" android:title="#string/mnuBookmarks" android:icon="#drawable/bookmarks" />
<item android:id="#+id/mnuFontSize" android:title="#string/mnuFontSize" android:icon="#drawable/fontsize"/>
<item android:id="#+id/mnuHelp" android:title="#string/mnuHelp" android:icon="#drawable/help" />
<group android:id="#+id/group_term_nav">
<item android:id="#+id/btnTtod"
android:title="#string/ttod"
android:showAsAction="always"
android:actionLayout="#layout/menu_action_layout"
/></group>
</menu>
Below is menu_action_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="#+id/actionBarMenuPoints"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:paddingLeft="5dp"
android:layout_gravity="center"
android:layout_marginRight="8dp"
android:textStyle="bold"
android:drawablePadding="3dp"
android:gravity="center"
android:scaleType="fitCenter"
/>
</FrameLayout>
Activity code in onCreateOptionsMenu..
public boolean onCreateOptionsMenu(final Menu menu) {
MenuInflater menuInflater = this.getMenuInflater();
menuInflater.inflate(R.menu.nav_home, menu);
// Calling super after populating the menu is necessary here to ensure
// that the
// action bar helpers have a chance to handle this event.
MenuItem pointsMenu = menu.findItem(R.id.btnTtod);
// pointsMenu.setTitle(pointsLeft );
TextView actionBarPoints = (TextView) pointsMenu.getActionView().findViewById(R.id.actionBarMenuPoints);
//actionBarPoints.setText(pointsLeft);
actionBarPoints.setGravity(Gravity.CENTER_VERTICAL);
actionBarPoints.setTextColor(context.getResources().getColor(R.color.white));
actionBarPoints.setTextSize(20);
//actionBarPoints.setTypeface(tfBold);
this.getActionBar().setCustomView(actionBarPoints);
return super.onCreateOptionsMenu(menu);
}
Solved the issue by using below code in styles.xml
<style name="MyActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:actionMenuTextAppearance">#style/MyActionBar.MenuTextStyle</item>
</style>
<style name="MyActionBar.MenuTextStyle"
parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textStyle">bold</item>
<item name="android:textSize">20sp</item>
Related
My toolbar contains :
my own views at start of the toolbar and
then a standard iconized searchview, which can be expanded by clicking the search icon:
When clicking icon to expand the search it overlaps the title:
How can I achieve to expand the searchview only to the end of "MY TITLE"?
Main layout:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/home_appbarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:id="#+id/app_toolbar"
android:layout_width="match_parent"
android:layout_height="44dp"
android:theme="#style/myToolbarTheme"
app:contentInsetLeft="0dp"
app:contentInsetStart="5dp"
app:contentInsetStartWithNavigation="0dp"
app:popupTheme="#style/myToolbarTheme"
app:title="">
<include layout="#layout/toolbar_title" />
</androidx.appcompat.widget.Toolbar>
<include layout="#layout/toolbar_tabs" />
</com.google.android.material.appbar.AppBarLayout>
<ViewFlipper .....
toolbar_title layout: included in Mainlayout
<ImageView
android:id="#+id/current_view_icon"
android:layout_width="#dimen/toolbar_view_icon"
android:layout_height="#dimen/toolbar_view_icon"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
app:srcCompat="#drawable/ic_chronology" />
<TextView
android:id="#+id/appbar_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:gravity="center_vertical"
android:text="MY TITLE"
android:textColor="#color/navigation_inner_color"
android:textSize="22dp" />
Inflating menu in code:
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
appToolbar = context.findViewById(R.id.app_toolbar);
((AppCompatActivity) context).setSupportActionBar(appToolbar);
appToolbar.inflateMenu(R.menu.main_menu);
R.menu.main_menu with quicksearch:
<?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="wrap_content"
android:layout_height="wrap_content">
<item
android:id="#+id/app_bar_search"
android:icon="#drawable/ic_search_black_24dp"
android:title="Search"
app:actionViewClass="mypackage.toolbar.ArrayAdapterSearchView"
app:showAsAction="always" />
<item
android:id="#+id/app_bar_bookmark"
android:icon="#drawable/ic_bookmark"
android:title="Bookmark"
app:showAsAction="always" />
<item
android:id="#+id/app_bar_menuitem_settings"
android:title="Settings"
app:showAsAction="never" />
</menu>
I think you have 2 options,
Set width to SearchView in styles
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="searchViewStyle">#style/MySearchViewStyle</item>
</style>
<style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView">
<item name="android:maxWidth">140dp</item>
</style>
Second option is get SeachView instance in onCreateOptionsMenu and calculate width of your toolbar_title layout set remaining width to search bar.
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setMaxWidth(calculatedWidth);
return super.onCreateOptionsMenu(menu);
}
But as per android standards, Search View has to extend completely in toolbar.
For this we have to use searchView.setMaxWidth(Integer.MAX_VALUE);.
This is the code of the menu main_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/newy"
>
<item
android:id="#+id/myswitch"
app:actionLayout="#layout/switch_layout"
android:title=""
app:showAsAction="always" />
<item
android:id="#+id/empty"
android:title="options"
android:enabled="true"
android:visible="true"
android:orderInCategory="101"
app:showAsAction="always"
android:icon="#drawable/ic_launcher_overflow1">
<menu>
<item android:id="#+id/action_help"
app:showAsAction="ifRoom"
android:title="help" />
<item
android:id="#+id/action_settings"
android:title="settings"
app:showAsAction="ifRoom" />
</menu>
</item>
</menu>
The first item of id "myswitch" is the switch,
this is it's actionlayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<Switch
android:id="#+id/switchForActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingRight="18dp"
android:text=""
android:visibility="visible" />
</RelativeLayout>
In MainActivity.java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu,menu);
return true;
}
Is there a way to get a reference to this switch in onCreate?
I want full access to the switch not only when it is clicked(using onOptionsItemSelected which doesn't work by the way; it was unable to detect when the switch was pressed unless clickable is set to false).
I tried too many ways from similar questions but in vain...
The function of this button is to toggle the wifi (WifiManager.setWifiEnabled(false) or WifiManager.setWifiEnabled(true)).
When the program starts it should detect whether the wifi is initially turned on or off and set the switch to the corresponding state.
I try to add menu in toolbar.i wrote some code but i have very strange situation.i can't see menu image right side in my toolbar,but when i click it(i mean right side in toolbar)menu is showing
this is a my source
<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="com.unipay.business.activities.MainActivity">
<item
android:id="#+id/transaction_all"
android:orderInCategory="100"
android:title="#string/history_all"
app:showAsAction="never" />
<item
android:id="#+id/transaction_Income"
android:orderInCategory="100"
android:title="#string/history_income"
app:showAsAction="never" />
<item
android:id="#+id/transaction_Outcome"
android:orderInCategory="100"
android:title="#string/u_history_outcome"
app:showAsAction="never" />
<item
android:id="#+id/transaction_advanced_search"
android:orderInCategory="100"
android:title="#string/u_history_advance"
app:showAsAction="never" />
i ty to create like this menu
this is a my custom Toolbar layout xml code
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/u_common_header_height"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ToolbarTheme"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:titleTextAppearance="#style/Toolbar.TitleText">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/u_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/u_sign_open_menu_margin"
android:padding="#dimen/u_sign_open_menu_margin"
android:src="#mipmap/ic_arrow_back_white" />
<ImageView
android:id="#+id/logo_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#mipmap/logo_header"
android:visibility="gone" />
<TextView
android:id="#+id/header_tittle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="72dp"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="#dimen/u_common_text_size" />
</RelativeLayout>
I initialized my toolbar like this
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
this is a toolbar style
<style name="ToolbarTheme" parent="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<!-- android:textColorPrimary is the color of the title text in the Toolbar -->
<item name="android:textColorPrimary">#android:color/holo_blue_light</item>
<!-- actionMenuTextColor is the color of the text of action (menu) items -->
<item name="actionMenuTextColor">#android:color/holo_green_light</item>
<!-- Tints the input fields like checkboxes and text fields -->
<item name="colorAccent">#color/colorPrimary</item>
<!-- Applies to views in their normal state. -->
<item name="colorControlNormal">#color/colorPrimary</item>
<item name="colorControlActivated">#color/colorPrimary</item>
<item name="colorControlHighlight">#color/colorPrimary</item>
</style>
<style name="Toolbar.TitleText" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="android:textSize">21sp</item>
<item name="android:textStyle">italic</item>
</style>
can anyone tell me what i'm doing wrong? as i said menu working in toolbar but i can't see menu image
thanks
try this
import android.support.v7.view.menu.MenuBuilder;
#SuppressLint("RestrictedApi")
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (menu instanceof MenuBuilder) {
((MenuBuilder) menu).setOptionalIconsVisible(true);
}
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
app:showAsAction="never"
change to
app:showAsAction="always" or "ifRoom"
I have a problem with a background color of my action menu items. As you can see below I tried to set the same background color as for the toolbar. But they are still different. I use different attributes -- background and itemBackground -- but they both don't work.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/BackgroundToolbarGrey"
android:contentInsetEnd="0dp"
android:contentInsetStart="0dp"
android:itemBackground= "#color/BackgroundToolbarGrey">
<android.support.v7.widget.ActionMenuView
android:id="#+id/mainMenu"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="left"
android:background="#color/BackgroundToolbarGrey"
android:itemBackground= "#color/BackgroundToolbarGrey"/>
</android.support.v7.widget.Toolbar>
And this is my menu.xml file where I also tried to set color:
<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=".MyActivity"
android:background = "#color/BackgroundToolbarGrey">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"
android:background = "#color/BackgroundToolbarGrey"
android:itemBackground= "#color/BackgroundToolbarGrey">
</item>
</menu>
I also tried to set it programmatically, but it doesn't help
mainMenu= (ActionMenuView) findViewById(R.id.mainMenu);
mainMenu.setBackgroundColor(getResources().getColor(R.color.BackgroundToolbarGrey));
What can I do wrong?
You should set a PopupTheme for your ActionMenuView.
java code
mActionMenuView = (ActionMenuView) findViewById(R.id.action_menu_view);
mActionMenuView.setPopupTheme(R.style.OverFlowMenuTheme);
style.xml
<style name="OverFlowMenuTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:itemBackground">#android:color/white</item>
<item name="android:textColorPrimary">#android:color/black</item>
<item name="overlapAnchor">false</item>
</style>
Try setting the menu item's properties in onCreateOptionsMenu.
Something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
View mainMenu = menu.findViewById(R.id.mainMenu);
mainMenu.setBackgroundColor(getResources().getColor(R.color.BackgroundToolbarGrey));
return true;
}
can I add a divider and items (labels) that is not selectable to my PopUp meny xml.
If want a meny like this
Label1
------------- (divider)
RadioButton1
RadioButton2
------------- (divider)
Item1
Item2
------------- (divider)
Item3
I create my popup menu with
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener((PopupMenu.OnMenuItemClickListener) this);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_popup, popup.getMenu());
popup.show();
Im using this xml for my menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".ComicBoothActivity">
<item android:id="#+id/Label1" android:title="Label1" />
<item android:id="#+id/Divider1" android:title="" android:showAsAction="always" android:actionLayout="#layout/menu_divider" />
<group android:checkableBehavior="single">
<item android:id="#+id/RadioButton1"
android:title="RadioButton1"
android:checked="true"/>
<item android:id="#+id/RadioButton2"
android:title="RadioButton2" />
</group>
<item android:id="#+id/Divider2" android:title="" android:showAsAction="always" android:actionLayout="#layout/menu_divider" />
<item android:id="#+id/Item1"
android:title="Item1" />
<item android:id="#+id/Item2"
android:title="Item2" />
<item android:id="#+id/Divider3" android:title="" android:showAsAction="always" android:actionLayout="#layout/menu_divider" />
<item android:id="#+id/Item2"
android:title="Item2" />
</menu>
And the divider layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="3dp"
android:background="#color/orange08">
</RelativeLayout>
How can I make the label "unselectable", i.e that nothing happens when you click it and the menu stays open. Is this possibe? The divider layout is not showing. Its only a empty item entry in the menu. What have I missed? Or is there a better way to add dividers?
<item android:title="unselectable">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#android:drawable/ic_menu_share"
android:title="Share"/>
</menu>
</item>