How To make YouTube Android App In Android Studio [Step By Step]
Do you know creating YouTube Android App is so easy as you just need to understand how to use YouTube API for that.How to Convert your YouTube Channel into an App for Free
In this application we will share about adding YouTube functionality to your Android application. Further we will also create playlist and run on real device. Will make use of multiple Android UI components to design and step by step developing a YouTube App in Android Studio.
Topics Used For Creating YouTube App – Before following the below steps it is recommended you check out Image View, Button, Linear Layout & Relative Layout topics. Also go through JAVA OOPS concept once.
Steps To Create a YouTube Application In Android Studio:
Below you can download code, see final output and step by step explanation of YouTube App in Android Studio
Step 1: Firstly get the Android Studio downloaded in your system, then open it.
Step 2: Create a new project choose basic activity and name it YoutubePlayer.
Now please read this tutorial How To choose basic activity.
Step 3: Now click here to download the YouTube Android Player API.
Step 4: After downloading extract the downloaded compressed folder, open it and find a executable jar file in libs folder.
Now please read this tutorial How To Add External JAR Files In Android Studio
Step 5: Copy this library and paste in your YoutubePlayer application app -> libs
Step 6: Add dependencies to build.gradle file and sync. Adding this will make our application compatible to add youtube functionality.
Add in Gradle Scripts >> build.gradle (Module: app)
Now please read this tutorial for Implementing abstract method.
iii of Step 8) Nextly we gonna add listeners in the code as:
youTubePlayer.setPlayerStateChangeListener(playerStateChangeListener);
youTubePlayer.setPlaybackEventListener(playbackEventListener);
iv of Step 8) You need to add Google API Key (it’s a unique key uses to take advantage of youtube functionality) and Youtube Video ID(it’s the id of video we want to play) for that follow following steps:
Open this link first.
You need to login first to get into this link thought your google ID.
Now you need to create a project then name that project.
Firstly change the relative layout to linear layout and add its orientation to vertical also remove the padding in the layout. See the code to be added.
Complete code of activity main file
ativity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webView"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
Step 11: Add users permission for internet in
AndroidManifest.xml.
Adding Internet Permission In Android Studio
<uses-permission android:name="android.permission.INTERNET"/>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dude.technicdude">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.dude.technicdude.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Step 12: Open file color.xml, add button in it which will redirect user to youtube player colors.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#FF0000</color>
<color name="colorPrimaryDark">#580606</color>
<color name="colorAccent">#D81B60</color>
</resources>
Step 13: Now open MainActivity.java class and paste the following code.
MainActivity.java
package com.dude.technicdude;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView);
myWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("https://www.youtube.com/channel/UC4M4mrKDDJoK5gocuCwet8w");
myWebView.setWebViewClient(new WebViewClient());
}
@Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
}
OUTPUT:
Now run this App and use the play the YouTube video you added.
YouTube Video
Conclusion
We have successfully created a YouTube app in android studio
Cheers!
Don’t forget to share this post!