-1

I am building an Android App for my group in my locality. I want to implement a feature into the app. I'm using Web View to cache the webpage's text only although the webpage comes with an images. The images which comes with text should be displayed only when there is an internet connection.

Please show me a way to achieve this.

UPDATE

    WebView web;
    ProgressBar progressBar;
    final Activity activity = this;
     TextView textview;
     final String url="http://mywebpages.com";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.executives_layout);

        web = (WebView) findViewById(R.id.webView01);
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        textview = (TextView) findViewById(R.id.progressText);


        web.setWebViewClient(new myWebClient());

        web.getSettings().setSupportZoom(true);
        web.getSettings().setBuiltInZoomControls(true);
        web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        web.setScrollbarFadingEnabled(true);
        web.getSettings().setLoadsImagesAutomatically(true);
        if(isNetworkAvailable()){
            web.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

            web.getSettings().setJavaScriptEnabled(false);
            web.loadUrl(url);
        }
        else{
                web.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);
                web.loadUrl(url);

               Toast.makeText(activity, "" + "Network Access Error: Check Network Connections", Toast.LENGTH_LONG)
               .show();

        }
    }


    public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            view.loadUrl(url);
            return true;

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

            progressBar.setVisibility(View.GONE);
            textview.setVisibility(View.GONE);
        }

         public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {
             String header = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
             String myHtmlString="<center><p>Oooops!</p>"
                    + "<p> Sorry,network settings<br/>"
                    + "is lost.</p></center>";
             view.loadData(header+myHtmlString, "text/html", "UTF-8");


         }
    }

    // To handle "Back" key press event for WebView to go back to previous screen.
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
            web.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager 
              = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }

NB: Please this is what I have done so far, this is able to cache all the web page content including the images, however i want to be able to cache only the texts that comes with the web page and display the images only when there is an internet connection.

NewBIe
  • 299
  • 6
  • 17
  • Please why the down vote? – NewBIe Aug 30 '15 at 06:57
  • I did not downvoted, but you should show your code and efforts in order to get help. What have you done so far? What did you try? Where is the problem? – TDG Aug 30 '15 at 07:21
  • This question is way to broad, stackoverflow is not a code generation site, please show your due-diligence on your side to handle your research and ask a specific question about a particular problem you're having. – JoxTraex Aug 30 '15 at 08:09
  • Hello buddies, I am sorry for not showing what i have done earlier, I have now updated my question, hope this helps clearifies the question – NewBIe Aug 30 '15 at 08:52

2 Answers2

1

Use BroadcastReceiver to get the information for internet connection. Please refer to Detect whether there is an Internet connection available on Android

Edit

Hello again, so, you need an algorithm to achieve your goal.

One way came up to my mind just now is that, when there is no internet connection, just parse "img" tags in your html(?!) or whatever your string is (or what the image tag is, for your string).

On the other hand, when you have an internet connection, just show whole html code.

Please refer to this link for html version :

Remove image elements from string

Community
  • 1
  • 1
Adnan Bal
  • 213
  • 3
  • 16
0

In order to listen to connectivity changes, you should listen to the intents from ConnectivityManager and then respond accordingly. You can also use some supplemental APIs in the ConnectivityManager that allow you to detect whether you are connected to anything as well.

Ref

http://developer.android.com/reference/android/net/ConnectivityManager.html

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
  • My problem is how can I cache only the text files – NewBIe Aug 30 '15 at 13:30
  • What do you mean "cache only the text file"? Its just a collection of data isn't it? Where is it? – JoxTraex Aug 30 '15 at 23:26
  • I mean my data from the url comes with a text and images. I want to only cache the text, so assuming a user goes to the page without an internet access, he can only view the text and when there is Internet access he can view the text with the images – NewBIe Aug 30 '15 at 23:37