Search code examples
androidmargin

Changing margin programmatically using sdp


I am using compile 'com.intuit.sdp:sdp-android:1.0.3' api for setting margins of linear layout like this

<LinearLayout
        android:id="@+id/layout_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/_10sdp"
        android:layout_marginTop="@dimen/_10sdp"
        android:padding="@dimen/_8sdp"
        android:orientation="vertical">
</LinearLayout>

Now when I change the margin programatically, it changes it in dp not in sdp

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

params.setMargins(0, 10, 10, 0);
layout_message.setLayoutParams(params);

So how to change the margin programatically according to sdp? Thank you


Solution

  • You can try

    For Java

    int margin = getResources().getDimensionPixelSize(R.dimen._10sdp);
    params.setMargins(0, margin, margin, 0);
    

    For Kotlin as pointed out by Zohab Ali

    resources.getDimensionPixelSize(R.dimen._10sdp)