Why should SeekBar be on 'onCreate' method?

android seekbar display value
how to set seekbar value in android
android seekbar with indicator
custom seekbar android
android seekbar example programmatically
seekbar discrete android
android seekbar style
how to set min and max value of seekbar in android

I wrote the code for a timer like below, but if I wrote it in a separate method, not in pre-created method like onCreate(), it doesn't work as I expected. But if I put the body part of the method in 'onCreate()', it works just as fine.

Are there some rules or differences between writing a method in 'onCreate()' method or independently?

public void seekBarController(View view){

    SeekBar timerSeekBar = (SeekBar) findViewById(R.id.timerSeekBar);
    final TextView timerTextView = (TextView) findViewById(R.id.timerTextView);

    timerSeekBar.setMax(10*60);
    timerSeekBar.setProgress(30);

    timerSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            int minutes = (int) progress / 60;
            int seconds = (int) progress % 60;

            String minutessString = seconds < 10? "0"+ Integer.toString(minutes): Integer.toString(seconds);
            String secondsString = seconds < 10? "0"+ Integer.toString(seconds): Integer.toString(seconds);

            Log.i("this is progress", Integer.toString(progress));
            timerTextView.setText(minutessString+":"+secondsString);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

}

Seekbar, like any View needs an Activity and is part of/owned by that Activity. It will be created when the Activity is created. Thus the findViewByID method can only be used when the activity has actually been created and after the Content (the layout) has been setup (via setContentView).

It's unclear what View you pass to the method, but, the following keeps your method separate though calling it appropriately (nothing is passed to the method, it uses the instance's Activity for the findViewById) :-

public class MainActivity extends AppCompatActivity {

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

    public void seekBarController(){

        SeekBar timerSeekBar = (SeekBar) this.findViewById(R.id.timerSeekBar);
        final TextView timerTextView = (TextView) findViewById(R.id.timerTextView);

        timerSeekBar.setMax(10*60);
        timerSeekBar.setProgress(30);

        timerSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                int minutes = (int) progress / 60;
                int seconds = (int) progress % 60;

                String minutessString = seconds < 10? "0"+ Integer.toString(minutes): Integer.toString(seconds);
                String secondsString = seconds < 10? "0"+ Integer.toString(seconds): Integer.toString(seconds);

                Log.i("this is progress", Integer.toString(progress));
                timerTextView.setText(minutessString+":"+secondsString);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {    
            }

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

SeekBar, allows users to touch the thumb and drag left or right to set the current progress levels. We can use seekbar in our android device like Brightness control, volume control etc. It is one of the important user Interface element which provides the option to select the integer values within the defined range like 1 to 100.

onCreate() is automatically called "under the hood" by the Activity's class. It is automatically called when the activity is first created. See the Android documentation on Activity Lifecycles.

Without knowing much of your Activity's implementation, I would say putting that timer's code in the onCreate method or have the onCreate method call that method.

Android SeekBar with Examples, How do I use SeekBar with Media Player on Android? I would like to use a dialog with a seekbar in my application. But I don’t really know how to do it because I lack experience with android. So, when you press a button: a dialog should come up, with a seekbar and the user is able to enter a value and then press OK-button.

Activity class provides a core set of six callbacks: onCreate(), onStart(), onResume(), onPause(), onStop(), and onDestroy(). The onCreate() fires when the system first creates the activity.

SeekBar and media player in android, A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or  onProgressChanged() should be called on every progress changed, not just on first and last touch (that why you have onStartTrackingTouch() and onStopTrackingTouch() methods). Make sure that your SeekBar have more than 1 value, that is to say your MAX>=3. In your onCreate:

How to change colour of the thumb in seekbar?, In Android, SeekBar is an extension of ProgressBar that adds a draggable thumb, a user can touch the It must be an integer value like 10, 20, 100, 200 etc. To provide functionality of seekbar to control position of playback. Classes used to perform this task: MediaPlayer class. Handler class. Runnable class; Introduction to classes:

SeekBar Tutorial With Example In Android Studio, Android seekBar is a modified version of progressBar that have draggable thumb in By dragging the thumb in SeekBar, we can slide back and forth to choose a If you like GeeksforGeeks and would like to contribute, you can also write an  Select Automatically decide to let Storyline decide whether or not the seekbar will be active for the layer. Here's the logic: Seeking will be allowed if you mark Hide objects on base layer, Prevent the user from clicking on the base layer, or Pause timeline of base layer. This documentation can be found here.

SeekBar in Kotlin, I would guide you about the SeekBar control and a few things that you should understand before using the control. I would also provide an  The thumb property of the SeekBarallows us to set a drawable instance to be shown within its content area, this is perfect because we intended on showing a square image for the current frame as the thumb. For this, we create a new instance of a BitmapDrawable and set it as the thumb of our SeekBar– at this point we almost have all that we need.