Lists & Views
The course is part of this learning path
In this course, we look at the various different ways that information can be presented to users within an Android app. We'll cover a variety of views that you can incorporate into your apps.
This course is intended for anyone who wants to learn how to start building their own apps on Android.
This is a beginner-level course so doesn't require any specific knowledge, but some basic knowledge of the fundamentals of Android would be beneficial.
Alright. Hello everyone. We're going to talk about WebView. Alright. So, WebView, what is it? Well, it's going to be used to show Internet sites in Android applications. So, we're going to use a WebView to show an Internet site right in your app. No jumping to a browser or whatever else. You can use WebView very easily, and in fact, let's just go right to Android Studio and have some practice. So, you can see, I've got a project and I will add a WebView to my project. Where is it? Under the Widgets section. All I do is drag it and drop it under the ConstraintLayout. Layout with an height of WebView can stay as match_parent. I will give the constraint values of the WebView and then lastly just needed to find it's ID, so I'll write web here.
So, now I'll define this WebView in the MainActivity.kt file. Now, I'm going to write in here lateinit var webView: WebView. You see that? So, inside the onCreate method webView = findViewById(R.id.web). So, after that, I'm going to give this URL to this WebView to load the Internet site. And I'll just write in here webView.loadUrl inside braces I'll write the URL address and I just want to load Google in this project. Now, if I leave it like this, it's going to open this site in an external browser, you understand? So, because of this, I'll need to add some more code. webView.webViewClient = WebViewClient(). So, after I write in that code, it's going to open this Internet site inside of my application. You don't believe me? Let's run it and see.
So, the application opens, website doesn't get loaded. Why? Because if an application wants to connect to the Internet from the device, it needs to get permission. So, obviously you cannot connect to the Internet site using people's devices without permission. Can you? Now, I have explained this before in the manifest file video, might have been a little while that you have seen it. So, I'll refresh your memory. Where do I get this permission? I get it from the manifest file. See how I gave you a big hint. So, I'll just open up the manifest file. Then after opening up the tag, I'll just write in here uses-permission and select the uses-permission option that gets offered to us by the code editor. Very convenient.
Alright. So, after that it shows me permissions. One of the permissions here is Internet access. I'm going to get that Internet connection permission from the user. But the second is the WiFi state. So, in other words, I'll need to get the user's permission to use its WiFi connection as well. And then that way users will see these permissions if they need to download something or whenever they download the app, they're going to say, "Hey, this is going to ask for this, this, and this. Do you agree?" Yes, of course I do.
So, let's just run again. I'll show you that it is not working again. Alright. So why isn't it working? Well, this is why you came here today because I want to show you this. So, after the Android 9 operating system, this is the problem that you're or the situation that you're going to face. I'm really not going to give you any more details about this. You just got to know it after nine. So, if you see this error, you really do need to add this code line under the application section in your manifest file. So, here I write traffic, I select the 'usesCleartextTraffic' option. This property should be true. After that, I'll be able to run my application lickety-split. No problems at all. Okay, so you see it's working. So, I can connect a Google on my application and I could go to any Internet site from here.
Alright. In fact, let me just search for something here. Now, if I were to click the 'back' button, it's not going to go back. It just closes the application. So, how would I solve that problem? Yes, I'll do it in the main activity file. There's actually method and its name is the onBackPress() method. What do you think? So, this method actually controls the back button on whichever Android device your user is on. So, I just write in this method if(webView.canGoBack()) webView.goBack(), else apply back press method. So, let's run it like that. So Google opens up and I'll write android to search. You see how it gets searched. Then, when I press the 'back' button, it goes back to the previous site until the previous site is, well, finished. So, after you finished going back, back, back throughout the history, it will close the application. Alright. So, that's how it works. Of course, that's going to be useful for you maybe in a little while later when you're developing your own applications, but you do need to use permissions. Alright, I'll see you in the next video.
Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.