forum

Home / DeveloperSection / Forums / Set Layout gravity for buttons programmatically

Set Layout gravity for buttons programmatically

Anonymous User 1949 18-Jan-2016
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)

Updated on 27-Jun-2023
I am a content writter !

Can you answer this question?


Answer

2 Answers

Liked By