Lesson 18. Change layoutParams in the working application

Lesson 18. Change layoutParams in the working application


In this lesson we:

– change layout parameters for existing screen components

We know how to create onscreen components and customize placements for them LayoutParams. In this lesson, we will learn how to change the layout parameters of existing components.

We will change the weight – weight. draw SeekBar (Regulator or slider) and two buttons. And we will regulate space is occupied by buttons using the 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 we create the following screen:



    
    
    
        
        
    

We use the SeekBar component. It is similar to a scroll bar and allows you to set any value from the range. This component has properties max and progress. Max is some value SeekBar gives when it is screwed on maximum. Progress is it current value slider. Make the maximum = 100, And the current value will be half – 50.

Buttons we have width in content and weight for both = 1. they equally share the space LinearLayout they are in.

All you have to do is write the right code to make it work. open MainActivity.java, We will describe and find the components and get access to their LayoutParams.

public class MainActivity extends Activity {

  SeekBar sbWeight;
  Button btn1;
  Button btn2;

  LinearLayout.LayoutParams lParams1;
  LinearLayout.LayoutParams lParams2;

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    sbWeight = (SeekBar) findViewById(R.id.sbWeight);

    btn1 = (Button) findViewById(R.id.btn1);
    btn2 = (Button) findViewById(R.id.btn2);

    lParams1 = (LinearLayout.LayoutParams) btn1.getLayoutParams();
    lParams2 = (LinearLayout.LayoutParams) btn2.getLayoutParams();
  }
}

We use the getLayoutParams method to get the LayoutParams component. But this method returns the basic one ViewGroup.LayoutParams, But we need it LinearLayout.LayoutParamsSo we do the conversion. As a result – lParams1 and lParams2 now there is LayoutParams for components btn1 and btn2. That is, working with, for example, lParams1, we influence btn1. Now we use it.

SeekBar will need a handler to respond to changes. This is our task for Activity. To do this, add to the class description implements OnSeekBarChangeListener:

public class MainActivity extends Activity implements OnSeekBarChangeListener {

Also, we need to add the handler methods that are now required to implement the Activity.
  @Override
  public void onProgressChanged(SeekBar seekBar, int progress,
      boolean fromUser) {

  }

  @Override
  public void onStartTrackingTouch(SeekBar seekBar) {

  }

  @Override
  public void onStopTrackingTouch(SeekBar seekBar) {

  }

The handler contains three methods. From the titles it is clear that:

– onStartTrackingTouch works when we start pulling the slider
– onProgressChanged works all the time as the value changes
– onStopTrackingTouch works when we release the slider

We will use the onProgressChanged method. This will make changes visible when you drag the slider.

  @Override
  public void onProgressChanged(SeekBar seekBar, int progress,
      boolean fromUser) {
    int leftValue = progress;
    int rightValue = seekBar.getMax() - progress;
    // настраиваем вес
    lParams1.weight = leftValue;
    lParams2.weight = rightValue;
    // в текст кнопок пишем значения переменных
    btn1.setText(String.valueOf(leftValue));
    btn2.setText(String.valueOf(rightValue));
  }

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

variable rightValue – that to the right of the slider, that is, to subtract the current value from the maximum.

Accordingly, we use these values ​​as weights. than the slider to the left, so less leftValue and more rightValue, and so less width btn1 and more width btn2. And vice versa.

Also, for the sake of clarity, the values ​​of variables will be written to the buttons.

And of course we do not forget that it is necessary to assign a handler (Activity) to the View component, the events that need to be processed:

    setContentView(R.layout.main);

    sbWeight = (SeekBar) findViewById(R.id.sbWeight);
    sbWeight.setOnSeekBarChangeListener(this);

    btn1 = (Button) findViewById(R.id.btn1);

(Please note. I am entering a new code submission technique. The underlined is the new code and the normal font is the existing code. You need to find the existing code and append a new one to get this snippet.)

We will save everything and launch the application. Drag the slider to resize the buttons:

It looks spectacular, I think) And the code – just a few lines.

There is a slight nuance. As rightly noticed in the comments, if you just write code lParams1.weight = 1, then the component will not change. Add code: btn1.requestLayout (). Then the button will read Layout and redraw. This method is already called in setText, so we do not explicitly call it here.

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

Full lesson code:

public class MainActivity extends Activity implements OnSeekBarChangeListener {

  SeekBar sbWeight;
  Button btn1;
  Button btn2;

  LinearLayout.LayoutParams lParams1;
  LinearLayout.LayoutParams lParams2;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    sbWeight = (SeekBar) findViewById(R.id.sbWeight);
    sbWeight.setOnSeekBarChangeListener(this);

    btn1 = (Button) findViewById(R.id.btn1);
    btn2 = (Button) findViewById(R.id.btn2);

    lParams1 = (LinearLayout.LayoutParams) btn1.getLayoutParams();
    lParams2 = (LinearLayout.LayoutParams) btn2.getLayoutParams();
  }

  @Override
  public void onProgressChanged(SeekBar seekBar, int progress,
      boolean fromUser) {
    int leftValue = progress;
    int rightValue = seekBar.getMax() - progress;
    // настраиваем вес
    lParams1.weight = leftValue;
    lParams2.weight = rightValue;
    // в текст кнопок пишем значения переменных
    btn1.setText(String.valueOf(leftValue));
    btn2.setText(String.valueOf(rightValue));
  }

  @Override
  public void onStartTrackingTouch(SeekBar seekBar) {
  }

  @Override
  public void onStopTrackingTouch(SeekBar seekBar) {
  }
}

In the next lesson:

– writing calculator application




Discuss in the forum [90 replies]

Leave a Comment