Question

i am trying to add a Text-view in a Scroll-view and then adding this scroll view to Relative-layout programmatically but its just showing empty view even adding only text-view to Relative-layout its working.

    TextView aboutTextView = new TextView(this);
    aboutTextView.setText(R.string.aboutText);

    aboutTextView.setId(5);
    aboutTextView.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

    ScrollView scroll = new ScrollView(this);
    scroll.setPadding(R.dimen.scroll_padding, R.dimen.scroll_padding,
            R.dimen.scroll_padding, R.dimen.scroll_padding);
    scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));
    scroll.addView(aboutTextView);

    RelativeLayout fragmentContainer = (RelativeLayout) findViewById(R.id.fragmentContainer);
    fragmentContainer.addView(scroll);
Was it helpful?

Solution

Try this code. I hope it will useful.

1) Java Code

package com.temp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class MainActivity extends Activity {

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

    TextView aboutTextView = new TextView(this);
    aboutTextView
            .setText("Temp Text Temp Text Temp");

    aboutTextView.setId(5);
    aboutTextView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

    ScrollView scroll = new ScrollView(this);
    scroll.setPadding(5, 5, 5, 5);
    scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    scroll.addView(aboutTextView);

    RelativeLayout fragmentContainer = (RelativeLayout) findViewById(R.id.fragmentContainer);
    fragmentContainer.addView(scroll);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

2) XML Code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    </RelativeLayout>

</RelativeLayout>

Place of Temp Text Temp Text Temp put your text.
It working for me. Check it out.

Thank you.

OTHER TIPS

ScrollView scroll = new ScrollView(context);
scroll.setBackgroundColor(android.R.color.transparent);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                             LayoutParams.FILL_PARENT));
scroll.addView(yourview);

Don't add the ScrollView in the same statement as you grab the RelativeLayout object. And I think you should specify which implementation of LayoutParams you are using.

aboutTextView.setLayoutParams(new ScrollView.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
ScrollView scroll = new ScrollView(this);
int scrollPadding = getResources().getDimensionPixelSize(R.dimen.scroll_padding);
scroll.setPadding(scrollPadding, scrollPadding, scrollPadding, scrollPadding);
scroll.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));
scroll.addView(aboutTextView);
RelativeLayout fragmentContainer = (RelativeLayout) findViewById(R.id.fragmentContainer);
    fragmentContainer;
fragmentContainer.addView(scroll);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top