user1079425
user1079425

Reputation:

Create a button programmatically onCreate

I have implemented ZBar and now I do want to create programmatically a Button on the Oncreate method of an activity which is a Barcode scanner and doesn't have any associated layout.. so I'm using this code:

LinearLayout layout = new LinearLayout(this); 
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

Button button = new Button(this);
button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
button.setText("Back");
layout.addView(button);
setContentView(layout);

and nothing's being displayed except the camera screen.

Do I have do add something else?

Barcode reading code:

public class ZBarScannerActivity extends Activity implements Camera.PreviewCallback, ZBarConstants {

private static final String TAG = "ZBarScannerActivity";
private CameraPreview mPreview;
private Camera mCamera;
private ImageScanner mScanner;
private Handler mAutoFocusHandler;
private boolean mPreviewing = true;

private ProgressDialog progress;
public static String MsgErr = null;
public static String BARCODE;

static {
    System.loadLibrary("iconv");
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout layout = new LinearLayout(this); 
    layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

    Button button = new Button(this);
    button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
    button.setText("Back");
    layout.addView(button);
    setContentView(layout);

    if(!isCameraAvailable()) {
        // Cancel request if there is no rear-facing camera.
        cancelRequest();
        return;
    }

    mAutoFocusHandler = new Handler();

    // Create and configure the ImageScanner;
    setupScanner();

    // Create a RelativeLayout container that will hold a SurfaceView,
    // and set it as the content of our activity.
    mPreview = new CameraPreview(this, this, autoFocusCB);
    setContentView(mPreview);
}

Upvotes: 0

Views: 1660

Answers (2)

Praveen
Praveen

Reputation: 916

LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);

//set the properties for button
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
LayoutParams.WRAP_CONTENT));
btnTag.setText("Button");
btnTag.setId(some_random_id);

//add button to the layout
layout.addView(btnTag);

Upvotes: 0

julien dumortier
julien dumortier

Reputation: 1303

you call 2 times setContentView, which has the effect of replacing the layout.

make these changes in the order

replace this line:

setContentView(layout);

by:

layout.setOrientation(LinearLayout.VERTICAL);

and replace:

setContentView(mPreview);

by:

layout.addView(mPreview);
setContentView(layout);

Now you should see the two elements

Upvotes: 1

Related Questions