Home > DeveloperSection > Forums > Set Layout gravity for buttons programmatically
Maria Susan

Total Post:29

Points:107
Posted on    January-18-2016 5:33 AM

 Android Android  Android Layout 
Ratings:


 1 Reply(s)
 294  View(s)
Rate this:
How would I set layout_gravity programatically for buttons?

I am implementing it , but its simply throws me a NullPointer exception:

 Button MyButton = new Button(this);
    LinearLayout.LayoutParams  lllp=(LinearLayout.LayoutParams)MyButton.getLayoutParams();
    lllp.gravity=Gravity.RIGHT;
    MyButton.setLayoutParams(lllp); 

    MyLinearLayout.addView(MyButton)


Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Supported
Posted on    January-18-2016 5:59 AM

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

Don't want to miss updates? Please click the below button!

Follow MindStick