Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinearLayout display item bottom to up

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.

like image 379
user2160008 Avatar asked Dec 27 '22 02:12

user2160008


2 Answers

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"

like image 83
No_Rulz Avatar answered Jan 05 '23 12:01

No_Rulz


Gravity.

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

like image 37
Dheeraj Bhaskar Avatar answered Jan 05 '23 13:01

Dheeraj Bhaskar