Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In android ,how to set layout_marginRight in code

below is a textview layout , and I hope to modify layout_marginRight to zero in code.

<TextView
                android:id="@+id/status1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_marginRight="@dimen/keyguard_lockscreen_status_line_font_right_margin"
                android:singleLine="true"
                android:ellipsize="marquee"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="@dimen/keyguard_lockscreen_status_line_font_size"
                android:drawablePadding="4dip"
 />

when I copy aqif code to my code as below, phone halt at power on animation.

private void updateStatus1() {
        if (mStatus1View != null) {
            MutableInt icon = new MutableInt(0);
            CharSequence string = getPriorityTextMessage(icon);
            mStatus1View.setText(string);
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mStatus1View.getLayoutParams();
            params.rightMargin = 0;
            mStatus1View.setLayoutParams(params);

            mStatus1View.setCompoundDrawablesWithIntrinsicBounds(icon.value, 0, 0, 0);
            mStatus1View.setVisibility(mShowingStatus ? View.VISIBLE : View.INVISIBLE);
        }
    }
like image 405
yao Avatar asked Nov 29 '25 04:11

yao


1 Answers

you can do it this way.

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.rightMargin = 30;
view.setLayoutParams(params);

to set Values in dp you can do it like this.

params.rightMargin = (int) (30f * this.getResources().getDisplayMetrics().density);

and the type of params depends upon its parent, if your view parent is LinearLayout the your params must be of LinearLayout.LayoutParams type, and in case of RelativeLayout, your params must be of RelativeLayout.LayoutParams type.

regards, Aqif Hamid

like image 53
Aqif Hamid Avatar answered Dec 01 '25 16:12

Aqif Hamid



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!