React Native: VideoView android native module spilling out of the container - react-native

Video view spilling to the previous view
I have a scrollview and inside the scrollview I have bunch of videos. I have created a native video module for android using VideoView class and following are code to resize the video.
public class CustomVideoLayout extends RelativeLayout {
private ThemedReactContext mThemedReactContext;
private RelativeLayout screenLayout;
private CustomVideoView videoView;
private ImageView imageView;
private ImageButton playPauseButton;
private ImageButton muteButton;
private boolean mMuted;
private boolean mRepeat;
private boolean mEnableControls;
private MediaPlayer mediaPlayer;
public CustomVideoLayout(#NonNull ThemedReactContext context, Activity activity) {
super(context);
mThemedReactContext = context;
screenLayout = (RelativeLayout) activity.getLayoutInflater().inflate(R.layout.layOutName, null);
this.addView(screenLayout);
imageView = findViewById(R.id.posterImage);
videoView = findViewById(R.id.customVideoView);
videoView.setOnPreparedListener(preparedListener);
videoView.setOnTouchListener(videoViewOnTouchListener);
videoView.setOnCompletionListener(videoOnCompleteHandler);
playPauseButton = findViewById(R.id.pause_play_button);
playPauseButton.setOnClickListener(onClickListener);
muteButton = findViewById(R.id.volume_button);
muteButton.setOnClickListener(onClickListener);
}
// ......
}
If i'm not resizing the height, the video is not spilling up and also its changing it's original aspect ratio. But in order to maintain the original aspect ratio i must recalculate the height and width. And when the height is exceeding the container's height, its spilling a portion of the video on the previous VideoView.
Note: The top video is not spilling at all when put on top.
Following is the Layout file.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:id="#+id/video_wrapper">
<com.videoplayer.CustomVideoView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:id="#+id/customVideoView"/>
<ImageView
android:id="#+id/posterImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/bg_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageButton
android:id="#+id/pause_play_button"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/pause"
android:scaleType="fitXY"
android:tag="#drawable/pause"
android:visibility="invisible"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right">
<ImageButton
android:layout_width="20dp"
android:layout_height="20dp"
android:id="#+id/volume_button"
android:background="#drawable/volume"
android:scaleType="fitXY"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:tag="#drawable/volume"
android:visibility="invisible"/>
</RelativeLayout>
</FrameLayout>

Related

Custom Toast inflater must be initialized

i have this fun
private fun testToast(){
val inflater:LayoutInflater
val view = inflater.inflate(R.layout.custom_toast_message)
val toast = Toast(this.context)
toast.setGravity(Gravity.TOP,0,70)
toast.duration = Toast.LENGTH_LONG
toast.view =view
toast.show()
}
the main idea its to change the background color of the toast without getting a square toast
in this code i get an error in inflater.inflate
(inflater must be initialized)
this is the test xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_toast_container"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#color/background_blue"
android:padding="16dp"
android:weightSum="1"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_toasticon"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"/>
<TextView
android:id="#+id/TextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_gravity="center_vertical"
android:layout_weight="0.8"
android:textColor="#color/gauge_red"
android:fontFamily="#font/roboto"
android:text="Prueba prueba PRUEBA"
/>
</LinearLayout>
con someone help me or annother way to do a toast with custom color.
change the color of a toast with rounded corners
You declared the inflater variable but never assigned anything to it (using =). You can’t use a variable’s value if you haven’t assigned anything to it yet.
If this function is in an Activity, you can delete the line that declares val inflater and replace inflater. with layoutInflater. to use the property of the Activity. If it’s in a Fragment, use requireActivity().layoutInflator..
By the way, the latest version of Android has deprecated setting a custom view for your toast. On Android R and later if you set a view on your Toast, it won’t show. They want you to use SnackBars instead. There might be a third party library available that could help create something more similar to Toasts.
you can make a drawable resource to achieve it
toast_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/teal_200" />
<corners android:radius="48dp" />
</shape>
and use this drawable for background of toast view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_toast_container"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#drawable/toast_background"
android:orientation="horizontal"
android:padding="16dp"
android:weightSum="1">
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:src="#drawable/ic_launcher_background" />
<TextView
android:id="#+id/TextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.8"
android:text="Prueba prueba PRUEBA"
android:textColor="#color/purple_700"
android:textSize="14sp" />
</LinearLayout>
the result

RecyclerView inside NestedScrollView does not retain recycler scroll position if rc view height is not given in dp

<RelativeLayout 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:background="#color/black"
android:fillViewport="true"
tools:context=".Details_Act.About_Act">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:id="#+id/master_nest"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Some More Layouts>...</Some More Layouts>
<androidx.recyclerview.widget.RecyclerView
android:nestedScrollingEnabled="false"
android:id="#+id/eps_rcview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/relative"
android:layout_marginTop="20dp" />
</androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</RelativeLayout>
In above case when i backpress from another activity recycler position is reset
But if i give recyclerview a height of any 400dp,500dp,etc... instead of fill parent or match parent it work it retain recycler position
Is there anyone who can help me😩
I know nestedscrollview take only one child , i am not going to give full code cuz it has too many lines

CollapsingToolbarLayout not Collapsing with RecyclerView Scroll Behavior

I have a CollapsingToolbarLayout that shows an image (placed inside the ConstraintLayout to keep the ratio 1:1) and a RecyclerView that has the com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior attached to it.
Image of Layout
The image is in green and the RecyclerView is in blue.
The problem is when I scroll up on the RecyclerView, the CollapsingToolbarLayout doesn't collapse. Instead, the RecyclerView just scrolls underneath the entire layout. I have to manually scroll inside the CollapsingToolbarLayout for it to collapse.
Here is the code for the layout:
<androidx.coordinatorlayout.widget.CoordinatorLayout
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">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="#+id/toolbar">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:src="#drawable/ic_launcher_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent"
android:nestedScrollingEnabled="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
app:layout_anchor="#id/app_bar"
app:layout_anchorGravity="bottom|end"
app:srcCompat="#android:drawable/ic_dialog_email" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
I originally had the RecyclerView inside a NestedScrollView with the scrolling behavior attached to the NestedScrollView, but this was causing issues with the Adapter creating a ViewHolder object for EVERY single item in the RecyclerView and that was causing a lot of performance and lagging/freezing issues.
From my understanding, a RecyclerView should not be placed inside a NestedScrollView for this reason and that is why I took the NestedScrollView out.
Any help would be appreciated!
Simply you should disable nested scrolling in recycler view
android:nestedScrollingEnabled="false"
Also, if you have nested recycler views you should disable nested scrolling in child recycler views.
If you want the image to collapse when recyclerview scrolls, use this scroll flags
app:layout_scrollFlags="scroll|snap|enterAlwaysCollapsed"
and use
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
in your recyclerview , and android:nestedScrollingEnabled attribute is not required for what you want to achieve.
have you looked into https://www.journaldev.com/13927/android-collapsingtoolbarlayout-example
Also see some of the things you are missing

Android Sliding Panel with Scrollview

I'm trying to use a Sliding Panel from the link here and I'm facing the problem shown in the image:
My idea is to have custom content when the user swipes the bottom menu up, and have the possibility to scroll this content. But when the panel goes to anchored state, it seems like the content height is not set correctly, because some piece of the content doesn`t show up.
I downloaded the sample in the link above, and the problem already occurs. i made a minor change, adding the scrollview.
Am I missing something? any help would be much appreciated.
<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=".DemoActivity" >
<com.sothree.slidinguppanel.SlidingUpPanelLayout
xmlns:sothree="http://schemas.android.com/apk/res-auto"
android:id="#+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
sothree:collapsedHeight="68dp"
sothree:dragView="#+id/name"
sothree:shadowHeight="4dp" >
<TextView
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="Main Content"
android:textSize="16sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eee"
android:clickable="true"
android:focusable="false"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="68dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:textSize="14sp" />
<Button
android:id="#+id/follow"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical|right"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="14sp" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/slide_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/graphic" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="teste" />
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/graphic" />
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/graphic" />
</LinearLayout>
</ScrollView>
</LinearLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>
</RelativeLayout>
This problem occurs because the sliding view is fully measured and then placed partially off the screen. As you slide the view up, its size doesn't change, just its position. This means that the ScrollView isn't fully visible when anchored.
The solution I came up with is to use the PanelSlideListener events to resize the ScrollView as the position of the sliding view changes. Just give your ScrollView an ID of "#+id/scroll_view" and use this modified section of the onCreate() code below from the AndroidSlidingUpPanel demo app.
final float anchorPoint = 0.5f;
final ScrollView scrollView = (ScrollView) findViewById(R.id.scroll_view);
final SlidingUpPanelLayout layout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);
layout.setAnchorPoint(anchorPoint);
layout.setPanelSlideListener(new PanelSlideListener() {
#Override
public void onPanelSlide(View panel, float slideOffset) {
Log.i(TAG, "onPanelSlide, offset " + slideOffset);
setActionBarTranslation(layout.getCurrentParalaxOffset());
resizeScrollView(panel, slideOffset);
}
#Override
public void onPanelExpanded(View panel) {
Log.i(TAG, "onPanelExpanded");
resizeScrollView(panel, 0.0f);
}
#Override
public void onPanelCollapsed(View panel) {
Log.i(TAG, "onPanelCollapsed");
}
#Override
public void onPanelAnchored(View panel) {
Log.i(TAG, "onPanelAnchored");
resizeScrollView(panel, anchorPoint);
}
private void resizeScrollView(View panel, float slideOffset) {
// The scrollViewHeight calculation would need to change based on
// what views you have in your sliding panel. The calculation below
// works because your layout has 2 child views.
// 1) The row with the drag view which is layout.getPanelHeight() high.
// 2) The ScrollView.
final int scrollViewHeight =
(int) ((panel.getHeight() - layout.getPanelHeight()) * (1.0f - slideOffset));
scrollView.setLayoutParams(
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
scrollViewHeight));
}
});

android, how to place a scroll view in the middle of the screen:

I'm trying to create a a screen layout of the following though:
A line with some TextView in it
A scroll view where the user actions will add or remove View from
A buttons line.
I have no problem with 1. or 3. But 2. gives me troubles. My scheme is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainX"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/labels"
android:layout_weight="1">
<!--Some text view of various sizes -->
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/dataScroll">
<LinearLayout
android:id="#+id/dataShow"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/buttons"
android:layout_weight="1">
<!--some buttons-->
</LinearLayout>
</LinearLayout>
When I run the program, the buttons line is in the middle of the screen and When I add element to the screen (using the addView on the dataShow layout), the frist one is added without a problem but after that I can't tell what is happening.
Thanks
I'm not exactly sure what you are trying to do.
It seems like you want to dynamically add items to the dataShow layout. Instead of using a ScrollView containing a LinearLayout, you should be using a ListView (vertical scrolling) or Gallery (horizontal scrolling) for your dataShow layout. This will provide the scroll functionality and allow you to add items dynamically to the list/gallery.
Try using a RelativeLayout, then align the header at the top, and the footer at the bottom. Set the ScrollView paddingBottom.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="text"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/header"
android:layout_above="#+id/footer"
android:paddingBottom="100dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="center main content"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"/>
</ScrollView>
<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#color/colorPrimary"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"/>
</RelativeLayout>
</RelativeLayout>