How can I programmatically move the Password to the center using .AddRule (RelativeLayout.CENTER_VERTICAL);
?
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.
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.
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);
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
User contributions licensed under CC BY-SA 3.0