megha
megha

Reputation: 301

dynamic layout changing with priority

I have to create a layout with 5 gridviews(GV1,GV2,GV3,GV4,GV5). gridviews has priority 1,2,3,4,5 respectively. Based on the size of arraylist passed to adapter, the weight of gridview is calculated

weight = size * priority

Now weight will decide the amount of area that will be occupied by that particular gridview in layout. I have attached the code below. but it doesnt work.

   RelativeLayout rl =new RelativeLayout(this);
        RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT
                ,RelativeLayout.LayoutParams.MATCH_PARENT);

    GridView burger_GV = new GridView(this);
    burger_GV.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, 
            LayoutParams.WRAP_CONTENT, (burgerAL.size()*BURGER_PRIORITY)));
    burger_GV.setBackgroundColor(Color.GRAY);

    GridView pizza_GV = new GridView(this);
    pizza_GV.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, 
            LayoutParams.WRAP_CONTENT, (pizzaAL.size()*PIZZA_PRIORITY)));
    pizza_GV.setBackgroundColor(Color.RED);

    GridView sides_GV = new GridView(this);
    sides_GV.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, 
            LayoutParams.WRAP_CONTENT, (sidesAL.size()*SIDES_PRIORITY)));
    sides_GV.setBackgroundColor(Color.YELLOW);


    GridView cookies_GV = new GridView(this);
    cookies_GV.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, 
            LayoutParams.WRAP_CONTENT, (cookiesAL.size()*COOKIES_PRIORITY)));


    GridView beverages_GV = new GridView(this);
    beverages_GV.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, 
            LayoutParams.WRAP_CONTENT, (beveragesAL.size()*BEVERAGES_PRIORITY)));


    burger_GV.setAdapter(new BurgerAdapter(burgerAL));
    pizza_GV.setAdapter(new BurgerAdapter(pizzaAL));
    sides_GV.setAdapter(new BurgerAdapter(sidesAL));
    cookies_GV.setAdapter(new BurgerAdapter(cookiesAL));
    beverages_GV.setAdapter(new BurgerAdapter(beveragesAL));

    rl.addView(burger_GV);
    rl.addView(pizza_GV);
    rl.addView(sides_GV);
    rl.addView(cookies_GV);
    rl.addView(beverages_GV);

    setContentView(rl,param);

Can anyone suggest me the solution or any ideas. Thanks in advance.

Upvotes: 0

Views: 166

Answers (1)

AlonsoFloo
AlonsoFloo

Reputation: 523

Try this GridView.LayoutParams for all your GridView

GV.setLayoutParams(new GridView.LayoutParams(0, 
            0, (burgerAL.size()*COOKIES_PRIORITY)));

It doesn't work because you are using WRAP_CONTENT as the height and width. The weight is used to distribute the remaining empty space or take away space when the total sum is larger than the LinearLayout. Set your widths to 0dip instead and it will work. Also RelativeLayout does not pay attention to android:layout_weight. (That's a property of LinearLayout.LayoutParams, but not of RelativeLayout.LayoutParams)

Upvotes: 1

Related Questions