• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps addView something from xml file

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:
//resultBox = (TextView) findViewById(R.id.result_box);
        resultBox = new TextView(this);
        resultBox.setText("Hello World!");
        tr = new TableRow(this);
        tr.addView(resultBox);
        buttonTable.addView(tr,300,40);
        
        
        for( int i = 1; i <= 3; i++ )
        {
            tr = new TableRow(this);
            for( int c = 1; c <= 3; c++)
            {
                btn = new Button(this);
                btn.setText("" + (c*i));
                btn.setTextSize(10);
                btn.setTextColor(Color.rgb(255, 50, 0));
                btn.setOnClickListener(this);
                tr.addView(btn, btnSize, btnSize);
            }
            buttonTable.addView(tr);
        }
        super.setContentView(buttonTable);

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 ;)
 
Have logcat open when trying out the app on the emulator, and post the exception thrown there.
 
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:
LayoutInflater inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v  = inflater.inflate(R.layout.field, null);
v.setLayoutParams(new GridView.LayoutParams(*someWidth*, *someHeight*);

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).
 
Back
Top Bottom