0

I want to set layout in Java code file. I use the following code to set this layout to center

        theme = db.getthemeByID(id);
        String themePath = theme.getFilepath();
        int resid = getResources().getIdentifier(themePath, "drawable", getPackageName());
        //layout for book theme
        LinearLayout LLT = new LinearLayout(context);
        LLT.setOrientation(LinearLayout.VERTICAL);
    >>>>LLT.setLayoutParams(new LayoutParams(500, 650, Gravity.CENTER_HORIZONTAL));
        LLT.setBackgroundResource(resid);   
        // add view 
        VF.addView(LLT);

I'm still getting error on this line:

LLT.setLayoutParams(new LayoutParams(500, 650, Gravity.CENTER_HORIZONTAL));

How can I do if I want to set layout to center_horizontal

codeMagic
  • 44,549
  • 13
  • 77
  • 93
user3001046
  • 235
  • 1
  • 10
  • 28

2 Answers2

0

I think it's because the third param in that constructor is weight, not Gravity.

Try removing that (so the LayoutParams only have two arguments, height and width), and then in the next line you can do

LLT.setGravity(Gravity.CENTER_HORIZONTAL);
Embattled Swag
  • 1,479
  • 12
  • 23
0
labelTV.setGravity(Gravity.CENTER_HORIZONTAL);

use this to set layout gravity programmatically...

or this will help you..

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
params.weight = 1.0f;
params.gravity=CENTER_HORIZONTAL;

See this question to see this in more clear manner

Community
  • 1
  • 1
Kiruba Karan
  • 39
  • 1
  • 11