0

convert a html to android Application using android webview

This is MainActivity.java file:

package bd.edu.httpdaffodilvarsity.htmlconvert;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String myURL = "file:///android_asset/www/index.html";
    WebView webView_KSera_Convert_HTML_to_Android =(WebView)findViewById(R.id.webViewKSeraConvertHTMLtoAndroid);
webView_KSera_Convert_HTML_to_Android.getSettings().setJavaScriptEnabled(true);
     webView_KSera_Convert_HTML_to_Android.setWebViewClient(new WebViewClient());

    webView_KSera_Convert_HTML_to_Android.loadUrl(myURL);


 }
 }

This is activity_main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="bd.edu.httpdaffodilvarsity.htmlconvert.MainActivity">

<WebView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/webViewKSeraConvertHTMLtoAndroid"/>
</RelativeLayout>

AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bd.edu.httpdaffodilvarsity.htmlconvert">

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

index.html file:

<!Doctype html>
<html>

<head>head</head>
<body>
<h1>Hello World</h1>

<h4>Hello</h4>
</body>
</html>

build.gradle file:

apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.2"

defaultConfig {
    applicationId "bd.edu.httpdaffodilvarsity.htmlconvert"
    minSdkVersion 10
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
}

Error in Emulator:

Show this message when run imulator

Folder structure:

I added assets folder under res folder

Brijesh Kumar
  • 1,685
  • 2
  • 17
  • 28
Mahmud Hasan
  • 33
  • 5
  • 17
  • Possible duplicate: http://stackoverflow.com/questions/4027701/loading-existing-html-file-with-android-webview – Tosin Onikute Oct 23 '16 at 10:37
  • 1
    It's been a while since I've done this with a `WebView`, but I'm pretty sure the `www/` folder needs to be a subfolder under `assets/` - `assets/www/`. In your screenshot, it looks like you've misnamed the `assets/` folder as `assets.www/`. – Mike M. Oct 23 '16 at 10:41
  • Thanks All I Solve my problem creating folder following way from java folder: Right click on Java folder: new > folder> Assets Folder – Mahmud Hasan Oct 23 '16 at 12:28

1 Answers1

0

I Solve my problem creating folder following way from java folder:

Right click on Java folder:

new > folder> Assets Folder

Mahmud Hasan
  • 33
  • 5
  • 17