---
title: "Set Layout gravity for buttons programmatically"  
description: "Set Layout gravity for buttons programmatically"  
author: "Anonymous User"  
published: 2016-01-18  
updated: 2023-06-27  
canonical: https://www.mindstick.com/forum/33888/set-layout-gravity-for-buttons-programmatically  
category: "android"  
tags: ["android", "android layout"]  
reading_time: 2 minutes  

---

# Set Layout gravity for buttons programmatically

How would I set layout_gravity programatically for buttons?\
I am implementing it , but its simply throws me a NullPointer [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling):

```
 Button MyButton = new Button(this);    LinearLayout.LayoutParams  lllp=(LinearLayout.LayoutParams)MyButton.getLayoutParams();    lllp.gravity=Gravity.RIGHT;    MyButton.setLayoutParams(lllp);     MyLinearLayout.addView(MyButton)
```

## Replies

### Reply by Aryan Kumar

Sure, here is how to set layout gravity for buttons programmatically:

Code snippet

```plaintext
import android.content.Context;
import android.view.Gravity;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

        // Get the button from the layout.
        Button button = findViewById(R.id.button);

        // Set the gravity of the button.
        int gravity = Gravity.TOP | Gravity.LEFT;
        button.setGravity(gravity);
    }
}
```

In this code, we first get the button from the layout using the `findViewById()` method. Then, we set the gravity of the button using the `setGravity()` method. The `setGravity()` method takes two arguments: the first argument is the horizontal gravity, and the second argument is the vertical gravity. In this case, we are setting the horizontal gravity to LEFT and the vertical gravity to TOP.

The `Gravity` class defines a number of constants that you can use to specify the gravity of a view. For example, the `Gravity.TOP` constant specifies that the view should be placed at the top of its parent. The `Gravity.LEFT` constant specifies that the view should be placed at the left of its parent.

You can also combine the horizontal and vertical gravity constants using the bitwise OR operator (`|`). For example, the following code sets the gravity of the button to the top-left corner:

Code snippet

```plaintext
int gravity = Gravity.TOP | Gravity.LEFT;
button.setGravity(gravity);
```

### Reply by Anonymous User

```
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);params.weight = 1.0f;params.gravity = Gravity.TOP;button.setLayoutParams(params);
```

\
For more check Gravity


---

Original Source: https://www.mindstick.com/forum/33888/set-layout-gravity-for-buttons-programmatically

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
