--ADVERTISEMENT--

Android WebView is used to display web content in android app. To display web page in android application, WebView uses webkit. In this example, I will show how to embed and play YouTube video in android web view. Embedding YouTube video in android web view is very simple. Follow the following instructions to embed and play YouTube video in android app.

Related:
Implementing Navigation Back Feature to Android WebView Application

How to Load HTML Data/String on Android WebView Application

Embed and Play YouTube Video in Android WebView


To embed YouTube video in android web view, first you have to add WebView to your XML activity layout and add little bit code in java activity file.

Create a new Android Project


Let’s start creating a new android project to embed YouTube video in WebView with following information.
Application Name: Embed YouTube Video in WebView
Company Domain: viralandroid.com
Package Name: com.viralandroid.embedyoutubevideoinwebview
Minimum SDK: Android 2.2 (API 8 Froyo)

Add WebView in XML Layout


You can directly drag and drop WebView to your app xml layout. After adding WebView, your XML activity layout looks like this.
res/layout/activity_main.xml

Add YouTube Embed Code in Java Activity


Following is the final code of java activity file. Here, I have added YouTube video embed code in string and some code to control web view.
src/MainActivity.java

Add Internet Permission


Before running your application, you have to add internet permission in your AndroidManifest.xml file. Final code of your manifest file is looks like this.
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
AndroidManifest.xml

You have done all things. Now run your application, your app will like this.

Embed YouTube Video in Android WebView

Download Complete Example Project

Download complete Embed YouTube Video in Android WebView Example project source code from GitHub.

Post a Comment

Powered by Blogger.