0

I can successfully add a textview to a scroll view on a button click as shown in the two screenshots: screenshot 1 and screenshot 2. But what if instead I want the textviews to be added to the bottom of the screen first, and each added textview pushes the previous one above it, which would then cause me to scroll up rather than down to see past textviews? I have been trying to achieve this for quite some time and can not get it to work. Any suggestions? I attempted to simply change the linear layout layout-gravity which works until the scroll part comes in.

<ScrollView
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginBottom="8dp"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toTopOf="@+id/editText">
    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:orientation="vertical"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    </LinearLayout>
</ScrollView>
<EditText
    android:id="@+id/editText"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:ems="10"
    android:inputType="textPersonName"
    android:hint="Enter Text"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.508"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/button" />
<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Create"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="1dp"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginEnd="8dp" />


private LinearLayout layout;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    layout = (LinearLayout) findViewById(R.id.layout);
    editText = (EditText) findViewById(R.id.editText);
    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(onClick());
}
private View.OnClickListener onClick() {
    return new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            layout.addView(createNewTextView(editText.getText().toString()),0);
        }
    };
}
private TextView createNewTextView(String text) {
    final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams
            (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    final TextView textView = new TextView(this);
    lparams.gravity = Gravity.BOTTOM;
    textView.setLayoutParams(lparams);
    textView.setText("New text: " + text);
    textView.setTextSize(30);;
    return textView;
}

2 Answers 2

1

Try using RelativeLayout as your root layout.

Try this:

<RelativeLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background">

    <LinearLayout
        android:id="@+id/bottom_layout"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_weight="4">
        <EditText
            android:id="@+id/edit"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/butt"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="GO"
            android:layout_weight="4"/>
    </LinearLayout>

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_layout">

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


        </LinearLayout>
    </ScrollView>

</RelativeLayout>
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the response, but the relative layout does not fix the issue.
@Pavan. Deleted old post, so use this for discussion.
0

After adding your new TextView, you can call scrollView.fullScroll(ScrollView.FOCUS_DOWN);

In your XML layout code:

<ScrollView
    android:id="@+id/scrollview"
    <!-- this adds an id for your scrollview -->
    <!-- rest of your XML layout below -->

In your activity code:

private ScrollView scrollView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Add the scrollView reference
    this.scrollView = (ScrollView) findViewById(R.id.scrollview);
    ...rest of onCreate

private View.OnClickListener onClick() {
    return new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            layout.addView(createNewTextView(editText.getText().toString()),0);
            // Scroll the scrollView to the bottom
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
        }
    };
}

Source: How to scroll to bottom in a ScrollView on activity startup

Tip: Instead of adding TextViews into a ScrollView, you can use a ListView or a RecyclerView.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.