Pratham Vaidya
Pratham Vaidya

Reputation: 105

How to set margins of a Toolbar programmatically using Kotlin

I want the status bar filled with gradient background of my Action Bar. So I searched on some articles and managed to do so. But I know that colored status bar requires API level 21 so I added conditions to check API. Inside my codes, i have a Toolbar which has a top margin set to 24dp which is required when the API level is above 21 ,so i needed to change the top margin if the API is below 21. But I don't know how to set Margin of a Toolbar.

I tried to add LayoutParams() but that was not working for the Toolbar.

I have a separate layout (action_bar.xml) for Action bar which i am including in the Main Activity

<?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout
            android:id="@+id/appbarlayout"
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:design="http://schemas.android.com/apk/res-auto"
            android:background="@drawable/gradient_theme"
            android:layout_width="match_parent"
            android:fitsSystemWindows="true"
            android:layout_height="80dp">

        <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_marginTop="24dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                design:title="@string/app_name"
                design:titleTextColor="@android:color/white"/>

    </RelativeLayout> 

Also i am calling this function in my MainActivity.kotlin after setting content view :

fun beautifyLayout(activity:Activity,window:Window) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
        activity.findViewById<RelativeLayout>(R.id.bottom_nav).also {
            it.setPadding(0, 0, 0, getSoftButtonsBarSizePort(activity))

        }
    }
else{
//I want to remove the margin top if API level is less than 21

      val params = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.WRAP_CONTENT
        )
     params.topMargin = 0  
        val bar = activity.findViewById<Toolbar>(R.id.toolbar)
        bar.layoutParams = params
}
     }

This is my Main Activity :

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout
        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=".MainActivity"
        android:id="@+id/mainContainer">

    <include layout="@layout/action_bar"/>
    <include layout="@layout/bottom_nav"/>

<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"
               android:paddingLeft="20dp" android:paddingRight="20dp" android:id="@+id/contentBox">
    <Button android:layout_width="match_parent" android:layout_height="wrap_content"
            android:text="@string/login"
            android:paddingTop="20dp"
            android:paddingBottom="20dp"
            android:id="@+id/testButton"
            android:layout_marginTop="100dp"
    />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

In short, How can i set margin of a Toolbar in Kotlin?

Upvotes: 0

Views: 3601

Answers (2)

You can create an alternative layout for API level >= 21 by creating directory res/layout-v21 in which you create a file with name action_bar.xml (same as your original layout) with the top margin and original layout will be without margin

res/layout-v21/action_bar.xml

<RelativeLayout
        android:id="@+id/appbarlayout"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:design="http://schemas.android.com/apk/res-auto"
        android:background="@drawable/gradient_theme"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true"
        android:layout_height="80dp">

    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_marginTop="24dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            design:title="@string/app_name"
            design:titleTextColor="@android:color/white"/>

</RelativeLayout>

res/layout/action_bar.xml

<RelativeLayout
        android:id="@+id/appbarlayout"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:design="http://schemas.android.com/apk/res-auto"
        android:background="@drawable/gradient_theme"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true"
        android:layout_height="80dp">

    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            design:title="@string/app_name"
            design:titleTextColor="@android:color/white"/>

</RelativeLayout> 

Upvotes: 0

Radesh
Radesh

Reputation: 13585

You must use ViewGroup.MarginLayoutParams instead of FrameLayout.LayoutParams

Use this code

val bar = activity.findViewById<Toolbar>(R.id.toolbar)
val params = bar.layoutParams as ViewGroup.MarginLayoutParams
params.topMargin = 0
bar.layoutParams = params

Upvotes: 3

Related Questions