What is the best way to center (in the screen) a image button programmatically?

user236520

I am trying to generate a button programatically in the center of the current view (which is the whole screen in this case). I get the screen height and width then find the center of the screen. Then I try to use the LayoutParams to set the top and left margins, taking into account the button size - but the result is not centered. What am I doing wrong?

public class StartActivity extends Activity {

private int cScreenWidth;
private int cScreenHeight;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);

    RelativeLayout ll = (RelativeLayout)findViewById(R.id.startLayout);

    Display display = getWindowManager().getDefaultDisplay(); 
    cScreenWidth = display.getWidth();  // deprecated but needed for API 10
    cScreenHeight = display.getHeight();  // deprecated but needed for API 10

    ImageButton myButton = new ImageButton(this);
    myButton.setId(100);
    myButton.setBackgroundResource(R.drawable.ic_launcher);

    int size = Math.min(cScreenWidth / 3, cScreenHeight / 3);
    LayoutParams lp = new LayoutParams( size, size );
    lp.leftMargin = (cScreenWidth / 2) - (size / 2);
    lp.topMargin = (cScreenHeight / 2) - (size / 2);
    ll.addView(myButton, lp);
}
Shobhit Puri

Try the following:

RelativeLayout.LayoutParams layoutParams = 
    (RelativeLayout.LayoutParams)myButton.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
myButton.setLayoutParams(layoutParams);

This way you don't need to explicitly define the margins.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What's the best way to add a full screen background image in React Native

From Java

What's the best way to detect a 'touch screen' device using JavaScript?

From Dev

What's the best way to set which image loads as a splash screen in Android?

From Dev

What's the best way to fix this radio button HTML?

From Dev

What is the best way to recreate an AlertDialog when the screen is rotated?

From Dev

Ribbon Button Image Programmatically

From Dev

Programmatically setting button image

From Dev

What is the best way to render a WSQ image in a QLabel

From Dev

Put ProgressBar in CENTER of the screen programmatically

From Dev

What is the best way to use multiple layouts with selective back button?

From Dev

Image for a submit button - which way is best?

From Dev

What is the right way to put a Drive image into a Sheets cell programmatically?

From Dev

What's the best way to add a bunch of Button to a ScrollPane?

From Dev

What is the best way to move an object on the screen?

From Dev

What is the best way to properly center a logo on an ActionBar?

From Dev

What is best way to generate a list of button/links dynamically

From Dev

What is the best way of storing image in mysql db

From Dev

What is the best image size of floating action button

From Dev

What is the best way to center list items using bootstrap?

From Dev

What is the best way to fix a stuck pixel on an LCD screen?

From Dev

What layout will be the best way to design this android screen

From Dev

What is the best way to store the image in the memory of app?

From Dev

center image inside button

From Dev

What's the best way to create items programmatically inside a Service?

From Dev

What's the best way to have a button come visible on a specific screen size in MVC?

From Dev

What's the best way to make multiple draggable views programmatically?

From Dev

What's the best way to place multiple images onto the screen simultaneously?

From Dev

what is the best way of image storage on hyperledger composer?

From Dev

Position button at the center of the screen

Related Related

  1. 1

    What's the best way to add a full screen background image in React Native

  2. 2

    What's the best way to detect a 'touch screen' device using JavaScript?

  3. 3

    What's the best way to set which image loads as a splash screen in Android?

  4. 4

    What's the best way to fix this radio button HTML?

  5. 5

    What is the best way to recreate an AlertDialog when the screen is rotated?

  6. 6

    Ribbon Button Image Programmatically

  7. 7

    Programmatically setting button image

  8. 8

    What is the best way to render a WSQ image in a QLabel

  9. 9

    Put ProgressBar in CENTER of the screen programmatically

  10. 10

    What is the best way to use multiple layouts with selective back button?

  11. 11

    Image for a submit button - which way is best?

  12. 12

    What is the right way to put a Drive image into a Sheets cell programmatically?

  13. 13

    What's the best way to add a bunch of Button to a ScrollPane?

  14. 14

    What is the best way to move an object on the screen?

  15. 15

    What is the best way to properly center a logo on an ActionBar?

  16. 16

    What is best way to generate a list of button/links dynamically

  17. 17

    What is the best way of storing image in mysql db

  18. 18

    What is the best image size of floating action button

  19. 19

    What is the best way to center list items using bootstrap?

  20. 20

    What is the best way to fix a stuck pixel on an LCD screen?

  21. 21

    What layout will be the best way to design this android screen

  22. 22

    What is the best way to store the image in the memory of app?

  23. 23

    center image inside button

  24. 24

    What's the best way to create items programmatically inside a Service?

  25. 25

    What's the best way to have a button come visible on a specific screen size in MVC?

  26. 26

    What's the best way to make multiple draggable views programmatically?

  27. 27

    What's the best way to place multiple images onto the screen simultaneously?

  28. 28

    what is the best way of image storage on hyperledger composer?

  29. 29

    Position button at the center of the screen

HotTag

Archive