2

I have implemented Dynamic TextViews. I am able to view my dynamically generated textview. however, I need to implement a scrollview:

1.Using Code only.

Kindly help.
How do i achieve these 2 functionality? The code below works just fine(it gets all the textview and displays in the screen dynamically but without the scrolling functionality)

TextView[] textViewArray = new TextView[iterator]; for( int i = 0; i < iterator; i++) { textViewArray[i] = new TextView(narutoLinksOnly.this); textViewArray[i].setText(narutoLinkHeadingName[i]); textViewArray[i].setId(i); textViewArray[i].setTextColor(0xff000000); textViewArray[i].setTextSize(20); textViewArray[i].setOnClickListener(this); textViewArray[i].setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));//suggested //textViewArray[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); ((LinearLayout) linearLayout).addView(textViewArray[i]); } 

InSide Oncreate:

linearLayout = findViewById(R.id.dynamicTextview1); 

XML Code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dynamicTextview1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/Ivory" android:orientation="vertical" > </LinearLayout> 
5
  • 1
    put the linearlayout inside scrollview in your xml.. Commented Feb 5, 2014 at 19:19
  • @RanjitPati Yes It works that way :). But how do we achieve the same using code and not by defining the same in xml. Commented Feb 5, 2014 at 19:20
  • move xmlns:android="schemas.android.com/apk/res/android" to scrollview th parent element of your layout Commented Feb 5, 2014 at 19:24
  • t works that way ranjit, but i am looking to achieve the same by code. Commented Feb 5, 2014 at 19:25
  • 1
    programatically crete scrollview with current context and add the total layout inside this after adding the textviews. Commented Feb 5, 2014 at 19:33

2 Answers 2

1

I tried it again in my editor with some different values and names, but concept is same as you.

My Activity class i.e MainActivity.java:

 public class MainActivity extends Activity implements OnClickListener { ScrollView scrollView; LinearLayout linearLayout; String[] narutoLinkHeadingName = { "abcv", "bvvvv", "cvvvv", "dvvvv", "avvvv", "bvvvv", "cvvvv", "d", "a", "b", "c", "d", "a", "b", "c", "d", "avvvv", "b", "c", "d", "a", "vvvb", "c", "vvvvd", "a", "vvvb", "cvvvv", "vvvvd" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); linearLayout = (LinearLayout) findViewById(R.id.dynamicTextview1); scrollView = new ScrollView(MainActivity.this); scrollView.setBackgroundColor(Color.BLUE); TextView[] textViewArray = new TextView[narutoLinkHeadingName.length]; for (int i = 0; i < narutoLinkHeadingName.length; i++) { textViewArray[i] = new TextView(MainActivity.this); textViewArray[i].setText(narutoLinkHeadingName[i]); textViewArray[i].setId(i); textViewArray[i].setTextColor(0xff000000); textViewArray[i].setTextSize(20); textViewArray[i].setOnClickListener(this); textViewArray[i].setLayoutParams(new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));// suggested ((LinearLayout) linearLayout).addView(textViewArray[i]); } if(((ViewGroup)linearLayout.getParent()) != null){ ((ViewGroup)linearLayout.getParent()).removeView(linearLayout); scrollView.addView(linearLayout); addContentView(scrollView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); }else{ scrollView.addView(linearLayout); addContentView(scrollView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); } } @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; } @Override public void onClick(View v) { // TODO Auto-generated method stub } } 

My layout i.e activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dynamicTextview1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff00ff" android:orientation="vertical" > </LinearLayout> 

Now its perfectly scrolling vertically and for horizontal scrolling you can use the HorizontalScrollView in developer site.

NOTE: We have to take care of removeView() method otherwise it may give IllegalStateException like The specified child already has a parent. You must call removeView() on the child's parent first

Sign up to request clarification or add additional context in comments.

6 Comments

The scroll view should match_parent, not wrap_content, the TextView, on the other hand, must wrap_content (to allow the scroll view to measure the size during onMeasure and onLayout). And finally, don't use FILL_PARENT, that is deprecated.
@Ranjit When you are doing scrollView.addView(linearLayout); are you putting the scrollview insde the linear layout or vice versa?
no its vice versa.. the linearlayout is inside the scrollview..in my answer i'm showing the total activity and the layout which i used in activity..apart from this nothing is there.
Thanks Ranjit. But does this line has any benefits? ((ViewGroup)linearLayout.getParent()).removeView(linearLayout);
a traditional example ..scrollview.removeView() is like as touch your nose directly because you know the position of your nose in your face. ((ViewGroup)linearLayout.getParent()).removeView(linearLayout) is like as touch your nose indirectly that is your hand will move through backside of your head and touch your nose because here you don't know the actual position of your nose.
|
0

just put your linearlayout inside scrollview in xml.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" > <LinearLayout android:id="@+id/dynamicTextview1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/Ivory" android:orientation="vertical" > </LinearLayout> </ScrollView> 

2 Comments

Actually i have edited my question . I need to achieve this using code only.BTW your's suggested way works , but i am looking for achieving the same using code.
when you are adding any view to scrollView then its child layout height must be wrap_content.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.