Skip to content Skip to sidebar Skip to footer

How To Set Edittext Topmargins In Dp Programatically?

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. Le

Solution 1:

set like follw code:

LinearLayout.LayoutParamslp=newLinearLayout.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

publicstaticintconvertPixelsToDp(float px, Context context){
    Resourcesresources= context.getResources();
    DisplayMetricsmetrics= resources.getDisplayMetrics();
    intdp= 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);

Solution 2:

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

Solution 3:

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.LayoutParamslp=newLinearLayout.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:

publicstaticintdpToPx(int dp){
    return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}

publicstaticintpxToDp(int px){
    return (int) (px / Resources.getSystem().getDisplayMetrics().density);
}

cheers,

Hamad

Solution 4:

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

finalTextViewticketCodeTV= (TextView) dialogView.findViewById(R.id.ticketCodeTV);
                        Displaydisplay= getActivity().getWindowManager().getDefaultDisplay();
                        Pointsize=newPoint();
                        display.getSize(size);
                        RelativeLayout.LayoutParamslp=newRelativeLayout.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:

<RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_alignParentRight="true"android:layout_centerVertical="true"><TextViewandroid: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>

Post a Comment for "How To Set Edittext Topmargins In Dp Programatically?"