views:

1372

answers:

2

Does anyone know how to perform or have a good reference for doing an activity layout at runtime in android?

Here is the code for my activity. I'm sure I'm just neglecting to do something here:

package com.isi.sa;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SimpleAssessmentTest extends Activity {
  LinearLayout layout;
  TextView question;

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    layout = new LinearLayout(this);
    question = new TextView(this);

    layout.setLayoutParams(new ViewGroup.LayoutParams(-1,-1));
    layout.setBackgroundColor(R.color.blue);

    question.setLayoutParams(new ViewGroup.LayoutParams(-1,-2));
    question.setTextColor(R.color.green);
    question.setTextSize(1,14);

    question.setText("This is question1");
    layout.addView(question);

    setContentView(layout);
  }
}

As you can see I'm just trying to add a linear layout with a single text view (just for testing purposes) however, when the activity starts I just get a black screen with a title bar of my app name.

Thanks

+4  A: 

You forgot to set your contentView. You should add

setContentView(layout);

At the end of the onCreate method

Gab Royer
@Gab Thanks for the reply. So I edited my code above and in my application per your answer but I'm still getting the same results as before, a black screen with a title bar.
Ryan
@Ryan: What about setting blue background to layout and green to the question TextView? You have not indicated the size.
Viet
@Viet: I've edited my question above and my code with your suggestions however I'm still getting the same result of a black screen with a title bar.
Ryan
+1  A: 

You can check out this URL: http://www.linux-mag.com/cache/7705/1.html . It has both library widgets and custom widgets.

EDIT:

setBackgroundColor requires input in proper ARGB format: 0xAARRGGBB. Each AA, RR, GG and BB range from 00 (minimum) to ff (maximum).

The bare minimum example goes here and it works flawlessly. Here are the screenshot and code (modified a bit):

http://picturepush.com/public/3313522 (old)

package us.simpleit;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SimpleGUI extends Activity {
    TextView tv;
    EditText et;
    LinearLayout ll;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //LinearLayout ll = new LinearLayout(this);
        ll = new LinearLayout(this);
        ll.setOrientation(android.widget.LinearLayout.VERTICAL);
        ll.setLayoutParams(new ViewGroup.LayoutParams(-1,-1));
        // ARGB: Opaque Red
        ll.setBackgroundColor(0x88ff0000);

        tv = new TextView(this);
        tv.setLayoutParams(new ViewGroup.LayoutParams(-1,-2));
        tv.setText("sample text goes here");
        // ARGB: Opaque Green
        tv.setBackgroundColor(0x5500ff00);
        ll.addView(tv);

        et = new EditText(this);
        et.setLayoutParams(new ViewGroup.LayoutParams(-1,-2));
        et.setText("edit me please");
        // ARGB: Solid Blue
        et.setBackgroundColor(0xff0000ff);
        ll.addView(et);

        Button btn = new Button(this);
        btn.setText("Go!");
        btn.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                tv.setText(et.getText().toString());
            }
        });

        ll.addView(btn);
        setContentView(ll);

        //setContentView(R.layout.main);
    }
}
Viet
@Viet: Very informative post! Thanks! You're right, it does work flawlessly however I'm not seeing what's different fundamentally between what you've posted above and my code? Help me out in seeing a difference if you can please!
Ryan
@Viet: Ok, if I just leave it very bare bones like in your example, it works, I get white text on a black linear layout. However, if like in my code I try to set attributes of the LinearLayout or the TextView such as background color on the layout or text color on the textview, they don't seem to take. The background of the layout always appears black no mater what color I pass into layout.setBackgroundColor() and when I try to setTextColor() on the text view it either always defaults to black (which I can't see because the bg of the layout is black) or the text just doesn't display.
Ryan
@Ryan: The background you were setting was not proper ARGB format :) Now check the updated code :)
Viet
@Viet: Thanks, I just found another question here that fixed this problem (http://stackoverflow.com/questions/1466788/android-textview-setting-the-background-color-dynamically-doesnt-work). Why can't I just use the values from my colors.xml resource file (R.color.xxxx)?
Ryan
Well, you need to make sure that those colors you defined follow the ARGB format.
Viet
@Viet: Thanks for all the info. I wish I could mark you both down as having answered my question but Gab really answered my original question of why my activity wasn't loading.
Ryan
Darn, how come I got a vote down?
Viet
Then you should separate the new questions with new posts, otherwise later helpers won't get a credit.
Viet