0

I would like to know how I could dynamically write the data below in Java. I am stuck on how you write the "layout_alignParent" can set the gravity of each text view....

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_alignParentBottom="true"
    android:gravity="center_horizontal" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:gravity="center_vertical" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView3"
    android:layout_alignBottom="@+id/textView3"
    android:layout_alignParentRight="true"
    android:gravity="center_vertical" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:gravity="center_horizontal" />

I know that the code below would not work. But how could I re-write it for it to work?

RelativeLayout m = (RelativeLayout)findViewById(R.id.tileContainerME);
EditText et1 = new EditText(this);
EditText et2 = new EditText(this);
EditText et3 = new EditText(this);
EditText et4 = new EditText(this);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
    android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
    android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);

params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);        
et1.setLayoutParams(params);
params.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);  
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);    
et2.setLayoutParams(params);
params.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.removeRule(RelativeLayout.CENTER_VERTICAL);

params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);     
et3.setLayoutParams(params);
params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);

params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);      
et4.setLayoutParams(params);
params.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
params.removeRule(RelativeLayout.CENTER_HORIZONTAL);

et1.setGravity(Gravity.CENTER_HORIZONTAL);
et1.setHint("Test");
et2.setGravity(Gravity.CENTER_VERTICAL);
et2.setHint("Test");
et3.setGravity(Gravity.CENTER_VERTICAL);
et3.setHint("Test");
et4.setGravity(Gravity.CENTER_HORIZONTAL);
et4.setHint("Test");

m.addView(et1);
m.addView(et2);
m.addView(et3);
m.addView(et4);
3
  • 2
    The "code below" is not "code", it is data. XML data. Once you understand this, it becomes easy as each XML element corresponds to a property of the object or it's parent which you can determine from the documentation. developer.android.com/reference/android/widget/…. A simple Google search will then offer many examples of how to set these properties in code. Commented Jan 1, 2014 at 21:22
  • Now that I understand it is data, I looked through the link you gave me, it only showed me the methods that I would need, but I am asking about how I can implement them through a simple example. Could you provide me with one perhaps? Commented Jan 1, 2014 at 21:29
  • Kostya's answer is correct. Commented Jan 1, 2014 at 21:39

1 Answer 1

1

I think this code will help you:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT,
    RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
textView.setLayoutParams(params);

Edited:

params.gravity = Gravity.CENTER_HORIZONTAL;
tv2.setLayoutParams(params);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Would gravity be written as follows? textView.setGravity(Gravity.CENTER_HORIZONTAL);
How would I then remove the rule stated for the other textViews?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.