How to configure EditText TopMargins in dp programmatically?

In my Android application, I want to change the topMargin of the editText file.

The thing is, I want to change its “dp” wise, not pixel.

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

programmatically can i set fields only in int?

+3
android android-layout margin layout
source share
4 answers

set as the following 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 the following code to change only one.

  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.topMargin = convertPixelsToDp(5,this); editText.setLayoutParams(params); 
+7
source share
 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); lp.setMargins(left, top, right, bottom); editText.setLayoutParams(lp); 
+3
source share

you cannot set fields directly using dp programmatically, because the .setMargins method sets pixels not for dp, so if you want to give dp instead of pixels, you must convert dps to pixels. first create an instance of LayoutParams:

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

then set to convert dp to pixels and set the layout fields as follows:

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

This is a 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); } 

amuses

Hamad

+1
source share

this is what i did to show the code next to the zxing generated barcode:

  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 to the xml layout:

 <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> 
0
source share

All Articles