简体   繁体   中英

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.

在此活动的样式中,为此活动在您的样式中使用此属性

<item name="android:windowNoTitle">true</item>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM