1. Download our Official Android App: Forums for Android!

Apps addView something from xml file

Discussion in 'Android Development' started by Jelmerholland, Feb 17, 2011.

  1. Jelmerholland

    Jelmerholland Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    8
    Joined:
    Feb 17, 2011

    Feb 17, 2011
    8
    0
    5
    Hello guys,

    I'm not sure if I'm in the rigth spot right now.. so.. let's hope I am..

    I have code which creates 9 buttons (1 to 9) in a <TableLayout> (created in a for-loop). But now I'd like to add a TextView element above the 9 buttons to display what you've pressed (or what so ever). It's my first app on android ever, so this is completely new to me. (We HAVE to make an android app for my study... so please help).

    What's not working.. the TextView. I'm trying to get this through a xml file in the layout folder, but it's not working.

    This is de code from the usefull lines:
    Code (Text):
    1.  
    2. //resultBox = (TextView) findViewById(R.id.result_box);
    3.         resultBox = new TextView(this);
    4.         resultBox.setText("Hello World!");
    5.         tr = new TableRow(this);
    6.         tr.addView(resultBox);
    7.         buttonTable.addView(tr,300,40);
    8.        
    9.        
    10.         for( int i = 1; i <= 3; i++ )
    11.         {
    12.             tr = new TableRow(this);
    13.             for( int c = 1; c <= 3; c++)
    14.             {
    15.                 btn = new Button(this);
    16.                 btn.setText("" + (c*i));
    17.                 btn.setTextSize(10);
    18.                 btn.setTextColor(Color.rgb(255, 50, 0));
    19.                 btn.setOnClickListener(this);
    20.                 tr.addView(btn, btnSize, btnSize);
    21.             }
    22.             buttonTable.addView(tr);
    23.         }
    24.         super.setContentView(buttonTable);
    25.  
    Why I'd like to do this throughout the xml file? Well.. I saw you have to use the xml files, it's better for you (as a developer) and when you have to edit something quick it's easier.. especially with huge apps. But for now.. I have to make a span of 3, because the result has to be in 1 row but also in 3 columns. And not in the first column in the upper row. So.. to accomplish the 'span' I have to use xml because there is no such function in the android packages (googled)

    As you can see in the code, currently I'm sin g a TextView created IN the .java file, but as you can see also.. line 1 is commented.. That's what I'm trying to use, but the app just crashes and says: sorry, can't help ya...:(

    I hope you understand what I'm trying to accomplish and I hope that you can help me.
    If you need more code/information or if you want me to be more specific, don't hesitate to ask ;)
     

    Advertisement

  2. miXer

    miXer Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    313
    Joined:
    Aug 12, 2010

    Aug 12, 2010
    313
    51
    53
    Norway
    Have logcat open when trying out the app on the emulator, and post the exception thrown there.
     
  3. JamTheMan

    JamTheMan Well-Known Member
    Rank:
    None
    Points:
    38
    Posts:
    136
    Joined:
    Dec 16, 2010

    I am not quite sure I understand what you are trying to do, but there might be one thing wrong with your approach...

    I guess you are trying to create the TextView from a different layout.xml file than the table is in? If thats the case you cannot just use findViewByID, as the layout in which you are looking for R.id.result_box, has never been initiated (usually by setContentView in the onCreate).
    Instead, when you want to create a new view from some kin of layout.xml, you should use the LayoutInflater, which will give you a View object based on whatever layout.xml file you want. Here is an example from one of my projects:

    Code (Text):
    1.  
    2. LayoutInflater inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    3. View v  = inflater.inflate(R.layout.field, null);
    4. v.setLayoutParams(new GridView.LayoutParams(*someWidth*, *someHeight*);
    5.  
    This gets an inflater from the system, creates a View from the field.xml with no direct parent and then sets its layout params to fit inside a GridView (replacing *someXxxxx* with desired numbers, or e.g. LayoutParams.WRAP_CONTENT).
     

Share This Page

Loading...