How to integrate shouldOverrideUrlLoading in my Activity

1.4k views Asked by At

I have 6 tabs and 6 webviews i want to add this

       public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.startsWith("tel:")) { 
            Intent intent = new Intent(Intent.ACTION_DIAL,
                    Uri.parse(url)); 
            startActivity(intent); 
    }else if(url.startsWith("http:") || url.startsWith("https:")) {
        view.loadUrl(url);
    }
    return true;
}

only for tab 6 in my MainActivity.java to handle the tel: links how can i make it work with my code ?

import android.app.ActionBar;
import android.app.FragmentTransaction;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings;


public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the three primary sections of the app. We use a
     * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
     * will keep every loaded fragment in memory. If this becomes too memory
     * intensive, it may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    CollectionPagerAdapter mCollectionPagerAdapter;

    /**
     * The {@link android.support.v4.view.ViewPager} that will display the
     * object collection.
     */
    ViewPager mViewPager;
    WebView myWebView;
    WebView tab1;



    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Create an adapter that when requested, will return a fragment
    // representing an object in
    // the collection.
    //
    // ViewPager and its adapters use support library fragments, so we must
    // use
    // getSupportFragmentManager.
    mCollectionPagerAdapter = new CollectionPagerAdapter(
        getSupportFragmentManager());

    // Set up action bar.
    final ActionBar actionBar = getActionBar();

    // Specify that the Home/Up button should not be enabled, since there is
    // no hierarchical
    // parent.
    actionBar.setHomeButtonEnabled(false);

    // Specify that we will be displaying tabs in the action bar.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Set up the ViewPager, attaching the adapter and setting up a listener
    // for when the
    // user swipes between sections.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setOffscreenPageLimit(6);
    mViewPager.setAdapter(mCollectionPagerAdapter);
    mViewPager
        .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
            // When swiping between different app sections, select
            // the corresponding tab.
            // We can also use ActionBar.Tab#select() to do this if
            // we have a reference to the
            // Tab.
            actionBar.setSelectedNavigationItem(position);
            }
        });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mCollectionPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter.
        // Also specify this Activity object, which implements the
        // TabListener interface, as the
        // listener for when this tab is selected.
        actionBar.addTab(actionBar.newTab()
            .setText(mCollectionPagerAdapter.getPageTitle(i))
            .setTabListener(this));
    }
    }

    public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    }

    public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onBackPressed(){
  // Rezolvarea butonului back
     WebView webViewa = (WebView) findViewById(R.id.webView1);
     WebView webViewb = (WebView) findViewById(R.id.webView2);
     WebView webViewc = (WebView) findViewById(R.id.webView3);
     WebView webViewd = (WebView) findViewById(R.id.webView4);


     if ( webViewa.canGoBack()) {
     webViewa.goBack();            
       }
     if ( webViewb.canGoBack()) {
         webViewb.goBack();
           }
     if ( webViewc.canGoBack()) {
         webViewc.goBack();            
           }
     if ( webViewd.canGoBack()) {
         webViewd.goBack();            
           }





    }


    public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the primary sections of the app.
     */
    public class CollectionPagerAdapter extends FragmentPagerAdapter {

    final int NUM_ITEMS = 6; // number of tabs

    public CollectionPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new TabFragment();
        Bundle args = new Bundle();
        args.putInt(TabFragment.ARG_OBJECT, i);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        String tabLabel = null;
        switch (position) {
        case 0:
        tabLabel = getString(R.string.label1);
        break;
        case 1:
        tabLabel = getString(R.string.label2);
        break;
        case 2:
        tabLabel = getString(R.string.label3);
        break;
        case 3:
        tabLabel = getString(R.string.label4);
            break;
        case 5:
            tabLabel = getString(R.string.label6);
                break;
        case 4:
            tabLabel = getString(R.string.label5);
                break;
        }

        return tabLabel;
    }
    }





    /**
     * A fragment that launches other parts of the demo application.
     */
    public static class TabFragment extends Fragment {

    public static final String ARG_OBJECT = "object";

    private WebView webView;





    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        Bundle args = getArguments();
        int position = args.getInt(ARG_OBJECT);

        int tabLayout = 0;
        switch (position) {
        case 0:
        tabLayout = R.layout.tab1;
        break;
        case 1:
        tabLayout = R.layout.tab2;
        break;
        case 2:
        tabLayout = R.layout.tab3;
        break;
        case 3:
        tabLayout = R.layout.tab4;
        break;
        case 5:
        tabLayout = R.layout.tab6;
        break; 
        case 4:
        tabLayout = R.layout.tab5;
        break; 

        }

        View rootView = inflater.inflate(tabLayout, container, false);

        webView = (WebView) rootView.findViewById(R.id.webView1);
        WebView tab2 = (WebView) rootView.findViewById(R.id.webView2);
        WebView tab3 = (WebView) rootView.findViewById(R.id.webView3);
        WebView tab4 = (WebView) rootView.findViewById(R.id.webView4);
        WebView tab5 = (WebView) rootView.findViewById(R.id.webView5);
        WebView tab6 = (WebView) rootView.findViewById(R.id.webView6);




        if (webView != null) {
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("file:///android_asset/tab1.html");
        }

        if (tab2 != null) {
            tab2.setWebViewClient(new WebViewClient());
            tab2.loadUrl("file:///android_asset/tab2.html");
            }

        if (tab3 != null) {
            tab3.setWebViewClient(new WebViewClient());
            tab3.loadUrl("file:///android_asset/tab3.html");
            }

        if (tab4 != null) {
            tab4.setWebViewClient(new WebViewClient());
            tab4.loadUrl("file:///android_asset/tab4.html");
            }
        if (tab5 != null) {
            tab5.setWebViewClient(new WebViewClient());
            WebSettings tb5 = tab5.getSettings();
            tb5.setJavaScriptEnabled(true);
            tab5.loadUrl("http://fbhostinger.com/po/map.html");
            }

        if (tab6 != null) {
            tab6.setWebViewClient(new WebViewClient());
            tab6.loadUrl("file:///android_asset/tab5.html");

            }




        return rootView;
    }
    }






}

also my tab6 loads a html file from assets with this

  if (tab6 != null) {
            tab6.setWebViewClient(new WebViewClient());
            tab6.loadUrl("file:///android_asset/tab5.html");

            }
1

There are 1 answers

0
Matt Gaunt On

I think you just need to do:

if (tab6 != null) {
    tab6.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith("tel:")) { 
                Intent intent = new Intent(Intent.ACTION_DIAL,
                    Uri.parse(url)); 
                startActivity(intent);
                return false;
            }
            return true;
        }
    });
    tab6.loadUrl("file:///android_asset/tab5.html");
}

Please note the adaption of no longer calling view.loadUrl('...'); <- if you return true in shouldOverrideURLLoading, it will load the url into the WebView.