Search code examples
androidscrollviewsettext

android setText doesn't work with scrollview


here's my code xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.secandroidapp.MainActivity"
tools:ignore="MergeRootFrame" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:onClick="postData" />

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="fill"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />
    </LinearLayout>
</ScrollView>
</FrameLayout>

mainactivity

public void postData(View view) {
    // Create a new HttpClient and Post Header
    Toast.makeText(this, "postData", Toast.LENGTH_LONG).show();

    AsyncHttpClient client = new AsyncHttpClient();
    client.get("http://cs-server.usc.edu:21111/geneXML.php?CSymbol=GOOG", new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            TextView show = (TextView) findViewById(R.id.textView1);
            show.setText(response);
        }
    });

The problem is when press button, nothing works, not even in logcat, but if i put the button inside the linearlayout, then data will show out correctly, but after pressed the button, it'll disappear and only leave the data. I guess this is because button and textview is in different view? but how to fix this? Thanks!


Solution

  • Replace FrameLayout with LinearLayout and try it. Try to understand the where and why FrameLayout is used.

    FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.