Home » Android » android – how to set EditText TopMargins in dp programatically?

android – how to set EditText TopMargins in dp programatically?

Posted by: admin June 15, 2020 Leave a comment

Questions:

In my android application I want to change the topMargin of an editText.

The thing is that I want to change it “dp” wise and not pixel wise.

I want to change only the topMaring. Leave the other as is. not set them to zeros ?

programmatically i can set margins in int only?

How to&Answers:

set like follw code:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, convertPixelsToDp(5,this), 0, 0);
editText.setLayoutParams(lp);

and convert Pixels To Dp

public static int convertPixelsToDp(float px, Context context){
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    int dp = px / (metrics.densityDpi / 160f);
    return dp;
}

use follow code for changing just one.

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    params.topMargin = convertPixelsToDp(5,this);
    editText.setLayoutParams(params);

Answer:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
editText.setLayoutParams(lp);

Answer:

you can not directly set margins using dp programmatically, because .setMargins method ask pixels not for dp so if you want to give dp’s instead of pixels, you should convert dps to pixels.
first create LayoutParams instance:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

then set convert dp to pixels and set margins of the layout like this:

lp.setMargins(left, dpToPx(30), right, bottom);
youredittext.setLayoutParams(lp); 

this is the conversion method from pixels to dp and dp to pixels:

public static int dpToPx(int dp)
{
    return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}

public static int pxToDp(int px)
{
    return (int) (px / Resources.getSystem().getDisplayMetrics().density);
}

cheers,

Hamad

Answer:

this is what I made to show code next to barcode generated by zxing:

                        final TextView ticketCodeTV = (TextView) dialogView.findViewById(R.id.ticketCodeTV);
                        Display display = getActivity().getWindowManager().getDefaultDisplay();
                        Point size      = new Point();
                        display.getSize(size);
                        RelativeLayout.LayoutParams lp    = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                        lp.rightMargin  = -(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 135, getResources().getDisplayMetrics());
                        lp.topMargin    = Math.round((size.y / 2) - (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90, getResources().getDisplayMetrics()));
                        Log.d("rightMargin", Integer.toString(lp.rightMargin));
                        ticketCodeTV.setLayoutParams(lp);
                        ticketCodeTV.setText(promo.getTicketCode());

enter image description here

add this in layout xml:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text=""
        android:background="#ffff"
        android:textColor="#000000"
        android:layout_marginBottom="20dp"
        android:rotation="-90"
        android:textSize="25sp"
        android:textStyle="normal"
        android:paddingLeft="70dp"
        android:paddingRight="70dp"
        android:id="@+id/ticketCodeTV"
        />
</RelativeLayout>