Search code examples
javaandroidviewandroid-seekbarfindviewbyid

What is timerSeekBar in this java code an object or variable?What is findViewById,


I checked findViewById is a method so what is timerSeekBar, an object or a variable,got told its a variable but we are calling methods with it so it should'nt it be an object.Please explain why are we writing SeekBar before findViewById.

SeekBar -Class,findViewById- Method ?,timerSeekBar- variable or object?

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

timerSeekBar.setMax(600);
timerSeekBar.setProgress(30);

another eg-

TextView answerLabel = (TextView) findViewById(R.id.button1);

Image


Solution

  • The method findViewById returns an instance of the class that is actually used to define that view in your XML file. (Seekbar) is used to cast that view with specific view.

    You have to cast that(findViewById) to the class that your variable is defined when you use it. So, This is the reason for

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

    From API 26, findViewById uses inference for its return type, so you no longer have to cast.

    Please check below reference link : https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/SeekBar.java

    https://android.googlesource.com/platform/frameworks/base.git/+/android-4.2.1_r1/core/java/android/widget/AbsSeekBar.java

    https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/ProgressBar.java

    ProgressBar is super class, AbsSeekbar is Child class of Progressbar and SeekBar is childclass of AbsSeekbar.

    setMax() is method of Seekbar and setProgress() is method of Progressbar.