1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Layouts and Animation for Android Apps

Project 1 Launch Video on Youtube App

Start course
Overview
Difficulty
Intermediate
Duration
4h 7m
Students
1
Description

In this course, you'll start experimenting with XML code and diving deeper into layouts, namely linear and constraint layouts. We'll also look at animations and build a few fun features with them. Then, we'll take a deeper dive into the Kotlin programming language and constraint layouts, before building a fully functioning Tic-Tac-Toe game.

On top of that, we'll then build a second app which can play YouTube videos within it, and you'll learn how to work with APIs and API keys.

Intended Audience

This course is intended for beginners to Android app development or anyone who wants to master coding in Kotlin.

Prerequisites

Since this is a beginner-level course, there are no requirements, but any previous experience with coding would be beneficial.

Transcript

Hello, and welcome back. Over the next few videos, we'll build a couple of apps which deal with playing YouTube videos. And the second app which we'll build, which is the main app, will be the one where we play the YouTube videos within the app that we're running itself. And that will be a bit more complicated involving a few steps and API keys and all that good stuff. But the first one will be a simple one, and all we want to accomplish is the task of playing a YouTube video. So, what our mini app will do is launch the YouTube app in the emulator and play the video. So, let's set this up. First, create a new app. I've called mine YoutubeMiniDemo, you can call yours whatever you like and add view binding to it. 

Mine is all set up and ready to go. So, go ahead, pause the video, and take care of that step. Okay, and here we are. First, I will add a button to my app which when clicked will launch the video. So, I'm going to get rid of this TextView and bring in a Button, okay? I will change the text to Play video, the id of button is okay. Let me drag it towards the middle right here. All right, let's set up the constraints. Okay, that looks good. And I'll also add an onClick, and I can do it from here or I'll get and go to my split screen right here, onClick and I'll call my method playVideo. Okay, and this doesn't exist, so let's go ahead and add it down here. I'm going to say, fun playVideo it's going to take in view of type Android view, right there. Open and close curly braces, all right. 

Now, what we'll be dealing with is a YouTube video URL. So, let me pull up YouTube. Here we go. I'll grab one of my videos, and this one is a mini tutorial on Python, for loops, generators, and list comprehension. And you have to excuse my YouTube channel appearance depending on when you're watching this video. There's not much content in there right now; it's in the works, but I'll grab this one. It's a reasonable video, and you can either copy the link from here, right? 'Command+C' or you can go to Share. And here, that's the link I'm going to copy, all right?

So, 'Command+C' back to Android Studio. Right here, I'm going to add the URL as a string variable here. So, I'll say val and I'll call it path, okay? And within double quotes, I'll paste in the link right there, okay. Now, what I want to do next is load the URL. And since it's a web-based URL, I'm going to use a WebView. So, back to my layout, I'm simply going to drag in a WebView over here. So, let's see, WebView right there. Just put it right here, okay? And I'll constrain the side, top, and the right and I'll give it an id of webView, okay? Now back to my MainActivity file. Now that I have the webView, I can reference this directly. I'll say, binding., there is my webView. And then webView, I can use this method called loadUrl. And the URL I want to load is my path, right there. 

Here's the URL, and that's in my path variable. So, I'm passing in the path to my loadUrl method. All right, let's try it out, let's play. All right, looks like it worked. Let's pull up our emulator. Okay, YoutubeMiniDemo, there's our 'Play Video' button, let's click on it. We have an issue: Webpage not available. What happened? Well, you see, we're dealing with the Internet, right? So, whenever you have links like this and you want to load that from your app, you need to give your app Internet permission. And we haven't seen this before, so this is a good learning opportunity. What you want to do is go to your manifests, and AndroidManifest.xml file, pull that up. And over here, I'm going to give my app permission for Internet. Open angle bracket and then uses. You see uses permission. So uses-permission, and the name is Internet. so right here you see Internet, there we go. 

And then I to close this tag, so \ will should do it. There you go, okay? So, you're going to be dealing with permissions quite a lot when working with apps, but this is the way you add permission for your app. So, now that we've added it, this should work. So, I'll rerun. We have my emulator up over here. All right. Let's tap on the button, 'Play video' and there you go. You see the YouTube app pop up. Depending on the Internet, it's going to load and it's working. It just takes a little bit of time to load everything. It's a little slower in the emulator. If you were using this directly on your phone, it will be faster. There you go. It started playing. I'm going to pause this and there you go. The video has loaded and you see it's playing in the YouTube app within the emulator, okay? 

So, you can imagine that if you wanted to create videos for, let's say, 10-15 links like this and had an app where users post YouTube videos, they like, and share with each other, this could be a useful way of doing that. And those links could not just be for YouTube videos, it could be for websites as well, just general links. For example, if I wanted to change the path to something else, let's say I wanted this to go to my website. So, I'll send this to a different link, okay? If I did that and reran the app, as long as the Internet permissions are set, this should be working. All right? It worked. Let's pull up our emulator. Okay, YouTube Mini video. Obviously, we haven't changed any of this, but that's fine. 

I'll tap on the button, and they should pull up my website in a browser, okay? I haven't accessed Chrome before in this emulator, so let's see, Accept, No, thanks. There we go, mashrurhossain.com, pulling up in Chrome. All right, but again, as with the YouTube app previously, this is not loading in my app itself, right? What we want to do is load the YouTube video inside our app. And the app we've built here, simply launches the YouTube app and plays the video there. So, if you wanted to launch the video within our app itself, then that involves a few more steps and permissions, API keys, and all that good stuff. So, that's what we're going to handle in the next slightly more complicated project. And for building this app, you'll need to have a Google account. If you don't have one already, this is the perfect time to create one because we'll be using an API key from there, all right? So, I hope you're excited, and I'll see you in the next video.

 

About the Author

Mashrur is a full-time programming instructor specializing in programming fundamentals, web application development, machine learning and cyber security. He has been a technology professional for over a decade and has degrees in Computer Science and Economics. His niche is building comprehensive career focused technology courses for students entering new, complex, and challenging fields in today's technology space.

Covered Topics