I have lists of LinearLayouts with horizontal orientation each one containing two textviews added dynamically.
This LinearLayout is finally wrapped into master LinearLayout.
I want the second textview of each linear layout to be right aligned progrmatically. How can I do this dynamically.
Here’s sample code:
LinearLayout placeHolderLinearLayout = (LinearLayout)findViewById(R.id.listhosts);
//Several such layouts with 2 text views will be added to placeholder
LinearLayout l = new LinearLayout(this);
l.setClickable(true);
TextView h = new TextView(this);
h.setText("left");
h.setSingleLine(true);
TextView t = new TextView(this);
t.setText("right");
t.setSingleLine(true);
l.addview(h);
l.addview(t);
placeHolderLinearLayout.addView(l);
There is android:layout_alignParentRight attribute. But how to set this dynamically in this case. Any clue?
The
android:layout_alignParentRightcan only be applied to a view if its parent is aRelativeLayout. Change your container to that, and the 2 sub-views can use any of thelayout_alignParent*attributes.If you can’t do this programatically (which I can’t see how to do quickly), then you could always define your inner layout in xml (where you can easily get the layout correct) and inflate manually via:
Edit: added layout definition
Use a layout like this, and inflate it in the code as above:
You will be creating multiple of these layouts for each item you’re adding to your list.