SET LAYOUT GRAVITY FOR BUTTONS PROGRAMMATICALLY

Maria Susan

Total Post:29

Points:107
Posted by  Maria Susan
 593  View(s)
Ratings:
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)
  1. Mayank Tripathi

    Post:397

    Points:3117
    Re: Set Layout gravity for buttons programmatically

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!