0

I got these two Buttons I'd like to add to may layout dynamically.

Button settingsButton = new Button(this);
settingsButton.setText("Settings");
View view = findViewById(R.id.content_frame);
int width = view.getWidth() / 5;
int height = view.getHeight() / 5;
settingsButton.setLayoutParams(new LinearLayout.LayoutParams(Math.max(width, height), Math.min(width, height)));
((ViewGroup) view).addView(settingsButton);

Button entryButton = new Button(this);
entryButton.setText("add Entry");
entryButton.setLayoutParams(new LinearLayout.LayoutParams(Math.max(width, height), Math.min(width, height)));

((ViewGroup) view).addView(entryButton);

Now to make the buttons not appear on top of each other I tried giving the second button a margin like:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Math.max(width, height), Math.min(width, height));
params.setMargins(Math.max(width, height), 0, 0, 0);

and then either

((ViewGroup) view).addView(entryButton, params);

or

entryButton.setLayoutParams(params);
((ViewGroup) view).addView(entryButton);

Which both didn't change anything. Any ideas? Thanks!

tpei
  • 671
  • 9
  • 26

2 Answers2

1

You probably have to use

ViewGroup.MarginLayoutParams.setMargins

Doing something like this will do the trick:

LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
     LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

layoutParams.setMargins(10, 10, 10, 10);

Button button = new Button(getActivity());
button.setText("My Button");
linearLayout.addView(button, layoutParams);
Pascal
  • 15,257
  • 2
  • 52
  • 65
  • Maybe something different in your code. Check this http://stackoverflow.com/questions/2481455/set-margins-in-a-linearlayout-programmatically and give a try: use the same approach and is working (according to SO user votes) – Pascal Feb 10 '14 at 09:24
  • 1
    ok, since it was used inside a FrameLayout I had to change your code accordingly, basically changing LinearLayout to FrameLayout everywhere. Thanks! – tpei Feb 10 '14 at 16:09
0

I tried with LinearLayout. It works fine.

Your Layout file will be like

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/rlParent">


</LinearLayout>
</RelativeLayout>

and inside oncreate, we need to give like

     Button settingsButton = new Button(this);
settingsButton.setText("Settings");
settingsButton.setTextColor(Color.BLACK);
settingsButton.setHeight(LayoutParams.WRAP_CONTENT);
settingsButton.setWidth(LayoutParams.WRAP_CONTENT);
View view = findViewById(R.id.rlParent);        
          ((ViewGroup) view).addView(settingsButton,0);
          Button entryButton = new Button(this);
entryButton.setText("add Entry");
entryButton.setTextColor(Color.BLACK);
entryButton.setHeight(LayoutParams.WRAP_CONTENT);
entryButton.setWidth(LayoutParams.WRAP_CONTENT);
((ViewGroup) view).addView(entryButton,1);
Priya
  • 489
  • 1
  • 10
  • 20