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");
}
}
TL;DR: it's complicated.
Until Youtube exposes an endpoint in their API to do this, you're gonna need to find a workaround.
Some would suggest to query a search with "IMG" followed by a random integer (ex. "IMG 22930"). The end result is quite surprising, since tons of videos are uploaded without renaming.
Here's an example of this workaround implementation in python: https://github.com/0x01h/random-youtube-video-generator
Related
I would like to show some svg logos in my android mobile app. Some weeks ago google released AI www.autodraw.com (Web based tool for drawing)
I loaded the url in a webview, and everything works fine.
I would like to download the svg files (small pictures on top, the results of your drawings) directly to my app from the webview, when the user clicks one the choosed one. (after it, I would like to convert it to VectorDrawable and set it as a background of ImageViews to use it flexible with all pixel densities).
Google allows you to download your drawings, but it returns a png file and not always centered (users will not draw correctly always). That's why I would like to download the svg directly.
As an example of a result svg file from storage.googleapis:
https://storage.googleapis.com/artlab-public.appspot.com/stencils/selman/house-01.svg
This is what I have now:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_draw);
myWebView = (WebView) findViewById(R.id.autodraw_webview);
imgView = (ImageView) findViewById(R.id.imageView);
myWebView.loadUrl("http://www.autodraw.com");
myWebView.setWebViewClient(new WebViewClient() {
#Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
return super.shouldInterceptRequest(view, request);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
Question:
Is it possible to get the svg file from the WebView to the app by user clicking one of the result images of his/her drawing ?
I'm trying to embed a Youtube video in a WebView, and I want to support Android version 2.
Since Google owns both Android and Youtube, I thought it would be easy, but apparently it's not. I've seen similar questions but they didn't help.
On Android 2.1 when I tap the play button, I have the message "An error occurred, please try again later."
On Android 2.3 I have player buttons but the video does not start.
Here is my code:
public class YoutubeActivity extends Activity {
private WebView webView;
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
webView = new WebView (this);
setContentView (webView);
webView.getSettings ().setJavaScriptEnabled (true);
webView.setWebChromeClient (new WebChromeClient ());
webView.setWebViewClient (new WebViewClient () {
public boolean shouldOverrideUrlLoading (WebView view, String url) {
view.loadUrl (url);
return true;
}
});
if (Build.VERSION.SDK_INT < 8) {
webView.getSettings ().setPluginsEnabled (true);
}
else {
webView.getSettings ().setPluginState (PluginState.ON);
}
webView.loadUrl ("http://youtube.com/embed/NAg4qXsk99c?rel=0");
}
protected void onPause () {
super.onPause ();
webView.loadUrl ("about:blank");
}
}
I also added android:hardwareAccelerated="true" in AndroidManifest.xml.
The Youtube Android Player API is not an option for me because I also want to support platforms without the Youtube app (like Amazon, Blackberry...).
I also tried with RTSP links but the quality is too bad.
Thanks for your help.
I'm trying to call JS functions from Android native code, but they don't seem to be working. I've tried many solutions but to no solution. Here is my code:
public class MainActivity extends Activity {
private WebView webView;
#SuppressLint("SetJavaScriptEnabled")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView1);
webView.setWebViewClient(new MyCustomWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.loadUrl("http://google.com");
}
private class MyCustomWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
webView.loadUrl("javascript:alert('shadow');");
Toast.makeText(getApplicationContext(), "DONE", Toast.LENGTH_LONG).show();
}
}
}
Please help.
Thanks
You can't do alert() with Android's WebView. If you want to show alert dialogs, you need to handle it in your Activity code or use a WebChromeClient. Per the docs:
Creating and setting a WebChromeClient subclass. This class is called
when something that might impact a browser UI happens, for instance,
progress updates and JavaScript alerts are sent here (see Debugging
Tasks).
See: https://developer.android.com/reference/android/webkit/WebView.html
With a WebView you can still write a JavaScript function that sets the value of a textbox to some string. If you need specific code to help you with this, let me know. If you've gotten this far though, you probably can handle it on your own is my guess.
Edit 1
First create a method in JavaScript called sendValueToAndroid()
myWebView.loadUrl("javascript:sendValueToAndroid()");
In the JavaScript method, call an exposed method in your Android code.
function sendValueToAndroid()
{
val divValue = ...
Android.sendValueToAndroid(divValue);
}
Expose a method in the Android app in any object of your choosing.
#JavascriptInterface
public String sendValueToAndroid(String val)
{
//do something in your app
}
Basically, what you're doing is telling the WebView to invoke a JavaScript method which invokes a callback method in your own app.
What are the settings to enable or disable in WebView to do this?
If you want to use a webview with exactly the same features as the native android browser, you have to check MANY options and settings. A WebView is made to NOT use all of the browsers options and settings for better performance and for having more controll on what the users can and can not do while browsing. to figure out all the opportunities you should read through the api documentation here:
http://developer.android.com/reference/android/webkit/WebView.html
For some further dealing and handling with the WebView class also here are some usefull sample codelines:
http://pankajchunchun.wordpress.com/2013/01/09/example-of-webview-with-result-handler-and-connection-timeout-handler/
I hope this will help you.
this is the webview example source..
what kind of setting do you wanna know??
public class MainActivity extends Activity {
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView)findViewById(R.id.webview);
webview.setWebViewClient(new WebClient());
WebSettings set = webview.getSettings();
set.setJavaScriptEnabled(true);
set.setBuiltInZoomControls(true);
webview.loadUrl("http://www.google.com");
findViewById(R.id.btnStart).setOnClickListener(onclick);
}
OnClickListener onclick =new OnClickListener() {
#Override
public void onClick(View v) {
String url= null;
EditText add = (EditText)findViewById(R.id.add);
url = add.getText().toString();
webview.loadUrl(url);
}
};
class WebClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
In addition to #Ssam's answer, you might want to manage the back button press so your app/Activity doesn't get closed on back press.
#Override
public void onBackPressed() {
if (webview.canGoBack()) {
webview.goBack();
}else
super.onBackPressed();
}
where webview is your webview
I found that this question has been asked before How to resolve overlapping the content of the webview here, but it was closed for some reason. But here is what happens. I have created a webview, applied transparent background, but when i try to load something, and reload something again, the older content will still remain on the webview and the newer one will overlap it. Here is a picture that explains it.
So i am creating a jokes application, and i have added webview. Then i added transparency and the random joke button reloads the webpage so that the webview can get another joke. But it just overlapps instead. Any solutions? Here is my piece of code
Button random = (Button) findViewById(R.id.reload);
final WebView web = (WebView) findViewById(R.id.webView);
web.setBackgroundColor(Color.TRANSPARENT);
web.setWebViewClient(new WebViewClient());
String selected = (String) getIntent().getCharSequenceExtra("selected");
final String url = "http://someurl.com/?s=dirty&x=" + selected;
web.loadUrl(url);
random.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
web.loadUrl(url);
}
});
Any help would be really appreciated :)
Try before doing web.loadUrl(url); doing web.clearView();
See docs.