0

Read this and this. Attempted to apply the post's answers, but the button remains without margins when I build and run the app. What am I doing wrong?

public class ResultsActivityA extends Activity {

    Button button, submitButton;

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

        //...
        LinearLayout activityResultsA= new LinearLayout(this);

        Button submitButton=new Button(this);

        LayoutParams dimensions= new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
        activityResultsA.setLayoutParams(dimensions);

        LayoutParams viewDimensions= new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        LayoutParams buttonDimensions= new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

        buttonDimensions.setMargins(50, 40, 0, 0);

        submitButton.setLayoutParams(buttonDimensions);

        activityResultsA.setOrientation(LinearLayout.VERTICAL);

        submitButton.setText("Home");
        submitButton.setId(5555);

        activityResultsA.addView(submitButton);

        setContentView(activityResultsA);
        addListenerOnButton();

    }

}
Community
  • 1
  • 1
Jesse Galdamez
  • 81
  • 1
  • 2
  • 11

2 Answers2

2

Margins that you're expecting will be seen in lower density devices (ldpi) or medium density devices (mdpi) but not in higher density devices (hdpi, xxhdpi, ...).

setMargins(top, left, bottom, right) accepts int values in px unit. In your case, You're actually passing 50px, 40px, and 0px.

You need to convert those values from dp to px

public static int convertDPtoPX(Context context, int dpValue) {

     DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();

     return dpValue * displayMetrics.densityDpi;

}
Glenn
  • 12,741
  • 6
  • 47
  • 48
  • I wrote this method in a class file and I called it in the layout .java file by writing "lt=cm.convertDPtoPX(this,50);", but "this" gives me an error. What should I be using instead? – Jesse Galdamez Jan 27 '15 at 03:16
  • You can put `getBaseContext()` or `getApplicationContext()` – Glenn Jan 27 '15 at 04:14
  • Thank you for your help. I used getBaseContext() but it gives me the following error: The static method convertDPtoPX(Context, int) from the type ConvertMeasure should be accessed in a static way – Jesse Galdamez Jan 27 '15 at 04:19
  • I removed static from the method and this solves the problem. – Jesse Galdamez Jan 27 '15 at 04:26
  • When I build the app, the margin has not budged an inch (that is, it has not shifted left 50 dp). – Jesse Galdamez Jan 27 '15 at 04:36
  • Probably my calculation from dp to px returns incorrect value. Try calculations from [here](http://stackoverflow.com/questions/4605527/converting-pixels-to-dp) – Glenn Jan 27 '15 at 05:37
  • Tried their methods, same result. It seems to me that no matter what values I give to "buttonDimensions.setMargins(left, top, right, bottom);", I still do not shift the margins(at all). What am I missing? I know this can be frustrating. What else can I do to center my button dynamically/programmatically? – Jesse Galdamez Jan 28 '15 at 02:49
  • Use [gravity](http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#gravity). Alignment of Children of LinearLayout will be based on that gravity – Glenn Jan 28 '15 at 05:02
  • I wrote "((LinearLayout.LayoutParams)submitButton.getLayoutParams()).gravity=Gravity.CENTER_HORIZONTAL;" after addView, but before setContentView and it centered the button. – Jesse Galdamez Jan 31 '15 at 23:14
  • Yes, that will work before calling `setContentView();` – Glenn Feb 01 '15 at 08:00
0

I wrote ((LinearLayout.LayoutParams)submitButton.getLayoutParams()).setMargins(left, top, right, bottom); after addView, but before setContentView and it set the margins.

Jesse Galdamez
  • 81
  • 1
  • 2
  • 11