javaandroidkotlinlayoutparamslayoutmargins

How to set margin to main ConstraintView in android?


I want to set margin to my main layout with programmatically. I use layout params as following code.

val parMain = mainLayout?.layoutParams as ConstraintLayout.LayoutParams

But i have some error like

android.view.ViewGroup$LayoutParams cannot be cast to androidx.constraintlayout.widget.ConstraintLayout$LayoutParams

if I change the code as Android wants like this:

val parMain = mainLayout?.layoutParams as ViewGroup.LayoutParams

There is no set margin function in my layoutParams.

Thats my xml code.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/detail_feed_scrool_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/transparent"
    tools:context=".DetailActivity">

    <View
        android:id="@+id/shadow_view"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:background="#99000000"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/detail_feed_view_pager_activity"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    </androidx.viewpager.widget.ViewPager>

    <FrameLayout
        android:id="@+id/detail_fragment_frame_layout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/white"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

How can i set margin in my case?


Solution

  • A Layout/View's LayoutParams depends on it's parent layout

    For example:-

    If you will try to get layout param from your <View> who have id android:id="@+id/shadow_view" then the layoutParams will be ConstraintLayout.LayoutParams type.

    Why? because ConstraintLayout is the parent layout of <View> . now same goes for the <ViewPager>, it's direct parent is also ConstraintLayout, so same ConstraintLayout.LayoutParams

    and also when you want to set layoutParams on any View/Layout, Always look for the direct parent layout and set same as that parent layout type layoutParams

    Now Answer to Your Problem

    In your case, you don't have any parent layout that's why you are getting the ViewGroup.LayoutParams.

    ViewGroup is parent class of layouts, more like a generic class and this class don't have margins.

    easiest solution for you will be just wrap your whole layout in another layout that you want, so there will be a parent for your ConstraintLayout and your LayoutParams will be the type of layout that you used to wrap

    something like this

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/detail_feed_scrool_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary">
    
            <View
                android:id="@+id/shadow_view"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:background="#99000000"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
            <androidx.viewpager.widget.ViewPager
                android:id="@+id/detail_feed_view_pager_activity"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">
    
            </androidx.viewpager.widget.ViewPager>
    
            <FrameLayout
                android:id="@+id/detail_fragment_frame_layout"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="@android:color/white"
                android:visibility="gone"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">
    
            </FrameLayout>
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    </LinearLayout>
    

    Now when you will try to get the LayoutParams you will get them as LinearLayout type