Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing margin programmatically using sdp

Tags:

android

margin

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

like image 989
shehzy Avatar asked Nov 04 '16 07:11

shehzy


1 Answers

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)
like image 81
Jimit Patel Avatar answered Sep 19 '22 08:09

Jimit Patel