0

I am adding one edit text pro-grammatically, in that i am setting the gravity but its not reflecting.

image

code:

EditText bcc = new EditText(getApplicationContext());
        LayoutParams para = new LayoutParams(LayoutParams.FILL_PARENT, 45);
        //bcc.setBackgroundColor(Color.parseColor("#00000000"));
        bcc.setTextColor(Color.parseColor("#000000"));
        bcc.setSingleLine(true);
        para.setMargins(0, 0, 0, 5); // left, top, right, bottom.       
        bcc.setTextSize(15);
        bcc.setGravity(Gravity.BOTTOM);
        bcc.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
        bcc.setId(100);
        bcc.setLayoutParams(para);
Noby
  • 6,562
  • 9
  • 40
  • 63

4 Answers4

1

This gravity bcc.setGravity(Gravity.BOTTOM); marks only how text should lay inside EditText.

If parent of EditText is RelativeLayout you can provide rules inside RelativeLayout.LayoutParams.

Jin35
  • 8,602
  • 3
  • 32
  • 52
1

set gravity of parent of the view. If view parent is layout then the code will be like the following

((LinearLayout) bcc.getParent()).setGravity(Gravity.CENTER_VERTICAL);
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
0

When you creating EditText programmatically, you must at first set setKeyListener(TextKeyListener.getInstance());

Otherwise your view will always be aligned with Gravity.TOP.

I don't know real reason, but before you specifiy any other parameter to EditText, you must set setKeyListener(TextKeyListener.getInstance());

Correction : It only work if you create you custom widget by extending EditText and defining your widget in XML. Only tested on Android 5.0.1

0

Eventually you would be adding this EditText bcc to a view group? Depending on what type of ViewGroup the parent is, you would need to do the following:

LinearLayout:

via XML: You have to set android:layout_gravity="center_vertical".

via code:

LinearLayout.LayoutParams lp = viewGroup.getLayoutParams();
lp.gravity = Gravity.CENTER_VERTICAL;
viewGroup.setLayoutParams(lp);

The code will be different for different parent layout types.

Code Poet
  • 11,227
  • 19
  • 64
  • 97