0

I have a piece of code I am trying to understand , this is an Java-Android Based .

     volumeControl.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
        }

    });

In this code there an @Override method done inside the parentheses is this the best way to do this code and how this is done .

  • How the java compiler understand this constructor call.

  • There is a int variable called progress from where it gets it's value , how the function call is done.

ahmed osama
  • 1
  • 1
  • 5

2 Answers2

1

There are 2 ways to implement an interface in Android (in java). The same as you did (anonymous implementation) or you can add implements OnSeekBarChangeListener to your class signature. When you implement the interface with the class, all these methods will become your class methods. Then you can call volumeControl.setOnSeekBarChangeListener(this) because your class instance is implementing the required interface.

I don't think there are any other difference than code readability to both implementation methods.

But remember, when you want to use method variables inside anonymous implementation, they should be declared as final.

public class SeekBarTestActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seek_bar_test);

        SeekBar seekbar = (SeekBar) findViewById(R.id.seekBar);
        seekbar.setOnSeekBarChangeListener(this);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        // TODO add your implementation here
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO add your implementation here
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO add your implementation here
    }
}
0

The way it works is that the method is expected a class implementing OnSeekBarChangeListener.

Inside the volumeControl object, a reference is kept to the Listener.

Whenever the volumeControl has to notify to the listener it will use the reference, and this way the listener will be notified.

In this particular example, instead of creating a class implementing the Listener separately, it is provided an anonymous inline implementation.

It is very typical way of dealing with this in Swing or Android development. It is quite ugly and so, but it was always like that.

Fortunately Java 8 opens much better ways to deal with it (finally!!) thanks to functional programming, lambda, and in more general terms, "function pointers".

Edit (cannot write comments yet), and as an answer to a comment please see Function Pointers in Java

Community
  • 1
  • 1
Fernando
  • 396
  • 1
  • 5
  • Java is ***finally*** getting function references????? – Mat Jones Jan 11 '17 at 16:07
  • so the called store an image from the object and whenever a notification happens to listener the variable ar effected automatically without any further action , then the variable int progress is set by the system according to the action happens , – ahmed osama Jan 11 '17 at 16:24