How can I programmatically move to center EditText?

0

How can I programmatically move the Password to the center using .AddRule (RelativeLayout.CENTER_VERTICAL);?

enter image description here

LinearLayout PO = (LinearLayout)RV .findViewById(R.id.LL1);

                EditText ET = new EditText(getActivity());
                ET.setId(1);
                ET.setHint("Your name");
                ET.setInputType(0x00000061);
                ET.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
                ET.setPadding(40,20,40,20);
                ET.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                //ET.addRule(RelativeLayout.CENTER_VERTICAL);
                PO.addView(ET);

.addRule lit.

java
android
eclipse
asked on Stack Overflow Jun 28, 2014 by user3784189 • edited Jun 28, 2014 by Aaron Hall

3 Answers

0

You can use the gravity attribute, but this is useless if you want to add more components in your view. You can also use RelativeLayout instead of LinearLayout (you need to change your layout), which helps you doing things like that.

answered on Stack Overflow Jun 28, 2014 by Angelo Cassano
0

XML:

  <LinearLayout
        android:id="@+id/xxx"
        android:gravity="center_vertical">

or in Code:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
layout.setGravity(Gravity.CENTER_VERTICAL);

or

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
      ((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
xyzView.setLayoutParams(params);
answered on Stack Overflow Jun 28, 2014 by David
0

Before doing that programmatically you'd better try to build your UI in the layout.xml and only then do that things programmatically.

There should be relative layout in your layout.xml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
...

Your edittext should be inside relative layout in order to apply RelativeLayout.LayoutParams to it. And your code will look like that:

RelativeLayout PO = (RelativeLayout)RV .findViewById(R.id.RL1);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
          ((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
ET.setLayoutParams(params);

Also you may found helpful the last answer in the following link: http://www.coderanch.com/t/505316/Android/Mobile/put-layout-middle-screen

answered on Stack Overflow Jun 29, 2014 by Access Denied • edited Jun 29, 2014 by Access Denied

User contributions licensed under CC BY-SA 3.0