I'm working on a small app using YouTube API & webview to load the channel's chat, now there is no errors everything loads fine I can see the video I can see the chat fine but when I try to interact with the chat it doesn't seems to work, I have tried many things but nothing seems to work and I was wondering if anybody could give me a hand solving this issue.
LiveTube.java
package com.live.tube;
import com.live.tube.R;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class LiveTube extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
private YouTubePlayer YPlayer;
private static final String YoutubeDeveloperKey = "AIzaSyBMwV1heA9q55LybRCK5dXXTJWADRbmDh4";
private static final int RECOVERY_DIALOG_REQUEST = 1;
//Live Channel//";
public static final String VIDEO_ID = "eTWrGNka-ik";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_tube);
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(YoutubeDeveloperKey, this);
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(
"There was an error initializing the YouTubePlayer",
errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
#Override
public void onInitializationSuccess(Provider provider,
YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
YPlayer = player;
//loadVideId
YPlayer.loadVideo(VIDEO_ID);
}
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.setWebViewClient(new WebViewClient());
myWebView.clearCache(true);
myWebView.clearHistory();
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.loadUrl("https://www.youtube.com/live_chat?v=h98Psy5NZPo");
}
}
activity_live_tube.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube_view"
android:layout_width="fill_parent"
android:layout_height="180dp" />
</FrameLayout>
Related
I have found it impossible to correctly set the autocompletion of fields in html forms in an android studio webview:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">
<RelativeLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ProgressBar
android:id="#+id/progressbar_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" />
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:usesCleartextTraffic="true" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
MainActivity.java:
package com.example.webview;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
private ProgressBar progressBar;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = findViewById(R.id.webview);
progressBar = findViewById(R.id.progressbar_id);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webSettings.setGeolocationEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setSaveFormData(true);
WebViewClient webViewClient = new WebViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar.setVisibility(View.VISIBLE);
myWebView.setVisibility(View.GONE);
}
#Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
myWebView.setVisibility(View.VISIBLE);
}
};
myWebView.setWebViewClient(webViewClient);
myWebView.setSoundEffectsEnabled(true);
myWebView.loadUrl("https://tcg-wallet.ga/home");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}
i have try using:
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
but nothing happens there are no suggestions, I expected something like:
I don't know what I need to add to enable those suggestions!
I'm trying to refresh a WebView after regaining internet connection, I have a try again button. If we click on it, the URL will be loaded again. But with the code I've written, this feature is not working.
This is my code:
MainActivity.java
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
public class MainActivity extends AppCompatActivity {
private WebView webView;
private ProgressBar progressBar;
private SwipeRefreshLayout swipeRefreshLayout;
private LinearLayout NoInternetLayout;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progress_horizontal);
swipeRefreshLayout = findViewById(R.id.SwipeRefresh);
NoInternetLayout=findViewById(R.id.NoInternetLayout);
webView = findViewById(R.id.Webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new myWebViewClient());
chekInternet();
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
webView.reload();
}
});
}
#Override
public void onBackPressed() {
if(webView.canGoBack()){
webView.goBack();
}
}
private void chekInternet(){
ConnectivityManager connectivityManager= (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isConnected()){
webView.loadUrl("https://www.google.com/");
webView.setVisibility(View.VISIBLE);
NoInternetLayout.setVisibility(View.INVISIBLE);
}
else if (mobile.isConnected()){
webView.loadUrl("https://www.google.com/");
webView.setVisibility(View.VISIBLE);
NoInternetLayout.setVisibility(View.INVISIBLE);
}
else {
webView.setVisibility(View.INVISIBLE);
NoInternetLayout.setVisibility(View.VISIBLE);
}
}
private class myWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
swipeRefreshLayout.setRefreshing(false);
super.onPageFinished(view, url);
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
chekInternet();
Button newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent;
intent = new Intent();
startActivity(intent);
chekInternet();
}
});
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/SwipeRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/Webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<androidx.core.widget.ContentLoadingProgressBar
android:id="#+id/progress_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<LinearLayout
android:id="#+id/NoInternetLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff"
android:gravity="center_horizontal"
android:orientation="vertical"
android:visibility="invisible">
<Button
android:id="#+id/new_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/refresh"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:src="#drawable/nointernet" />
</LinearLayout>
</RelativeLayout>
First you have to listen for internet connection, by referring algrid's answer
use registerDefaultNetworkCallback for API >= 24 (and registerNetworkCallback for API < 24)
ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
#Override
public void onAvailable(Network network) {
// network available
}
#Override
public void onLost(Network network) {
// network unavailable
}
};
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
connectivityManager.registerDefaultNetworkCallback(networkCallback);
} else {
NetworkRequest request = new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build();
connectivityManager.registerNetworkCallback(request, networkCallback);
}
In onAvailable() you can call reload/refresh using these 2 ways
webView.loadUrl(mWebView.getUrl().toString());
or
webView.reload()
No full screen button appears on webview while watching a video, what's the cause?
I will not go to these matters please help.
I am sorry for my English
MainActivity.java
package net.aniplus.aniplus;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("http://youtube.com");
final ProgressDialog progressDialog=ProgressDialog.show(this, "Mobil Video İzle", "Bağlantınız Kontrol Ediliyor", true);
progressDialog.show();
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Toast.makeText(MainActivity.this, "Bir hata oluştur!", Toast.LENGTH_SHORT);
progressDialog.dismiss();
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.aniplus.aniplus.MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView" />
</RelativeLayout>
I am trying for hours but I didn't get it to work :(
On android manifest change or add Activity's theme.
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
please i am very new in android development, i have my website which i load in webview and is working now i want to add a progress bar to show when a link is processing to render and hide the progress bar after page is loaded.
please i have see a lot of examples but i kept on getting error while trying to implement it, please can someone help me using me own webview for example.
package com.mypage.mypage;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class SourceProjectActivity extends AppCompatActivity {
private WebView projectLoaderView;
private ProgressBar progress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_source_project);
projectLoaderView = (WebView)findViewById(R.id.sourcejailview);
WebSettings webSettings = projectLoaderView.getSettings();
progress = (ProgressBar) findViewById(R.id.progressBar);
progress.setMax(100);
webSettings.setJavaScriptEnabled(true);
webSettings.setSaveFormData(true);
webSettings.setDatabaseEnabled(true);
webSettings.setDomStorageEnabled(true);
//local offline loader
webSettings.setCacheMode( WebSettings.LOAD_DEFAULT );
projectLoaderView.loadUrl("http://google.com");
projectLoaderView.setWebViewClient(new WebViewClient());
SourceProjectActivity.this.progress.setProgress(0);
}
private class MyWebViewClient extends WebViewClient {
#Override
public void onProgressChanged(WebView view, int newProgress) {
//super.onProgressChanged(view, newProgress);
}
}
public void setValue(int progress) {
this.progress.setProgress(progress);
}
#Override
public void onBackPressed(){
if(projectLoaderView.canGoBack()){
projectLoaderView.goBack();
}else {
super.onBackPressed();
}
}
}
Here is my xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app=""
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_source_project"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mypage.mypage.SourceProjectActivity">
<WebView
android:id="#+id/sourcejailview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" >
<ProgressBar
style="#android:style/Widget.Material.Light.ProgressBar.Small"
android:layout_width="match_parent"
android:layout_x="124dp"
android:layout_y="43dp"
android:id="#+id/progressBar"
android:layout_height="wrap_content" />
</WebView>
</RelativeLayout>
WebViewClient doesn't have onProgressChanged() method. Instead of that you can use WebChromeClient and set progressbar as below:
private class MyWebViewClient extends WebViewClient {
public void onProgressChanged(WebView view, int newProgress) {
progress.setProgress(newProgress);
//Maybe you want to hide it once it reaches 100
if (newProgress == 100){
progress.setVisibility(ProgressBar.GONE);
}
}
}
here i have a question. I have already make my blog app. But, when i run this app, suddenly it crashed. Here my source code
package blog.app;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity
{
private WebView myWebView;
private WebSettings myWebSettings;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView myWebView=(WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://teknorats.blogspot.com");
WebSettings myWebSettings=myWebView.getSettings();
myWebSettings.getJavaScriptEnabled();
myWebView.setWebViewClient(new WebVewClient());
}
private static class WebVewClient extends WebViewClient {
public WebVewClient() {
}
}
public class WebAppInterface{
Context mContext;
WebAppInterface(Context c) {
mContext=c;
}
private WebAppInterface() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.addJavascriptInterface(new WebAppInterface(),"Android");
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}
And this is the xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webview"
android:src="#drawable/tr_launcher"
>
</LinearLayout>
Please, i need your correction with that code.
If you ask where i got this code, i got this code from here http://developer.android.com/guide/webapps/webview.html
It seems that in your xml file LinearLayout tag has id webview and you are trying to get
cast LinearLayout to WebView.
So change
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webview"
android:src="#drawable/tr_launcher"
>
</LinearLayout>
to
<Webview xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webview">
</Webview>
and you has already defined myWebView as class variable so change
WebView myWebView=(WebView) findViewById(R.id.webview);
to
myWebView=(WebView) findViewById(R.id.webview);