0

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?

Toolbar

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>
KIM
  • 15
  • 8
  • Hi, try this and let me know it works or not? http://stackoverflow.com/questions/25930380/android-search-widgethow-to-hide-the-close-button-in-search-view-by-default – Jay Sep 12 '16 at 20:53

2 Answers2

0

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.

Nohus
  • 739
  • 8
  • 16
  • But then how do I write the if statement for user login status? like if user is in logged session then show the search button. How should I write it in MainActivity.java?? – KIM Sep 12 '16 at 21:43
  • Because login is not a part of activity in android. It is done in my website. – KIM Sep 12 '16 at 21:46
  • if (!url.contain("login")) { getMenuInflater().inflate(R.menu_main, menu); and following scripts. I didn't write else statement and then return super.onCreateOptionsMenu(menu); } This didn't work. – KIM Sep 13 '16 at 00:48
  • Is the login URL actually containing "login"? Does the search icon disappear when you comment out the entire `onCreateOptionsMenu()`? – Nohus Sep 13 '16 at 01:21
  • Yes, when I remove onCreateOptionsMenu() the search icon disappeared. – KIM Sep 13 '16 at 20:53
  • Well then there is no reason why it shouldn't work if you wrapped it in an `if` statement. Try `Log`ging the URL returned by the `WebView` to see if it's like you think it is. – Nohus Sep 13 '16 at 21:00
  • I tried many times but it seems like it is not working. What actually mWebView.getUrl() outputs? for example, does it output string value of "http://asdfasdfafs.com/login/"? if the current url is that? – KIM Sep 20 '16 at 17:02
  • Like I said, it would be best for you to just try outputting whatever `getUrl()` returns and see in LogCat what is it. Or you can use the debugger to make a breakpoint before `getUrl()`, you can then use the "Evaluate expression" button to execute any code you want and see it's output. You can use that to see what methods return. – Nohus Sep 21 '16 at 12:08
0

In your styles for this activity use this attribute in your style for this activity

<item name="android:windowNoTitle">true</item>
Shadab K
  • 1,677
  • 1
  • 16
  • 25