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!

7
  • probably width and height are both zero. If I were you I would start to check it. Commented Feb 10, 2014 at 8:33
  • Is R.id.content_frame a LinearLayout ? Commented Feb 10, 2014 at 8:33
  • can u show some sample image how u want Commented Feb 10, 2014 at 8:35
  • @blackbelt width and height are not zero / null Commented Feb 10, 2014 at 8:48
  • @fiddler R.id.content_frame is a FrameLayout Commented Feb 10, 2014 at 8:49

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Maybe something different in your code. Check this stackoverflow.com/questions/2481455/… and give a try: use the same approach and is working (according to SO user votes)
ok, since it was used inside a FrameLayout I had to change your code accordingly, basically changing LinearLayout to FrameLayout everywhere. Thanks!
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.