Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Runtime Layout Tutorial

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

like image 998
Ryan Avatar asked Apr 22 '10 19:04

Ryan


2 Answers

You forgot to set your contentView. You should add

setContentView(layout);

At the end of the onCreate method

like image 147
Gab Royer Avatar answered Nov 07 '22 20:11

Gab Royer


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);
    }
}
like image 43
Viet Avatar answered Nov 07 '22 19:11

Viet