Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed HTML code in Android app?

I am writing an app for which I need to embed HTML object in the Android app, I followed the tutorial given here: http://mobile.tutsplus.com/tutorials/android/android-sdk-embed-a-webview-with-the-webkit-engine/ and modified the code accordingly, following is the code, but I am getting a white screen...

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

        WebView engine = (WebView) findViewById(R.id.web_engine);

    engine.getSettings().setJavaScriptEnabled(true);
    engine.getSettings().setPluginsEnabled(true); 
    engine.getSettings().setAppCacheEnabled(false); 
    engine.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 

        String data = "<html>" +
                "<body>" +
                "bb"+
                "<object width=\"450\" height=\"300\" align=\"middle\"><param name=\"movie\" value=\"http://www.raaga.com/widgets/top10/widgettopten.swf?l=H&q=1\" /><param name=\"wmode\" value=\"transparent\"><embed src=\"http://www.raaga.com/widgets/top10/widgettopten.swf?l=H&q=1\" width=\"450\" height=\"300\" align=\"middle\" type=\"application/x-shockwave-flash\" wmode=\"transparent\"  FlashVars=\"gig_lt=1312323434023&gig_pt=1312325057958&gig_g=2\"/> <param name=\"FlashVars\" value=\"gig_lt=1312323434023&gig_pt=1312325057958&gig_g=2\" /></object>"+
                "gg"+
                "</body>" +
                "</html>";

        engine.loadData(data, "text/html", "UTF-8");
    }
}

The object that I want to embed is:

<object width="450" height="300" align="middle"><param name="movie" value="http://www.raaga.com/widgets/top10/widgettopten.swf?l=H&q=1" /><param name="wmode" value="transparent"><embed src="http://www.raaga.com/widgets/top10/widgettopten.swf?l=H&q=1" width="450" height="300" align="middle" type="application/x-shockwave-flash" wmode="transparent"  FlashVars="gig_lt=1312323434023&gig_pt=1312326303531&gig_g=2"/> <param name="FlashVars" value="gig_lt=1312323434023&gig_pt=1312326303531&gig_g=2" /></object>
like image 222
Nohsib Avatar asked Aug 02 '11 23:08

Nohsib


People also ask

Can I use HTML in Android app?

The Short answer: Yes, you can develop apps using HTML / CSS / Javascript.

Can I convert HTML to APK?

To convert your HTML app to APK, you need to open HTML App Template on AppsGeyser, and insert your code. After that, you need to name the app and upload the icon. It takes up to 5 minutes to build an apk.

Can I use HTML in Android Studio?

Step 1: To add a local HTML file into your Android project there must be an asset folder in it. To create an asset folder in Android studio open your project in Android mode first as shown in the below image. Step 2: Go to the app > right-click > New > Folder > Asset Folder and create the asset folder.

Where do I put HTML files on Android?

Go to assets > right-click > New > File and a dialog box will pop up. 6: Or you can simply paste your html files into assets folder created in step 4.


2 Answers

Put the file in the /assets directory, you can access it with the file:///android_asset/ directory.

Instead of putting the html in code the assets folder is where you can put static data files such as HTML and its related CSS, images, and JavaScript files, etc. Then you can use WebView.loadUrl(). This has the advantage of not having to be in code with Java String escapes and cumbersome editing. You will be able to create the file(s) separately, in your favorite web project editor, then copy and paste them. If you need to edit the file at runtime you can still load the file from the assets directory and still not have to worry about Java String escapes and cumbersome editing. Then apply the edits and pass to WebView.loadData().

To help debug this problem implement WebChromeClient.onLoadResource and check out the console message tutorial.

Don't forget to use PackageManager.getPackageInfo() to check if flash is available. If its not you can give the user the option to download it from the market.

like image 154
Dan S Avatar answered Sep 28 '22 21:09

Dan S


Android has no official Flash viewer, so it doesn't know what to do with an embedded SWF.

If you put other (visible) HTML in your code, you should be able to see it in the view.

If you try to access your SWF through the webbrowser, you should get a broken/missing plugin icon.

like image 42
Tim Sparkles Avatar answered Sep 28 '22 21:09

Tim Sparkles