[SOLVED]Generating(loop) TextViews from Array?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

[SOLVED]Generating(loop) TextViews from Array?

Postby scheich » Tue Dec 22, 2009 1:22 pm

Hi!

Is it possible to generate Textviews from an array?
I mean in the way like this:(pseudocode)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. for(int i=0, i<array.length(), i++) {
  3.  
  4. Textview [i] = new TextView.findViewById(R.id.LinearLayout01);
  5.  
  6. [i].setText(array[i])
  7.  
  8. }
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
Last edited by scheich on Tue Dec 22, 2009 6:15 pm, edited 1 time in total.
scheich
Junior Developer
Junior Developer
 
Posts: 16
Joined: Thu Nov 26, 2009 9:58 pm

Top

Postby qlimax » Tue Dec 22, 2009 2:09 pm

you can do something like this: (pseudocode)
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. LinearLayour lay=findViewById(R.id.linla01);
  3.  
  4.  
  5.  
  6. ArrayList<TextView> tvReferences=new ArrayList<TextView>();
  7.  
  8. //optional ... you can get the views from the layout, but you have to know the child number of the view you added
  9.  
  10.  
  11.  
  12. String[] textArray={"one","two","asdasda"};
  13.  
  14.  
  15.  
  16. int length=textArray.length(); //performances
  17.  
  18.  
  19.  
  20. for(int i=0;i<length;i++){
  21.  
  22. TextView tv=new TextView(getApplicationContext());
  23.  
  24. tv.setText(textArray[i]);
  25.  
  26. lay.addView(tv);
  27.  
  28. tvReferences.add(tv);//optional
  29.  
  30. }
  31.  
  32.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


hope it helps
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby scheich » Tue Dec 22, 2009 6:07 pm

Thx a lot. Works fine!

btw, is there a possibility to clear/reset that LinearLayout? I mean remove all objects(TextViews) in it?
EDIT:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. lay.removeAllViewsInLayout();
  2.  
  3.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
scheich
Junior Developer
Junior Developer
 
Posts: 16
Joined: Thu Nov 26, 2009 9:58 pm

Postby achie1266 » Tue Dec 22, 2009 6:37 pm

scheich wrote:Thx a lot. Works fine!

btw, is there a possibility to clear/reset that LinearLayout? I mean remove all objects(TextViews) in it?
EDIT:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. lay.removeAllViewsInLayout();
  2.  
Parsed in 0.043 seconds, using GeSHi 1.0.8.4

you need to use this method to remove the views
lay.removeAllViews();
you can also customize to remove child views within a range.
http://developer.android.com/reference/ ... ayout.html
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Postby scheich » Wed Dec 23, 2009 1:26 am

Thx.
Another little question.:
How can I influence the sharing of the Layout?
There's no tv.layout_weight..
scheich
Junior Developer
Junior Developer
 
Posts: 16
Joined: Thu Nov 26, 2009 9:58 pm

Postby achie1266 » Wed Dec 23, 2009 1:40 am

scheich wrote:Thx.
Another little question.:
How can I influence the sharing of the Layout?
There's no tv.layout_weight..
What do u mean by influencing the sharing of layout? can you give an example or a little more explination of what you are trying to do.
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Top

Postby scheich » Wed Dec 23, 2009 1:43 am

I want to share the whole space of the Layout for the generated TextViews. If the TextViews reache the right border, it should continue in the next line.
scheich
Junior Developer
Junior Developer
 
Posts: 16
Joined: Thu Nov 26, 2009 9:58 pm

Postby achie1266 » Wed Dec 23, 2009 1:47 am

scheich wrote:I want to share the whole space of the Layout for the generated TextViews. If the TextViews reache the right border, it should continue in the next line.
It would normally do that when you set layout_width to fill-parent and layout_height to wrap_content.
It should be the case normally unless you are setting the textView to single line.
Can you post your layout code here if it still does not work.
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Postby scheich » Wed Dec 23, 2009 1:58 am

I have several LLayouts in a RLayout. I use a specific high to get the right distance to next Layouts.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. <LinearLayout
  2.  
  3.         android:id="@+id/LinearLayout01"
  4.  
  5.         android:layout_height="160dp"
  6.  
  7.         android:layout_width="fill_parent">
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Changed 160dp to wrap_content, but that didn't work.
scheich
Junior Developer
Junior Developer
 
Posts: 16
Joined: Thu Nov 26, 2009 9:58 pm

Postby achie1266 » Wed Dec 23, 2009 2:03 am

hmm well it seems that it might have something to do with the relative layout and the other layouts within it too. I can't say anything by just looking at this linearlayout tag that you posted.
Anyway what is happening when you set the height to 160dp and what does it do when it is wrap_content.
Is it possible for you to post the entire layout? That might give us a better idea.
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Postby scheich » Wed Dec 23, 2009 11:57 am

Yes, sure:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <!-- HTC Dream/Port -->
  4.  
  5. <RelativeLayout
  6.  
  7.    android:id="@+id/RLayout"
  8.  
  9.    android:layout_width="wrap_content"
  10.  
  11.    android:layout_height="fill_parent"
  12.  
  13.    android:orientation="vertical"
  14.  
  15.    android:background="#FFFFFF"
  16.  
  17.    xmlns:android="http://schemas.android.com/apk/res/android">
  18.  
  19.        
  20.  
  21.         <LinearLayout
  22.  
  23.                 android:id="@+id/LinearLayout01"
  24.  
  25.                 android:layout_height="160dp"
  26.  
  27.                 android:background="@drawable/one"
  28.  
  29.                 android:layout_width="fill_parent">            
  30.  
  31.         </LinearLayout>
  32.  
  33.        
  34.  
  35.         <LinearLayout
  36.  
  37.                 android:id="@+id/LinearLayout02"
  38.  
  39.                 android:layout_width="fill_parent"
  40.  
  41.                 android:orientation="vertical"
  42.  
  43.                 android:layout_below="@+id/LinearLayout01"
  44.  
  45.                 android:layout_height="60dp">
  46.  
  47.         </LinearLayout>                
  48.  
  49.        
  50.  
  51.         <LinearLayout
  52.  
  53.                 android:id="@+id/LinearLayout03"
  54.  
  55.                 android:layout_width="fill_parent"
  56.  
  57.                 android:layout_height="wrap_content"
  58.  
  59.                 android:orientation="horizontal"
  60.  
  61.                 android:layout_below="@+id/LinearLayout02">
  62.  
  63.         </LinearLayout>
  64.  
  65.  
  66.  
  67. </RelativeLayout>
Parsed in 0.004 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. LinearLayout lay=(LinearLayout)findViewById(R.id.LinearLayout01);
  2.  
  3. lay.removeAllViewsInLayout();
  4.  
  5.  
  6.  
  7. String[] textArray={"a","b","c","d","e","f","g","h","i","j","k"};
  8.  
  9. int length=textArray.length;
  10.  
  11.  
  12.  
  13. for(int i=0;i<length;i++){
  14.  
  15.         TextView tv =new TextView(getApplicationContext());
  16.  
  17.         tv.setText(textArray[i]);
  18.  
  19.         tv.setTextSize(30);
  20.  
  21.         tv.setGravity(Gravity.CENTER);
  22.  
  23.         lay.addView(tv);
  24.  
  25. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Problem is if textArray > 8:
| tv tv tv tv tv tv tv tv |tv tv

want it that way:

| tv tv tv tv tv tv tv tv |
| tv tv

Thx a lot for your patience
scheich
Junior Developer
Junior Developer
 
Posts: 16
Joined: Thu Nov 26, 2009 9:58 pm

Postby scheich » Wed Dec 23, 2009 5:14 pm

Ok, I solved it, but probably this is only a bad workarround:

Used 4 extra destination layers in the Layout:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <LinearLayout
  3.  
  4.         android:id="@+id/LinearLayout01"
  5.  
  6.         android:layout_height="160dp"
  7.  
  8.         android:background="@drawable/zero"
  9.  
  10.         android:orientation="vertical"
  11.  
  12.         android:layout_width="fill_parent">
  13.  
  14.         <LinearLayout
  15.  
  16.                 android:id="@+id/LinearLayout09"
  17.  
  18.                 android:layout_height="fill_parent"
  19.  
  20.                 android:layout_width="fill_parent"
  21.  
  22.                 android:layout_weight="1"
  23.  
  24.                 android:orientation="horizontal"
  25.  
  26.                 >
  27.  
  28.         </LinearLayout>
  29.  
  30.         <LinearLayout
  31.  
  32.                 android:id="@+id/LinearLayout10"
  33.  
  34.                 android:layout_height="fill_parent"
  35.  
  36.                 android:layout_width="fill_parent"
  37.  
  38.                 android:layout_weight="1"
  39.  
  40.                 android:orientation="horizontal"
  41.  
  42.                 >
  43.  
  44.                
  45.  
  46.         </LinearLayout>
  47.  
  48.         <LinearLayout
  49.  
  50.                 android:id="@+id/LinearLayout11"
  51.  
  52.                 android:layout_height="fill_parent"
  53.  
  54.                 android:layout_width="fill_parent"
  55.  
  56.                 android:layout_weight="1"
  57.  
  58.                 android:orientation="horizontal"
  59.  
  60.                 >
  61.  
  62.         </LinearLayout>
  63.  
  64.         <LinearLayout
  65.  
  66.                 android:id="@+id/LinearLayout12"
  67.  
  68.                 android:layout_width="fill_parent"
  69.  
  70.                 android:layout_height="fill_parent"
  71.  
  72.                 android:layout_weight="1"
  73.  
  74.                 android:orientation="horizontal"
  75.  
  76.                 >
  77.  
  78.         </LinearLayout>
  79.  
  80.         </LinearLayout>
Parsed in 0.005 seconds, using GeSHi 1.0.8.4


I managed the right "line" for the textview this way:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                 LinearLayout lay=(LinearLayout)findViewById(R.id.LinearLayout09);
  3.  
  4.                 LinearLayout lay2=(LinearLayout)findViewById(R.id.LinearLayout10);
  5.  
  6.                 LinearLayout lay3=(LinearLayout)findViewById(R.id.LinearLayout11);
  7.  
  8.                 LinearLayout lay4=(LinearLayout)findViewById(R.id.LinearLayout12);
  9.  
  10.  
  11.  
  12.                 lay.removeAllViews();
  13.  
  14.                 lay2.removeAllViews();
  15.  
  16.                 lay3.removeAllViews();
  17.  
  18.                 lay4.removeAllViews();
  19.  
  20. [...]
  21.  
  22.  
  23.  
  24.               if ( i > <img src="http://www.anddev.org/images/smilies/cool.png" alt="8)" title="Cool" /> {
  25.  
  26.                                 if ( i > 16) {
  27.  
  28.                                         if ( i > 24) {
  29.  
  30.                                                 lay4.addView(tv);
  31.  
  32.                                         }
  33.  
  34.                                         else {
  35.  
  36.                                                 lay3.addView(tv);
  37.  
  38.                                         }
  39.  
  40.                                 }
  41.  
  42.                                 else {
  43.  
  44.                                         lay2.addView(tv);
  45.  
  46.                                 }
  47.  
  48.                         }
  49.  
  50.                         else {
  51.  
  52.                                 lay.addView(tv);
  53.  
  54.                         }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
scheich
Junior Developer
Junior Developer
 
Posts: 16
Joined: Thu Nov 26, 2009 9:58 pm

Postby achie1266 » Wed Dec 23, 2009 5:32 pm

Well it looks like you are increasing the layout tree height.

It may be worthwhile for you to just get the screen size and orientation and then compare with textView width and do all the calculation in the code itself.
You may have to use something like
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Display display = getWindowManager().getDefaultDisplay();
  2.  
  3. int width = display.getWidth();
  4.  
  5. int tvWidth = tv.getWidth();//your text view
  6.  
  7.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
Keep adding textviews to the layout horizontally aslong as the sumof its width and all the previous textviews is not more than the width of the screen /your parent layout.
Also take care that it has to be done when the orientation changes too.
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests