I created a menu and popup menu like picture below (I get in internet and edit by paint), I don't know why it doesn't display normal http://i.imgur.com/jHPobCH.png. I want the interface like this http://i.imgur.com/08XomVV.png and moever, how do I change popup menu background color? I tried as below but not success. I test on Samsung Galaxy J and android 4.4.2.
MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
menu_main.xml
<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=".MainActivity">
<item android:id="#+id/action_filter"
android:icon="#drawable/ic_filter_white_18dp"
android:title="#string/action_filter"
app:showAsAction="ifRoom" />
<item android:id="#+id/action_label"
android:icon="#drawable/ic_add_circle_outline_white_18dp"
android:title="#string/action_label"
app:showAsAction="ifRoom" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
<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=".MainActivity">
<item android:id="#+id/action_filter"
android:icon="#drawable/ic_filter_white_18dp"
android:title="#string/action_filter"
app:showAsAction="ifRoom" />
<item android:id="#+id/action_label"
android:icon="#drawable/ic_add_circle_outline_white_18dp"
android:title="#string/action_label"
app:showAsAction="ifRoom" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:background">#3079EB</item>
<item name="android:popupMenuStyle">#style/PopupMenu</item>
</style>
<style name="PopupMenu" parent="Widget.AppCompat.Light.PopupMenu">
<item name="android:popupBackground">#android:color/white</item>
</style>
P/S: I'm a beginner android development it
Can you please post Your entire code here . From the first look what i can understand is the height of the layout where you are populating the popup menu is much high . So even after populating the four items , its having enough of empty space . Change the layout_height of the layout where you are inflating the popup to wrap-content instead of using some fixed height . I hope that will solve the issue .
Related
See end of post for a workaround
I added an overflow menu to my main Android Action. However when pressing the [...] in the top right corner, the PopupMenu does not display the menu items correctly. They are left blank, see the image, it should contait 2 menu items with the strings "Manage" and "About":
This problem only arises for the MainActivity, i.e. the Activity starting at application launch. A secondary Activity, that is spawned later, correctly displays menu items.
The Items do get added to the menue somehow, since the box changes its size with more/less menu items. Also I do get a response when clicking where a menu item is supposed to be, e.g. show a Toast with some text.
Adding the menu items with showAsAction="always" will display the item correctly with it's icon in the ActionBar.
The MainActivity was initially created as a PageView Activity, the Toolbar was then later added to it's layout.
Edit
Making the menu items checkable with android:checkable="true" will show a checkbox next to the empty spot where the text is supposed to be.
layout/main_activity.xml:
<androidx.coordinatorlayout.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"
tools:context=".ui.MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.PickupList.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/main_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:tabIndicatorColor="#color/purple_200"
app:tabTextColor="#color/teal_200" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/main_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
layout/main_menu.xml:
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/mainmenu_manage"
android:icon="#android:drawable/ic_menu_manage"
android:title="#string/activitymain_menu_manage" />
<item
android:id="#+id/mainmenu_about"
android:icon="#android:drawable/ic_menu_info_details"
android:title="#string/activitymain_menu_abount" />
</menu>
themes/themes.xml (almost unchanged):
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.PickupList" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/purple_500</item>
<item name="colorPrimaryVariant">#color/purple_700</item>
<item name="colorOnPrimary">#color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/teal_200</item>
<item name="colorSecondaryVariant">#color/teal_700</item>
<item name="colorOnSecondary">#color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
<style name="Theme.PickupList.ActionBar">
<item name="windowActionBar">true</item>
<item name="windowNoTitle">true</item>
</style>
<style name="Theme.PickupList.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="Theme.PickupList.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
MainAction.java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...
Toolbar toolbar = findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
// works fine
if (id == R.id.mainmenu_about) {
Toast.makeText(this, "about", Toast.LENGTH_SHORT).show();
return true;
} else if (id == R.id.mainmenu_manage) {
Toast.makeText(this, "manage", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sorry for the code-dump. I am at my wits end with what else I can do here.
Edit / Workaround:
So in the end I came up with a workaround.
I manually added a menu item with the icon for the overflow menu to the Toolbar and set its showAsAction="always" property.
In the click handler I then create custom PopupMenu and anchor it to the Toolbar with Gravity.END.
The PopupMenu will then display it's items correctly.
I do not mark this solution as an answer, although it effectively solves my problem. It solves the problem with the bad aftertaste, that the android api has defeated me and I still have hope that I (or someone else) sees an error and comes up with a solution that uses the internal overflow menu mechanics.
I just got the same problem you described, no ripple effect whatsoever, but got a response while clicking in the white box from the menu.
Fixed it by adding the following line in my styles.xml
<item name="android:textColor">#000000</item>
I've only started learning android recently and I've encountered the following problem:The toolbar doesn't go all the way Here's the code:
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" >
</android.support.v7.widget.Toolbar>
activity_main.xml
<?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"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<include
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="#layout/toolbar" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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.
return true;
}
}
menu_main.xml
<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="android.example.user.gsmnet.MainActivity"
app:contentInsetLeft="0dp"
android:layout_width="match_parent">
<item
android:id="#+id/action_home"
android:orderInCategory="1"
android:title="Home"
android:icon="#drawable/ic_home"
app:showAsAction="always" />
<item
android:id="#+id/action_search"
android:orderInCategory="2"
android:title="Home"
android:icon="#drawable/ic_home"
app:showAsAction="always" />
<item
android:id="#+id/action_cart"
android:orderInCategory="3"
android:title="Home"
android:icon="#drawable/ic_home"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_favorites"
android:orderInCategory="4"
android:title="Home"
android:icon="#drawable/ic_home"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_user"
android:orderInCategory="5"
android:title="Home"
android:icon="#drawable/ic_home"
app:showAsAction="ifRoom" />
</menu>
I've tried to remove the app name to make space for the actions (as seen in MainActivity), but only the 2 that have "always" on showAsAction of the 5 show up.
Also, as an extra, could anyone please tell me how to align the actions from left to right?
Menu items to be shown on app bar depends on screen width in dp (density independent pixels )
Below is the link to find reference
https://developer.android.com/design/media/action_bar_pattern_table.png
To confirm this , some of the android phones have
"Smallest width" option in developer options
You can edit that,
Just increase it above 500 and you would be able to see 4 icons visible on action bar
Also you can verify by using an emulator or a phone with higher screen density
By screen density, I mean smallest screen width
Thanks
I have the same problem as posted many times like here or here. I define a menu item which shows up in the preview in AndroidStudio:
But when I run the app on my phone the icon (a png image) is not visible, and there is a lot of space available. However, this 'Add' option shows up in the Options menu (to the very right; together with 'Srttings'). Here is my menu.xml:
<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=".MainActivity">
<item android:id="#+id/action_favourite"
android:icon="#mipmap/ic_add"
android:title="#string/menu_add"
android:showAsAction="always"/>
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
I tried the suggestions I could find, but none of them solved my problem. My phone is a LG G3. How can I solve this problem?
Additional information: onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Just use app:showAsAction="always" and xmlns:app="http://schemas.android.com/apk/res-auto" then it will show.
<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=".MainActivity">
<item android:id="#+id/action_favourite"
android:icon="#mipmap/ic_add"
android:title="#string/menu_add"
app:showAsAction="always"/>
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
You're probably using the support library which is dependent on the app namespace. If in doubt, just add the same property twice (android: and app: namespaces).
In onCreate() add setHasOptionsMenu(true)
And you are probably inflating the wrong menu file.
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;
}
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>