0

I have a simple android screen which consists of a button as shown below

Android screen with button

I also have a local webpage named webpage.html stored within my assets folder on android studio.

enter image description here

When the button on the android screen is clicked, I want to open the local webpage that I have stored. If I try opening http://www.google.com it works just fine. But it seems to have a problem in opening any local page for now. Following is my MainActivity.java code snippet -

//click this button to open a web page 
    public void onOpenWebBrowser(View v)
    {
        Intent webPageIntent = new Intent(Intent.ACTION_VIEW);
        webPageIntent.setData(Uri.parse("/Users/user/AndroidStudioProjects/ProjectName/app/src/main/assets/webpage.html"));

        try {
            startActivity(webPageIntent);
        } catch (ActivityNotFoundException ex) {
            // can do something about the exception.
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

And here are the contents of my activity_main.xml file -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="onOpenWebBrowser"
        android:text="Open Web Browser" />

</RelativeLayout>

P.S - The webpage works fine when and if I open it from the outside, in a browser. Please help!

TheLuminor
  • 1,411
  • 3
  • 19
  • 34
  • you are trying to open your local page on a default web browser app of your phone right? not on a WebView? – user1506104 Apr 05 '17 at 13:04
  • Yeah I was trying to use the default web browser of my phone, but I'd make do with a webView also, after I've pulled out so many of my hair strands already! @user1506104 – TheLuminor Apr 05 '17 at 13:06
  • Possible duplicate of [Open HTML file from assets folder](http://stackoverflow.com/questions/29939174/open-html-file-from-assets-folder) – user1506104 Apr 05 '17 at 13:16
  • Okay I shall take a look at it! Thank you for your help! @user1506104 – TheLuminor Apr 05 '17 at 13:17

3 Answers3

0

First add webview layout in your activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity" >
            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onOpenWebBrowser"
                android:text="Open Web Browser" />

        <WebView
            android:id="@+id/web_view"
            android:layout_below="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        </RelativeLayout>

Now in MainActivity.java

WebView webView = (WebView)findViewById(R.id.web_view);
webView.loadUrl("file:///android_asset/filename.html");

This will work!

Hope this help!

Wilson Christian
  • 650
  • 1
  • 6
  • 17
  • It says that `webView cannot be resolved` even after I've trying a quick fix for the import, which failed. @Wilson Christian – TheLuminor Apr 05 '17 at 13:08
  • I guess im going wrong somewhere, I get an error that says webpage could not be loaded because the file was not found! :/ – TheLuminor Apr 05 '17 at 14:05
0

You may try this on the place of Intent:-

WebView wb = new WebView(this);
    wb.loadUrl("file:///android_asset/index.htm");
    setContentView(wb);

Replace this dummy url path with your file path.

Hope it may help you. :)

Rajshree Tiwari
  • 630
  • 1
  • 7
  • 27
0

You can show a html in a text view but only if you're only doing basic HTML tagging, this will work:

try
{
  InputStream inputStream = getResources().getAssets().open("myFile.html");

  String html = IOUtils.toString(inputStream);

  myTextView.setText(Html.fromHtml(html));
}
catch (IOException exception)
{
  myTextView.setText("Failed loading html.");
}
Simo
  • 345
  • 4
  • 12