I'm trying and failing at properly showing fullscreen video.
My WebChromeClient implements onHideCustomView and onShowCustomView. My layout has separate layouts for showing video and displaying webView. The problem is that sometimes it works correctly, and other times it does not. It happens only with a single website. I've noticed, that video goes fullscreen correctly on JW player 7.2.2, while it fails often on JW player 6.12.4945.
Here's how it looks when clicking fullscreen button or double clicking the video. Notice that there is website contents above the video, and there is empty view at the bottom of screen: https://drive.google.com/file/d/0B942jMuE3MylMVZQNVp6X0Y2ZGc/view?usp=sharing
webChromeClient = new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
progressBar.setProgress(newProgress);
}
#Override
public void onHideCustomView() {
fullscreen = false;
showUI();
mVideoView.removeAllViews();
}
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
fullscreen = true; //for onBackPressed
hideUI();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mVideoView.addView(view, params);
}
};
<RelativeLayout
android:id="#+id/root"
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"
android:fitsSystemWindows="true">
<RelativeLayout
android:id="#+id/contentView"
android:layout_width="match_parent"
android:layout_height="match_parent"></RelativeLayout>
<RelativeLayout
android:id="#+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Related
Currently working on application with integrating youtube player in it, all things working fine, the only problem I'm facing is that I'm not able to get onClick call back on youtubePlayerView. I'm trying to toast youtube video title when user click on youtube player. It's working fine when no video is loaded in youtube player but as soon as I initialised the youtubePlayerView and loadVideo clickListener start not working. How to handle clickListener in youtube player?
This is my xml code :
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:onClick="onClick"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/player">
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtubePlayer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="onClick"
android:background="#color/black"/>
</RelativeLayout>
<include layout="#layout/video_player_bottom_sheet"/>
</android.support.design.widget.CoordinatorLayout>
My code for playing video :
#Override
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return mYouTubePlayerView;
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
mYoutubePlayer = youTubePlayer;
mYoutubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
if (!wasRestored) {
mYoutubePlayer.loadVideo("0g0sy2x_vX4");
}
}
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),videoTitle,Toast.LENGTH_SHORT).show();
}
Remove the #Override from your method that you have defined for your method onClick() and add this to your player component in xml
android:clickable="true"
I have a WebView Fragment in my application, I want to check, the user scrolled the WebView down or not ,for example if the user scrolled down my WebView , I want show a Toast message that "you scrolled down" I'm trying this for the past 3 days, I didn't get any proper solution. I have seen many tutorials and discussions, none of them are working for me. Is there any one who can help ?
This is my fragment java code
public class tab1 extends Fragment {
public ProgressBar bar;
public FrameLayout frameLayout;
public tab1()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tab1, null);
final SwipeRefreshLayout swipe =(SwipeRefreshLayout)rootView.findViewById(R.id.swiperefresh1);
frameLayout=(FrameLayout) rootView.findViewById(R.id.frame1);
bar=(ProgressBar)rootView.findViewById(R.id.progressBar1);
final WebView view=(WebView) rootView.findViewById(R.id.webview);
bar.setMax(100);
view.loadUrl("http://facebook.com");
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyWebViewClient());
view.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(WebView view1,int progress){
frameLayout.setVisibility(View.VISIBLE);
bar.setProgress(progress);
if (progress==100){
frameLayout.setVisibility(View.GONE);
swipe.setRefreshing(false);
}
super.onProgressChanged(view1,progress);
}
});
view.getSettings().setBuiltInZoomControls(true);
view.getSettings().setDisplayZoomControls(false);
bar.setProgress(0);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
swipe.setColorSchemeResources(
R.color.pink, R.color.indigo, R.color.lime);
String webUrl = view.getUrl();
view.loadUrl(webUrl);
}
});
return rootView;
}
}
This is my xml file
<LinearLayout 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"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.hackerinside.jaisonjoseph.polysocial.tab1">
<FrameLayout
android:id="#+id/frame1"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#android:color/transparent">
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="3dp"
android:background="#android:color/transparent"
android:foregroundGravity="top"
android:progressDrawable="#drawable/custom_progress"
android:progress="20"/>
</FrameLayout>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swiperefresh1"
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" >
</WebView>
</android.support.v4.widget.SwipeRefreshLayout>
Error message after adding answer
Cast from null to OnScrollChangeListener requires API level 23 (current min is 15) less... (Ctrl+F1)
This check scans through all the Android API calls in the application and warns about any calls that are not available on all versions targeted by this application (according to its minimum SDK attribute in the manifest). If you really want to use this API and don't need to support older devices just set the minSdkVersion in your build.gradle or AndroidManifest.xml files. If your code is deliberately accessing newer APIs, and you have ensured (e.g. with conditional execution) that this code will only ever be called on a supported platform, then you can annotate your class or method with the #TargetApi annotation specifying the local minimum SDK to apply, such as #TargetApi(11), such that this check considers 11 rather than your manifest file's minimum SDK as the required API level. If you are deliberately setting android: attributes in style definitions, make sure you place this in a values-vNN folder in order to avoid running into runtime conflicts on certain devices where manufacturers have added custom attributes whose ids conflict with the new ones on later platforms. Similarly, you can use tools:targetApi="11" in an XML file to indicate that the element will only be inflated in an adequate context
ok try this
view.setOnScrollChangeListener(new View.OnScrollChangeListener() {
#Override
public void onScrollChange(View v, int scrollX, int scrollY, int
oldScrollX, int oldScrollY) {
}
});
here view is your WebView object. :) its native method so it will work fine.
I have a WebView inside a FrameLayout, to add tabs because i am creating a browser. The FrameLayout is inside a SwipeRefreshLayout.
The problem: Whenever i scroll the content up fast in the WebView, the refresh icon appears from behind the toolbar. It should only happen when the WebView content is at the top. It has something to do with the FrameLayout, when i remove it, the issue is gone.
The layout looks like this:
<SwipeRefreshLayout
android:id="#+id/swipeRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/webViewFrame"
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" />
</FrameLayout>
</SwipeRefreshLayout>
Just implement your Fragment or Activity with ViewTreeObserver.OnScrollChangedListener then set Listener like webview.getViewTreeObserver().addOnScrollChangedListener(this);
In onScrollChanged() method do like this
#Override
public void onScrollChanged() {
if (webview.getScrollY() == 0) {
swipeLayout.setEnabled(true);
} else {
swipeLayout.setEnabled(false);
}
}
No Solution worked for me. Here is my approach.
#Override
public boolean dispatchTouchEvent(MotionEvent event)
{
super.dispatchTouchEvent(event);
int x = (int)event.getX();
int y = (int)event.getY();
if(y>800){
swipeRefreshLayout.setEnabled(false);
}else {
swipeRefreshLayout.setEnabled(true);
}
return false;
}
I have an activity which should display a VideoView in landscape, but the activity itself must not be in landscape mode, so this is what i have.
<activity
android:name=".gui.VideoPlayer"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait" >
</activity>
<?xml version="1.0" encoding="utf-8"?>
Activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<VideoView
android:id="#+id/myvideoview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" />
</LinearLayout>
(The activity itself must remain in portrait mode because the device will be put in a case which covers the navigation and statusbars, but in landscape mode it wont cover them any more)
I know this is not the optimal answer but I had the same issue a while ago and found a pretty convenient workaround that might help you too.
Try to use a TextureView instead of VideoView because when you rotate it, the content inside rotates as well.
<TextureView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/my_texture_view"
android:rotation="90"
android:scaleX="1.8"
/>
Next you'll have to create a SurfaceTextureListener and set it to your view like below:
TextureView.SurfaceTextureListener mTextureListener = new TextureView.SurfaceTextureListener() {
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
Surface s = new Surface(surfaceTexture);
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(currentPageVideoUrl);
mMediaPlayer.setSurface(s);
mMediaPlayer.prepare();
}catch (Exception e){e.printStackTrace();}
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return false;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
};
And now just set the listener to your TextureView:
mTextureView.setSurfaceTextureListener(mTextureListener);
All that's left to do is to call this to start the video:
mMediaPlayer.start();
BONUS:If you want to make more complex adjustments like adjusting the video aspect ratio You can check this library for more details.
I get this error when using the following code, what is wrong?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout1">
</LinearLayout>
linearLayout1=(LinearLayout)findViewById(R.id.linearLayout1);
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
webView=new WebView(this.getApplicationContext()); // also with just (this)
webView.setLayoutParams(params);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
Log.d("finished",url);
}
}
);
linearLayout1.addView(webView);
//also tried linearLayout1.forceLayout(); linearLayout1.measure(View.MeasureSpec.UNSPECIFIED,View.MeasureSpec.UNSPECIFIED);