Reputation: 1075
I want to do something like XML attr when dynamically adding a view:
android:layout_alignRight="@+id/myView1">
my activity code:
button1.setOnClickListener(new View.OnClickListener(){
@Override public void onClick(View v) {
RelativeLayout ll = (RelativeLayout)findViewById(R.id.aincrad);
TextView view = new TextView(Activity.this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_RIGHT,R.id.Btn1Holder);
view.setText("testing...");
view.setLayoutParams(layoutParams);
ll.addView(view, layoutParams);
}});
the result should aligned the new textview stay right of Btn1Holder
Upvotes: 0
Views: 1358
Reputation: 6862
I'm not sure this is the right approach, but I guess you have to call setLayoutParams
before adding your view to the layout.
If on the other hand you want be sure of having the view right there, you could add it to your xml and set trigger its visibility from the code.
UPDATE: AlignRight "Makes the right edge of this view match the right edge of the given anchor view ID." it means that it works if your layout is under the target and you want to align their edges. You need to use RIGHT_OF which "Rule that aligns a child's left edge with another child's right edge. "
Upvotes: 1