tpei
tpei

Reputation: 681

Programatically adding margin to Android Button

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!

Upvotes: 0

Views: 143

Answers (2)

Priya
Priya

Reputation: 489

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);

Upvotes: 0

Pascal
Pascal

Reputation: 16611

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);

Upvotes: 1

Related Questions