dimanche 24 mai 2020

Get random Youtube video in webview by clicking button in Android studio

I want to make an app where the user can get random youtube videos in a webview by clicking a button. I already can show a video in my webview with the url but I want to get any random video from youtube by clicking my button.

In my xml file I have a webview and a button

´´´

MainActivity.java

´´´ public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String path = "https://www.youtube.com/watch?v=1uwvxTT5V5M";
    String dataUrl =
           "<html><body>Video From YouTube<br><iframe width=\"420\" height=\"315\" src=\"https://www.youtube.com/embed/47yJ2XCRLZs\" frameborder=\"0\" allowfullscreen></iframe></body></html>";


    WebView showYoutubeVideo = (WebView) findViewById(R.id.zufallvideo);
    showYoutubeVideo.setWebViewClient(new WebViewClient(){

        public boolean shouldoverloadUrlLoading(WebView view, String url) {
            return false;
        }
    });

    showYoutubeVideo.getSettings().setJavaScriptEnabled(true);
    showYoutubeVideo.loadData(dataUrl,"text/html", "utf-8");

}

}




Aucun commentaire:

Enregistrer un commentaire