Reputation: 11
I have created the Tab Layout example found on the Android Developers site Tab Layout in Android 2.2. While the example works as advertised I can not scroll at all on the text views that I have placed in one of the activities. The TextView objects were created dynamically in the given activity if that matters. I have tried wrapping the different portions of the components in main.xml with ScrollView but I either get a build error or a Force Close on launch. Attached is the code that implements the textViews and it's xml as well as main.xml file. I cannot post images since I am new. Any suggestions are greatly appreciated.
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<!-- android:padding="5dp" -->
</LinearLayout>
</TabHost>
Source that creates my TextViews in the Tab:
public class Activity2 extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
DisplayTexts(20);
}
private void DisplayTexts(int length){
if(length == 0)
return;
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
for(int i = 0; i < length; i++)
{
TextView tv = new TextView(getApplicationContext());
registerForContextMenu(tv);
tv.setTextSize(30);
tv.setText("TextView " + String.valueOf(i));
tv.setLayoutParams(params);
layout.addView(tv);
tv.setId(i);
}
LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
this.addContentView(layout, layoutParam);
}
xml file for above activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
Upvotes: 1
Views: 1809
Reputation: 19250
Have you tried with this xml for your activity:
activity2.xml:
<?xml version="1.0" encoding="utf-8" ?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_content">
<LinearLayout
android:layout_width="fill_parent"
android:id="@+id/layout1"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
And then try adding component in Activity2.class like:
setContentView(R.layout.activity2);
...
LinearLayout layout = (LinearLayout)findViewById(R.id.layout1);
for(int i = 0; i < length; i++)
{
TextView tv = new TextView(getApplicationContext());
registerForContextMenu(tv);
tv.setTextSize(30);
tv.setText("TextView " + String.valueOf(i));
tv.setLayoutParams(params);
tv.setId(i);
layout.addView(tv);
}
...
Upvotes: 1
Reputation: 444
Instead of addContentView, use SetContentView and try setting canScrollVertically (int direction) for the layout.
A listView could be an alternative for what you are trying to accomplish.
Upvotes: 0