Changing textView at runtime from xml file

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

Changing textView at runtime from xml file

Postby chris_uk_83 » Sat Oct 31, 2009 4:41 pm

I'm trying to create a score table using a tablelayout, tableRows and TextViews. My code will add a new row each time the "add row" button is pressed (at this point I'm trying to sort the concept out, later it will add the scores that a player achieves whenever a score() method is called).

I've created a particularly laid out row in an xml file and am using a LayoutInflater to add it to my TableLayout each time the button is pressed.

What I can't do is actually change the text in the TextViews, thus rendering me unable to enter any scores!

Here's the code for the new row:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <TableRow
  4.  
  5.         xmlns:android="http://schemas.android.com/apk/res/android"
  6.  
  7.         android:id="@+id/row1"
  8.  
  9.         android:layout_width="fill_parent"
  10.  
  11.         android:layout_height="wrap_content">
  12.  
  13. <TextView
  14.  
  15.         android:layout_gravity="center"
  16.  
  17.         android:id="@+id/gap_filler"
  18.  
  19.         android:text="-"
  20.  
  21.         android:layout_width="wrap_content"
  22.  
  23.         android:layout_height="fill_parent"     />
  24.  
  25. <TextView
  26.  
  27.         android:layout_gravity="center"
  28.  
  29.         android:id="@+id/my_textview1"
  30.  
  31.         android:text="-"
  32.  
  33.         android:layout_width="wrap_content"
  34.  
  35.         android:layout_height="fill_parent"     />
  36.  
  37. <TextView      
  38.  
  39. android:layout_gravity="center"
  40.  
  41.         android:id="@+id/my_textview2"
  42.  
  43.         android:text="-"
  44.  
  45.         android:layout_width="wrap_content"
  46.  
  47.         android:layout_height="fill_parent"     />
  48.  
  49. <TextView      
  50.  
  51.         android:layout_gravity="center"
  52.  
  53.         android:id="@+id/my_textview3"
  54.  
  55.         android:text="-"
  56.  
  57.         android:layout_width="wrap_content"
  58.  
  59.         android:layout_height="fill_parent"     />
  60.  
  61. <TextView      
  62.  
  63.         android:layout_gravity="center"
  64.  
  65.         android:id="@+id/my_textview4"
  66.  
  67.         android:text="-"
  68.  
  69.         android:layout_width="wrap_content"
  70.  
  71.         android:layout_height="fill_parent"     />
  72.  
  73. <TextView      
  74.  
  75.         android:layout_gravity="center"
  76.  
  77.         android:id="@+id/my_textview5"
  78.  
  79.         android:text="-"
  80.  
  81.         android:layout_width="wrap_content"
  82.  
  83.         android:layout_height="fill_parent"     />
  84.  
  85. <TextView      
  86.  
  87.         android:layout_gravity="center"
  88.  
  89.         android:id="@+id/my_textview6"
  90.  
  91.         android:text="-"
  92.  
  93.         android:layout_width="wrap_content"
  94.  
  95.         android:layout_height="fill_parent"     />
  96.  
  97. <TextView      
  98.  
  99.         android:layout_gravity="center"
  100.  
  101.         android:id="@+id/total_textview"
  102.  
  103.         android:text="-"
  104.  
  105.         android:layout_width="wrap_content"
  106.  
  107.         android:layout_height="fill_parent"     />
  108.  
  109.  
  110.  
  111.        
  112.  
  113. </TableRow>
Parsed in 0.006 seconds, using GeSHi 1.0.8.4


and this is the code I'm trying to use to implement it:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package tiff.messing;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.content.Context;
  8.  
  9. import android.os.Bundle;
  10.  
  11. import android.view.LayoutInflater;
  12.  
  13. import android.view.View;
  14.  
  15. import android.view.View.OnClickListener;
  16.  
  17. import android.widget.Button;
  18.  
  19. import android.widget.TableLayout;
  20.  
  21. import android.widget.TextView;
  22.  
  23.  
  24.  
  25.  
  26.  
  27. public class messing extends Activity {
  28.  
  29.        
  30.  
  31.         private int endNo = 1;
  32.  
  33.         private int shot1 = 0;
  34.  
  35.         private int shot2 = 0;
  36.  
  37.         private int shot3 = 0;
  38.  
  39.         private int shot4 = 0;
  40.  
  41.         private int shot5 = 0;
  42.  
  43.         private int shot6 = 0;
  44.  
  45.        
  46.  
  47.         private int total = 0;
  48.  
  49.        
  50.  
  51.         private TextView tvEnd;
  52.  
  53.         private TextView tvShot1;
  54.  
  55.         private TextView tvShot2;
  56.  
  57.         private TextView tvShot3;
  58.  
  59.         private TextView tvShot4;
  60.  
  61.         private TextView tvShot5;
  62.  
  63.         private TextView tvShot6;
  64.  
  65.        
  66.  
  67.     /** Called when the activity is first created. */
  68.  
  69.     @Override
  70.  
  71.     public void onCreate(Bundle savedInstanceState) {
  72.  
  73.         super.onCreate(savedInstanceState);
  74.  
  75.         this.setContentView(R.layout.main);
  76.  
  77.  
  78.  
  79.         final LayoutInflater inflater = (LayoutInflater) this.getSystemService(
  80.  
  81.                         Context.LAYOUT_INFLATER_SERVICE);
  82.  
  83.        
  84.  
  85.         final TableLayout tl = (TableLayout) findViewById(R.id.myTableLayout);
  86.  
  87.        
  88.  
  89.         //This button adds a row to the table. The row is a seperate xml file.
  90.  
  91.         Button newRow = (Button) findViewById(R.id.new_row_button);
  92.  
  93.         newRow.setOnClickListener(new OnClickListener() {
  94.  
  95.                 public void onClick(View v)     {
  96.  
  97.                         inflater.inflate(R.layout.tablerow, tl);
  98.  
  99.                        
  100.  
  101.                         ;
  102.  
  103.                         endNo++;
  104.  
  105.                 }
  106.  
  107.                
  108.  
  109.         });
  110.  
  111.        
  112.  
  113.         //The code I'm trying to use to add text to my score row
  114.  
  115.         Button fillText = (Button) findViewById(R.id.fill_button);
  116.  
  117.         fillText.setOnClickListener(new OnClickListener() {
  118.  
  119.                 public void onClick(View v)     {
  120.  
  121.                        
  122.  
  123.                         TextView peas = (TextView) findViewById(R.id.gap_filler);
  124.  
  125.                         peas.setText(endNo);
  126.  
  127.                         endNo++;
  128.  
  129.                 }
  130.  
  131.                
  132.  
  133.         });
  134.  
  135.     }
  136.  
  137. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


Help with this issue would be much appreciated!
chris_uk_83
Freshman
Freshman
 
Posts: 5
Joined: Sat Oct 31, 2009 4:34 pm

Top

Postby qlimax » Sun Nov 01, 2009 8:55 pm

hi chris,
1. You can obtain the inflater with this.getLayoutInflater();
2. I suppose that the behaviour of your program, is that only the first row get modified with the incremented values. and the other added rows remain -------
is that correct? what's the real behaviour of your program?

I have a solution but, it don't use xml file for define a row.

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

Postby chris_uk_83 » Mon Nov 02, 2009 11:04 pm

No, no rows get filled; the program force closes when I try to use setText() on any of the TextView elements in the row.

I'd be happy to try a non-xml solution if I could get the same behaviour from it.
chris_uk_83
Freshman
Freshman
 
Posts: 5
Joined: Sat Oct 31, 2009 4:34 pm

Postby qlimax » Tue Nov 03, 2009 9:52 pm

by code:

declare an empty tablelayout in your ui xml

then link it with findviewbyid (tableLayout)

then to construct a row do this

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. TextView cell1=new TextView(this);
  2.  
  3. TextView cell2=new TextView(this);
  4.  
  5.  
  6.  
  7. cell1.seGravity(...)  //you have all the properties to set for each cell
  8.  
  9. cell1.setLayoutParams(...)// "" "" ""same for the cell2
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


then construct the row with these cells

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. TableRow tr=new TableRow(this);
  2.  
  3. tr.addView(cell1);
  4.  
  5. tr.addView(cell2);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


then add it to the table
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. tableLayout.addView(tr);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


bye
:run:

edit: of course you can setText on the textview directly when you construct the table

to get the cells (in a second moment)from the table:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. TableRow tr=(TableRow)tableLayout.getChildAt(index i)
  3.  
  4. TextView tv=(TextView)tr.getChildAt(index i);
  5.  
  6. tv.setText(...)
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


you can also remove all rows from the table by call
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. tableLayout.removeAllViews();
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby chris_uk_83 » Wed Nov 04, 2009 11:00 pm

Thanks for the help, I've had a go at using your idea but it doesn't seem to work. The program does not crash, but the new row is not visible either. All I get is my original TableLayout without the new row or cells displayed on the screen. I've tried setTextColor() to make sure it's not just displaying in black, am I missing something?

This is a bit odd because if I use a framelayout instead I can add a TextView to it perfectly with no problems.

EDIT: I've got it working, but only if I don't use setLayoutParams(). I have no idea why I can't change it's layout at runtime, but I can't. Ideas?

Cheers
chris_uk_83
Freshman
Freshman
 
Posts: 5
Joined: Sat Oct 31, 2009 4:34 pm

Postby qlimax » Wed Nov 04, 2009 11:44 pm

on witch view you can't set the params?

the cells?the row?the tablelayout?
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Top

Postby chris_uk_83 » Thu Nov 05, 2009 7:00 pm

The cells. If I try to use cell1.setLayoutParams(etc...) the cell is not drawn. Don't worry about it though, it works perfectly if I just don't bother and I can still set other layout things like gravity, weight etc.

Thanks for your help, you've been ace!
chris_uk_83
Freshman
Freshman
 
Posts: 5
Joined: Sat Oct 31, 2009 4:34 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests