-2

Any idea to display the last textview inside a LinearLayout inside a ScrollView?

This is my code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.qianonnphoon.tradeal.chat.ChatActivity"
android:background="#ffffff"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical">

<ScrollView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/scrollView">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/layout1">

</LinearLayout>
</ScrollView>

<include
layout="@layout/message_area"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_marginTop="5dp"/>
</LinearLayout>

TextView will be added to layout1 when receive the data. So I will have multiple textviews in layout1 once finish loading. However, it display the first TextView then I need to scroll down to see the last TextView. Any idea to display the last TextView when it finish loading?

This is the code how I add TextView when receive data

TextView textView = new TextView(ChatActivity.this);
    textView.setText(message);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    lp.setMargins(0, 0, 0, 10);
    textView.setLayoutParams(lp);

    if(type == 1) {
        textView.setBackgroundResource(R.drawable.rounded_corner1);
    }
    else{
        textView.setBackgroundResource(R.drawable.rounded_corner2);
    }

    layout.addView(textView);
    scrollView.fullScroll(View.FOCUS_DOWN);
Ghulam Moinul Quadir
  • 1,638
  • 1
  • 12
  • 17
phoon
  • 369
  • 1
  • 6
  • 21

1 Answers1

0

This solution worked for me

final ScrollView scrollview = ((ScrollView) findViewById(R.id.scrollview));
scrollview.post(new Runnable() {
    @Override
    public void run() {
        scrollview.fullScroll(ScrollView.FOCUS_DOWN);
    }
});
phoon
  • 369
  • 1
  • 6
  • 21