Reputation: 465
I've googled for hours but couldn't come up with a solution... i found many similar problems and answers but none of them worked for me.
What I want to do: I have a view that I use to draw my app. Now I want to add a layout "on top" of my current canvas. The layout should contain a button and have a margin of 20 px on each side. I found a similar code snippet like the following code but just nothing happens on the screen. No button. Nothing.
This is the code that I am using:
LinearLayout bottomBar = new LinearLayout(this);
bottomBar.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20, 20, 20, 20);
Button button = new Button(this);
button.setText("text");
bottomBar.addView(button, layoutParams);
bottomBar.draw(canvas);
What am I doing wrong?
Upvotes: 1
Views: 128
Reputation: 39856
the fact that you created a Layout LinearLayout bottomBar = new LinearLayout(this);
does not mean it is attached to the screen.
also, DO NOT CALL draw on your bar. After you include it in the screen the system will call for you.
let's say for example that your XML (that was inflated with setContentView) have a FrameLayout which ID is R.id.frame, then you could do something like that:
LinearLayout bottomBar = new LinearLayout(this);
bottomBar.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20, 20, 20, 20);
Button button = new Button(this);
button.setText("text");
bottomBar.addView(button, layoutParams);
FrameLayout frame = (FrameLayout) findViewById(R.id.frame);
frame.addView(bottomBar);
you see... you have to call 'addView' in a view group that already is on the screen.
alternatively, you can call: setContentView(bottomBar);
replace whatever is on your screen by bottomBar
Upvotes: 3
Reputation: 1963
you dont provide WIDTH/HEIGHT for your LinearLayout and you dont attach it to your view.
Upvotes: 0