简体   繁体   中英

How to hide Linear Layout toolbar for specific webview url?

I have a Toolbar created in Linear Layout associated with a relative layout webview. How to hide this toolbar for specific urls for eg : 'index.php' My Toolbar code is given below I have tried many times and cant make it work properly with the given answers so updating the code with the xml layout code.

 if (TextUtils.isEmpty(getString(R.string.toolbar))) {
            showToolBar = false;
        }

        if (showToolBar) {

            mSearch = (ImageView) findViewById(R.id.search);
            mAdd = (ImageView) findViewById(R.id.add);
            mProfile= (ImageView) findViewById(R.id.profile);
            mHome= (ImageView) findViewById(R.id.home);
            mSettings= (ImageView) findViewById(R.id.settings);
          //  ImageView mRefresh = (ImageView) findViewById(R.id.refresh);

            mSettings.setOnClickListener(this);
            mHome.setOnClickListener(this);
            mProfile.setOnClickListener(this);
            mSearch.setOnClickListener(this);
            mAdd.setOnClickListener(this);

           // mRefresh.setOnClickListener(this);

        }

        else {
            LinearLayout llToolbarContainer = (LinearLayout) findViewById(R.id.toolbar_footer);
            if (llToolbarContainer != null) {
                llToolbarContainer.setVisibility(View.GONE);
                RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mAdView.getLayoutParams();
                lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            }

XML Layout code , please do help

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <FrameLayout
        android:id="@+id/webview_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:visibility="invisible"
        tools:context=".universalwebview.MainActivity">
        <WebView
            android:id="@+id/webview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scrollbars="none" />
    </FrameLayout>


    <LinearLayout
        android:id="@+id/toolbar_footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="#fff"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="?attr/selectableItemBackground"
            android:clickable="true"
            android:padding="10dp"
            android:src="@drawable/ic_action_home"
            android:tint="@color/tintcolor" />

        <ImageView
            android:id="@+id/search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="?attr/selectableItemBackground"
            android:clickable="true"
            android:padding="10dp"
            android:src="@drawable/ic_action_search"
            android:tint="@color/tintcolor" />

        <ImageView
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="?attr/selectableItemBackground"
            android:clickable="true"
            android:padding="10dp"
            android:src="@drawable/ic_action_add"
            android:tint="@color/tintcolor" />

        <ImageView
            android:id="@+id/profile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="?attr/selectableItemBackground"
            android:clickable="true"
            android:padding="10dp"
            android:src="@drawable/ic_action_profile"
            android:tint="@color/tintcolor" />


        <ImageView
            android:id="@+id/settings"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="?attr/selectableItemBackground"
            android:clickable="true"
            android:padding="10dp"
            android:src="@drawable/ic_action_settings"
            android:tint="@color/tintcolor" />

    </LinearLayout>

    <ImageView
        android:id="@+id/image_splash"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:visibility="visible"
        android:src="@drawable/splash" />
</RelativeLayout>

In Your MainActivity Code you can't make Your Showtoolbar bool variable false by default.As that is the place where your bottom navigation is actually populating.So it may gives a run time error.Instead of that, make your Linear Layout hide when url contains "index.php".

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {

     if(url.contains("index.php") 
      {
       findViewById(R.id.toolbar_footer).setVisibility(View.GONE);
      }
    else
     {
    findViewById(R.id.toolbar_footer).setVisibility(View.VISIBLE);
     }
}

Call this under your WebViewClient .

  1. Get url
  2. Use url.contains("index.php") to judge
  3. set showToolBar = false; or showToolBar = true;
  4. Use if (showToolBar) {} else {}

Try this .

private boolean showToolBar;
private LinearLayout llToolbarContainer;

public void initWebView() {
    llToolbarContainer = (LinearLayout) findViewById(R.id.toolbar_footer);

    String url = "your_url";
    if (url.contains("index.php")) {
        showToolBar = false;
        llToolbarContainer.setVisibility(View.GONE);
    } else {
        showToolBar = true;
        llToolbarContainer.setVisibility(View.VISIBLE);
    }

        mSearch = (ImageView) findViewById(R.id.search);
        mAdd = (ImageView) findViewById(R.id.add);
        mProfile = (ImageView) findViewById(R.id.profile);
        mHome = (ImageView) findViewById(R.id.home);
        mSettings = (ImageView) findViewById(R.id.settings);
        //  ImageView mRefresh = (ImageView) findViewById(R.id.refresh);

        mSettings.setOnClickListener(this);
        mHome.setOnClickListener(this);
        mProfile.setOnClickListener(this);
        mSearch.setOnClickListener(this);
        mAdd.setOnClickListener(this);

        // mRefresh.setOnClickListener(this);           

}

You need to create a function , that will hide an tool bar. And create custom WebViewClient for WebView

 webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
             super.onPageFinished(view, url);
             if (url.contains("index.php")) {
                 hideToolBar();
             }                  
        }
 };

Something like this. Not sure that you need onPageFinished , but you can find more info WebViewClient

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