Android Studio Menu Icon duplicated - android

I have the menu icon (3 dots) duplicated I used the following code:
#Override public boolean onPrepareOptionsMenu(final Menu menu) {
menu.clear();
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return super.onCreateOptionsMenu(menu);
}
but it didn't solve my problem is it because I'm using androidx libraries?
My XML code for the menu:
<?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:title="more"
android:id="#+id/moreVertical"
android:icon="#drawable/baseline_more_vert_black_18dp"
app:showAsAction="always" />
<item android:title="#string/titlecontactus" android:id="#+id/mnuContactUs"/>
<item android:title="#string/titlepolicy" android:id="#+id/mnuPolicy"/>
my toolbar XML code in the activity:
<androidx.appcompat.widget.Toolbar
android:id="#+id/my_toolbar" app:title="#string/title"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</androidx.appcompat.widget.Toolbar>

I just removed the item I made for 3 dots which has the title more and everything worked fine it seems the 3 dots appear by default and no need to add an item for it or download its 3 dots image.It seems the article I followed was wrong or may be out of date.Hope someone could help on my second question:
Copy sqlite in android studio assets not working

Related

Android Toolbar in webapp

I am developing a web app, and here I have a problem.
I have a tool bar(android widget toolbar) with logo and search button
As you can see I have the login page(webview). I want to user to see this search button after users login to the webpage. How should I do that?
Edited:
In my toolbar.xml
<?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:id="#+id/my_toolbar"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#000000"
android:elevation="3dp"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
In my menu_main.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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/action_search"
android:orderInCategory="200"
android:title="#string/action_search"
android:icon="#drawable/ic_action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
And in MainActivity.java onCreate,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
//toolbar.setNavigationIcon(R.mipmap.logo9);
toolbar.setTitle("");
toolbar.setSubtitle("");
......
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
And finally in activity_main.xml, I just include the toolbar,
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
></include>
If the search button is an Android View, you can hide it by calling View.setVisibility(View.GONE).
If you have instead defined it as a menu XML resource and loaded it in this Activity, then just don't load the menu resource unless the user is logged in. That is done in public boolean onCreateOptionsMenu(Menu menu) of the Activity.
If you go to another Activity after logging in, then just use a separate Toolbar there, with the icon like here, but without one in the login Activity.
Please provide more information on how you added the search icon to the Toolbar in the first place, then I will be able to help you more.
Edit:
You can use the WebView.getUrl() method to get the URL of the displayed website, if your login page is on login.php, you can look if the URL is that, and then not show the search icon. For example you can check the URL with String.contains(), like: .contains("login.php"). And if that is true don't show the button.
In your styles for this activity use this attribute in your style for this activity
<item name="android:windowNoTitle">true</item>

Dynamicly add items to NavigationDrawer menu

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).

ActionBar Menu icons only shows on overflow menu

I am currently having an issue showing the option menu items on the actionbar. For some reason, the items only show on the overflow menu and this is true for post ICS devices as well. I am using the v7-appcompat library to support sdks from version 8 onwards.
In the main activity I have:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.wild_news, menu);
return true;
}
My menu items are defined as follow:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
**xmlns:app="http://schemas.android.com/apk/res-auto"** >
<item
android:id="#+id/menu_refresh"
android:icon="#drawable/ic_action_refresh"
**app:showAsAction="always"**
android:title="#string/menuTitle_refresh"/>
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
**app:showAsAction="ifRoom"**
android:title="#string/action_settings"/>
I also have a spinner in the actionbar with the following xml layout:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:minWidth="140dp"
android:layout_gravity="center_vertical"
style="#style/ActionBarNavListTitle" />
I can't seem to get my head around it.
Great I found the issue to be related to a mismatch in the ActionBar style definition which I have resolved now:
<style name="ActionBarStyle" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">

android.support.v7 with `ActionBarActivity` no menu shows

In the new update Google has released a new API support library, that supports the ActionBar in API level 7+.
I used ActionBarSherlock until this update and I wrote the code to load the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
return true;
}
and the menu file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/item_menu_ok" android:icon="#drawable/ic_action_ok"
android:title="#string/ok" android:showAsAction="always"></item>
<item android:id="#+id/item_menu_cancel" android:icon="#drawable/ic_action_cancel"
android:title="#string/cancel" android:showAsAction="always"></item>
</menu>
To set up the menu buttons on the action bar. This code worked perfectly with ActionBarSherlock. But when I changed the action bar to the new support library, the buttons are not shown in the action bar. Even if they are set as android:showAsAction="always". And when I debug the code, the function menu.getSize() return 2, and that is correct, but no buttons are shown..
Why are the buttons not shown in the new support library?
Try pressing the MENU button on your device or emulator, and see if they appear in the overflow.
If they do, then the problem is that your <menu> XML needs to change. Menu XML that works with ActionBarSherlock and the native API Level 11+ action bar will not work with the AppCompat action bar backport.
Your menu XML would need to look like this:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
>
<item android:id="#+id/item_menu_ok" android:icon="#drawable/ic_action_ok"
android:title="#string/ok" yourapp:showAsAction="always"></item>
<item android:id="#+id/item_menu_cancel" android:icon="#drawable/ic_action_cancel"
android:title="#string/cancel" yourapp:showAsAction="always"></item>
</menu>
And you would need to use the same yourapp prefix for anything else related to the action bar (e.g., yourapp:actionLayout).
You can see this covered in the action bar documentation.
I'd like to add a little to the answer.
If you want to see both text and an icon, please use withText in showAsAction
I've just tested it; when I used always or ifRoom without withText, I only saw an icon.

Webview doesn't fit fullscreen with action bar on

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.

Categories

Resources