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
You forgot to set your contentView. You should add
setContentView(layout);
At the end of the onCreate
method
You can check out this URL: http://www.linux-mag.com/cache/7705/1.html . It has both library widgets and custom widgets.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With