Search code examples
androidandroid-edittextgravity

Why my edit text is not taking gravity...?


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);

Solution

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