Reputation: 379
i have a LinearLayout. and i am adding item in runtime. but all the items are display from TOP to BOTTOM. Now i am trying to display items BOTTOM to TOP.
I mean. i want to starting from BOTTOM to TOP for set the items in linear layout.
This is my linear layout:-
messagesContainer = (ViewGroup) findViewById(R.id.messagesContainer);
scrollContainer = (ScrollView) findViewById(R.id.scrollContainer);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
if (!leftSide) {
bgRes = R.drawable.right_message_bg;
params.gravity = Gravity.RIGHT;
params.leftMargin=30;
}
else
{
params.gravity = Gravity.LEFT;
params.rightMargin=30;
}
textView.setLayoutParams(params);
textView.setBackgroundResource(bgRes);
runOnUiThread(new Runnable() {
@Override
public void run() {
messagesContainer.addView(textView,messagesContainer.getChildCount());
//messagesContainer.bringChildToFront(textView);
// Scroll to bottom
if (scrollContainer.getChildAt(0) != null) {
scrollContainer.scrollTo(scrollContainer.getScrollX(), scrollContainer.getChildAt(0).getHeight());
}
scrollContainer.fullScroll(View.FOCUS_DOWN);
scrollContainer.pageScroll(View.FOCUS_DOWN);
}
});
please help me.
Upvotes: 6
Views: 7802
Reputation: 19039
Also, If you want the message to display from bottom to top, you need to add them to the
ViewGroup
in that order.
Set the Gravity to BOTTOM
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
params.weight = 1.0f;
params.gravity=48;
button.setLayoutParams(params);
For gravity values and how to set gravity check Gravity
Upvotes: 1
Reputation: 2719
You can add it programmatically with:
LinearLayout layout = findViewById(R.id.layout);
layout.addView(newView, index);
You just have to add always with index = 0
And you can also use android:gravity="bottom"
Upvotes: 9