Использование компонента WebView для полного экрана
Создал минималистичный браузер без всяких либо внедрений для улучшения качества. Решил я зайти на ютуб через мой браузер и наживая на кнопку "Во весь экран" ничего не происходит, хоть анимация нажатия кнопки была. То есть я не могу посмотреть видео со всех сайтов в которых присутствует кнопка для отображения в полный экран. Вот код экрана:
public class MainActivity extends AppCompatActivity {
ProgressBar progressBar;
EditText inputUrl;
WebView webView;
ImageButton sendButton, forwardButton, backButton, refreshButton;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
inputUrl = (EditText) findViewById(R.id.autoCompleteTextView);
webView = (WebView) findViewById(R.id.webView);
sendButton = (ImageButton) findViewById(R.id.sendButton);
forwardButton = (ImageButton) findViewById(R.id.forwardButton);
backButton = (ImageButton) findViewById(R.id.backButton);
refreshButton = (ImageButton) findViewById(R.id.refreshButton);
webView.setWebViewClient(new myWebClient());
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
progressBar.setProgress(newProgress);
if(newProgress==100)
progressBar.setVisibility(View.GONE);
else
progressBar.setVisibility(View.VISIBLE);
}
});
WebSettings webset = webView.getSettings();
webset.setJavaScriptEnabled(true);
webset.setJavaScriptCanOpenWindowsAutomatically(true);
webView.loadUrl("http://www.google.com");
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = inputUrl.getText().toString();
if(url.contains("http://")||url.contains("https://")){
return;
}
else if(url.contains(" ")||url.contains("")){
url = "https://www.google.com/search?q="+url;
}
else if(url.contains(".")&&!url.contains(" ")){
url = "http://"+url;
}
webView.loadUrl(url);
url = webView.getOriginalUrl();
inputUrl.setText(webView.getOriginalUrl());
inputUrl.setText(webView.getUrl());
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(webView.getWindowToken(), 0);
}
});
forwardButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (webView.canGoForward())
webView.goForward();
inputUrl.setCursorVisible(false);
inputUrl.clearComposingText();
inputUrl.clearFocus();
inputUrl.clearAnimation();
}
});
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (webView.canGoBack())
webView.goBack();
inputUrl.setCursorVisible(false);
inputUrl.clearComposingText();
inputUrl.clearFocus();
inputUrl.clearAnimation();
}
});
refreshButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
webView.reload();
inputUrl.setCursorVisible(false);
inputUrl.clearComposingText();
inputUrl.clearFocus();
inputUrl.clearAnimation();
}
});
inputUrl.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
inputUrl.selectAll();
return false;
}
});
if(inputUrl.isLongClickable()||inputUrl.isClickable()){
inputUrl.selectAll();
}
}
Вот код клиента:
public class myWebClient extends WebViewClient {
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Как мы видим JS разрешён и я в недоумении почему нельзя смотреть видео в полный экран
Ответы (1 шт):
Автор решения: Andrew
→ Ссылка
Решается такая проблема добавлением кастомного клиента:
private class MyChrome extends WebChromeClient {
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
protected FrameLayout mFullscreenContainer;
private int mOriginalOrientation;
private int mOriginalSystemUiVisibility;
MyChrome() {}
public Bitmap getDefaultVideoPoster()
{
if (mCustomView == null) {
return null;
}
return BitmapFactory.decodeResource(getApplicationContext().getResources(), 2130837573);
}
public void onHideCustomView()
{
((FrameLayout)getWindow().getDecorView()).removeView(this.mCustomView);
this.mCustomView = null;
getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
setRequestedOrientation(this.mOriginalOrientation);
this.mCustomViewCallback.onCustomViewHidden();
this.mCustomViewCallback = null;
}
public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback)
{
if (this.mCustomView != null)
{
onHideCustomView();
return;
}
this.mCustomView = paramView;
this.mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
this.mOriginalOrientation = getRequestedOrientation();
this.mCustomViewCallback = paramCustomViewCallback;
((FrameLayout)getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
getWindow().getDecorView().setSystemUiVisibility(3846 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}
}
вот тут более подробно описано в чем именно проблема и вот такая проблема упоминается здесь