Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot set Linearlayout params inside a Framelayout


I have a Linear layout inside a Framelayout. What I would like to do is to add some margins programatically to the Linear layout based on the screen size.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:scrollbars="none">
    <FrameLayout 
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.aba.webcampstest.MainActivity"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/headerImageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:layout_marginTop="0dp"
            android:scaleType="centerCrop"/>

        <LinearLayout
            android:id="@+id/mainContainerLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

         //oher stuffs
       </LinearLayout>
    </FrameLayout >
</ScrollView>

And my activity onCreate method contains

LinearLayout containerLayout = (LinearLayout) findViewById(R.id.mainContainerLayout);
int marginTopBottom = dpToPx((int)((dpHeight*8)/100),(int)density); //8%
int marginLeftRight = dpToPx((int)((dpWidth*6)/100),(int)density); //6%

LinearLayout.LayoutParams languageContainerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
languageContainerLayoutParams.setMargins(marginLeftRight,marginTopBottom,marginLeftRight,marginTopBottom);
containerLayout.setLayoutParams(languageContainerLayoutParams);

I get the error when starting the Activity

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.aba.webcampstest, PID: 11723 java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams at android.widget.FrameLayout.onMeasure(FrameLayout.java:311) at android.view.View.measure(View.java:17495) at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1779) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.widget.ScrollView.onMeasure(ScrollView.java:476) at android.view.View.measure(View.java:17495) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:17495) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410) at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) at android.view.View.measure(View.java:17495) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2548) at android.view.View.measure(View.java:17495) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2285) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1396) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1595) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6637) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:814) at android.view.Choreographer.doCallbacks(Choreographer.java:614) at android.view.Choreographer.doFrame(Choreographer.java:584) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:800) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5602) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) at dalvik.system.NativeStart.main(Native Method)

What could be the problem?

like image 912
Coder Avatar asked Apr 22 '26 16:04

Coder


2 Answers

setLayoutParams description :

Set the layout parameters associated with this view. These supply parameters to the parent of this view specifying how it should be arranged. There are many subclasses of ViewGroup.LayoutParams, and these correspond to the different subclasses of ViewGroup that are responsible for arranging their children.

The LayoutParams type should be relative to the container of your LinearLayout, in this case FrameLayout.LayoutParams :

FrameLayout.LayoutParams languageContainerLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
languageContainerLayoutParams.setMargins(marginLeftRight,marginTopBottom,marginLeftRight,marginTopBottom);
containerLayout.setLayoutParams(languageContainerLayoutParams);
like image 71
Nika Kurdadze Avatar answered Apr 25 '26 05:04

Nika Kurdadze


Read the exception more carefully, especially this part:

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams

To clarify, when setting the LayoutParams of a view, you are declaring how your view is positioned inside its parent element.

So, for this example you provided use FrameLayout.LayoutParams.

Update base on first comment:

Let me show you an example:

LinearLayout containerLayout = (LinearLayout) 

findViewById(R.id.mainContainerLayout);
int marginTopBottom = dpToPx((int)((dpHeight*8)/100),(int)density); //8%
int marginLeftRight = dpToPx((int)((dpWidth*6)/100),(int)density); //6%

FrameLayout.LayoutParams languageContainerLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
languageContainerLayoutParams.setMargins(marginLeftRight,marginTopBottom,marginLeftRight,marginTopBottom);
containerLayout.setLayoutParams(languageContainerLayoutParams);

You are setting params for LinearLayout, but that layout is inside FrameLayout. So the LayoutParams of LinearLayout are of type FrameLayout.LayoutParams.

Which type of LayoutParams you should use is based on parents type in which your view is.

like image 25
Goran Avatar answered Apr 25 '26 05:04

Goran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!