[Android] Beginner Tutorials – Lesson 18. Changing layoutParams in a running application

Translated by Taras Leskiv (http://android-by-example.blogspot.com/)

We know how to create screen components and configure their location using LayoutParams. In this lesson we will find out how to change layout-parameters for already existing screen components.

We will change weight of the components. We will draw a SeekBar and two buttons. We will configure the space occupied by the buttons using weight parameter.

Let’s create a project:

Project name: P0181_DynamicLayout3
Build Target: Android 2.3.3
Application name: DynamicLayout3
Package name: ru.startandroid.develop.dynamiclayout3
Create Activity: MainActivity

Open main.xml and create the following screen:

We are using a SeekBar component. It looks like a scrollbar and allows to specify a value from the range of values. This component has max and progress components. Max – the value which SeekBar returns when it is scrolled to maximum. Progress – is the current value of the bar. We will make maximum value = 100 and the current value will be on the half – 50;

Buttons have width by their content and their weight is both 1. They share the space of LinearLayout in which they are stretched equally.

The only thing  left is to write the code to make everything work. Open MainActivity.java, describe and find components and get access to their LayoutParams

We use getLayoutParams method to get LayoutParams of the component. This method returns basic ViewGroup.LayoutParams, but we need LinearLayout.LayoutParams, that’s why we do the casting. As a result – lParams1 and lParams2 are now LayoutParams for btn1 and btn2 components. That is when working with lParams1, for example, we influence btn1. We will use this now.

We will need a listener for our SeekBar, which will react to changes. We will make Activity do this. To do so we need to add implements OnSeekBarChangeListener to the class declaration:

We also have to add listener methods which Activity must now implement:

Listener contains three methods. It is clear from the name that:

onStartTrackingTouch triggers when we start dragging the bar
onProgressChanged triggers every time when the value is changed
onStopTrackingTouch triggers when we release the bar

We will be implementing onProgressChanged method so the changes will be visible when the bar is dragged.

leftValue variable – current value of the SeekBar, that is to the left of the bar

rightValue variable – value to the right, that is maximum value minus the current value.

We use these values as weight correspondingly. The more bar is to the left, the smaller leftValue and the bigger rightValue is, that means smaller btn1 width and greater btn2 width. And vice-versa.


Also to make it more obvious we will write variable values into the text of buttons.

And don’t forget that we have to assign the listener (Activity) to View-components whose events we need to process:

(Note: I implement a new way of presenting the code. Underlined code is the new one, code in common font is already existing code. You must find the existing code and add new one to get this fragment.)

Save everything and run the application. We change button sizes by dragging the bar:


I guess it looks tellingly ) And there are only a few lines of code.

There is a small nuance. If you just write the code lParams1.weight = 1, the component will not change. We need to add the code: btn1.requestLayout(). In this case button will read Layout and redraw. This method is already invoked in setText, that’s why we do not invoke it explicitly.

Now we know quite a lot, and in the following lessons we will try to write first sensible application – a calculator.

Complete lesson code:




Share this Post :

No comments yet.

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *