• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problem with setLayoutParams and weight

I'm having problem changing the weight of a view by code.

I have two views, each one with weight of 1, when I change the weight of one to 2, then the opposite occurs, the view that have weight 1 occupies 66% of the screen and the view that have weight 2 occupies 33% of the screen.

The code:

View view = findViewById(R.id.view1);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)view.getLayoutParams();

params.weight += 1f;

view.setLayoutParams(params);
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones