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>
Related
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
First, I will explain to you what I want to do. Then, I will show you what I've already done. Finally, I will ask for your help about what I will implement.
My aim
My aim is to have a Toolbar like the Playstore:
When you click on the burger menu, it displays the menu.
When you click on the input, it displays changes the icon and it displays keyboard:
What I have for now
I followed the documentation (https://developer.android.com/training/appbar/ + https://developer.android.com/guide/topics/search/search-dialog + https://developer.android.com/training/search/setup). Thus, I have:
res/menu/action_bar_menu.xml containing my SearchView widget
<?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/searchView"
android:title="#string/search_title"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
</menu>
A Toolbar in my fragment's layout:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorRoyalRed"
android:theme="#style/ToolbarStyle"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
The style ToolbarStyle (see above):
<style name="ToolbarStyle">
<item name="android:background">#color/colorRoyalRed</item>
<item name="android:textColorPrimary">#color/colorRichYellow</item>
<item name="actionButtonStyle">#style/ToolbarActionButton</item>
</style>
<style name="ToolbarActionButton">
<item name="android:padding">0dp</item>
<item name="android:paddingStart">0dp</item>
<item name="android:paddingEnd">0dp</item>
</style>
In the fragment class, I make use of the toolbar:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
Toolbar toolbar = view.findViewById(R.id.toolbar);
toolbar.setTitle("");
AppCompatActivity app_compat_activity = (AppCompatActivity) getActivity();
Objects.requireNonNull(app_compat_activity).setSupportActionBar(toolbar);
app_compat_activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
return view;
}
In the fragment class, I make use of the SearchView:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menu_inflater) {
menu_inflater.inflate(R.menu.action_bar_menu, menu);
super.onCreateOptionsMenu(menu,menu_inflater);
AppCompatActivity app_compat_activity = (AppCompatActivity) getActivity();
SearchView search_view = (SearchView) menu.findItem(R.id.searchView).getActionView();
search_view.setIconifiedByDefault(false);
SearchManager search_manager = (SearchManager) Objects.requireNonNull(app_compat_activity).getSystemService(Context.SEARCH_SERVICE);
search_view.setSearchableInfo(Objects.requireNonNull(search_manager).getSearchableInfo(app_compat_activity.getComponentName()));
}
Well, there are other implementations (modifications concerning the manifest, etc.) but they are not important (note all the required changements to implement a simple search bar in the toolbar... more than 5 steps...).
What I would want to do next (help please!)
I don't want to use anything other than the menu items structure (thus I don't want to actually add the SearchView in the fragment's layout).
How could I change the SearchView's icon? For now it's a magnifying glass. It must be a burger menu. I tried to modify res/menu/action_bar_menu.xml by indicating a android:icon= to the SearchView but it didn't work. I also tried to use <item name="searchIcon">#drawable/ic_baseline_menu_24px</item> in the style ToolbarActionButton but it didn't work. Here the aim is to replace the glass by a burger menu icon.
How could I handle the click on the SearchView's burger menu icon (once it will be set)? Indeed, I will call my DrawerLayout::openDrawer function to open my menu.
How could I handle the click on the SearchView's content? Indeed, I will replace the burger menu by the left arrow (and, if clicked, keyboard will disapear and the burger menu will be displayed).
Am I using the wrong things?
I just followed Google's documentation. But it seems so complicated to me to realize that I may be on the wrong path? Should I stop using the menu items structure?
You may try SearchEditTextLayout available under this Google project -
https://android.googlesource.com/platform/packages/apps/Dialer/
These libraries may also help you to achieve your desired results.
SearchView and FloatingSearchView
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).
I have in the following Activity this Toolbar:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_select_currency"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
Also the menu is declared in:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/search"
android:title="#string/search_currency"
android:icon="#drawable/ic_action_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
In the activity I call:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.currency_selector_options_menu, menu);
return true;
}
private void initActionBar() {
// Initialize the Toolbar( this is the new ActionBar in Lollipop)
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_select_currency);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(getString(R.string.select_currency));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Now my issue is that I do not get the SearchView like in normal ActionBar, what I get is a normal menu. How can I get a normal SearchView ?
Use your app namespace:
xmlns:myapp="http://schemas.android.com/apk/res-auto"
myapp:showAsAction="always"
Explanation:
Since you are using a support library for old android versions and it is not included into the sdk, the android namespace doesn't contains the showAsAction attribute, which is rather contained into the support library you linked to your project, so to reach the attribute you must use your app namespace
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);
}