-4

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

rov_0
  • 7
  • 6

2 Answers2

0

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.

Samir Bhatt
  • 3,041
  • 2
  • 25
  • 39
  • Is timerSeekBar a variable or an object? – rov_0 Oct 11 '17 at 12:28
  • timerSeekBar.setMax(600); and timerSeekBar.setProgress(30); - function call made by the variable.Currently i know only objects can only make function call.Here findViewById is a method and as mentioned getting assigned to the variable after being casted to SeekBar type.So typically timerSeekBar is a method and is calling other methods,please explain – rov_0 Oct 12 '17 at 06:10
  • timeseekbar is instance of Seekbar. – Samir Bhatt Oct 12 '17 at 06:22
  • setMax and setProgress are methods of Seekbar class. – Samir Bhatt Oct 12 '17 at 06:22
  • Seekbar extends AbsSeekBar which have synchronized method setMax(). – Samir Bhatt Oct 12 '17 at 06:26
  • Check my updated answer. Hope it will help you to understand. – Samir Bhatt Oct 12 '17 at 07:13
  • instance therefore its an object?Please detail on this – rov_0 Oct 12 '17 at 08:43
  • Yes, Instance means object of that class. So you can access it's method using that object. Java is object orient programming language. – Samir Bhatt Oct 12 '17 at 08:47
  • but u said its a variable,what is it an object or a variable? – rov_0 Oct 12 '17 at 10:07
  • That's my mistake. It's an onject. – Samir Bhatt Oct 12 '17 at 10:10
  • Okay thanks,what is this way of object creation called? – rov_0 Oct 12 '17 at 10:34
  • This is called intitalization. you can create object of class on different way. this link may help you. https://stackoverflow.com/a/5104630/3364266 – Samir Bhatt Oct 12 '17 at 10:40
  • Was checking the exact same link but could'nt identify what type of initialization it is? – rov_0 Oct 12 '17 at 11:17
  • This is simple giving reference of that view. – Samir Bhatt Oct 12 '17 at 11:25
  • I got that,what is this type of object creation ?Its an object right? – rov_0 Oct 12 '17 at 11:35
  • You can create object of class on different way. – Samir Bhatt Oct 12 '17 at 11:36
  • Still to identify what way it is getting created in this case.Any clues would be great – rov_0 Oct 12 '17 at 11:38
  • This is concept of android. R.id.timerSeekBar is fetching id of view that is associated with view in xml. On Compilatation time, R.java file will generated. – Samir Bhatt Oct 12 '17 at 12:26
  • "findViewById is a method of the View and Activity classes.This method will take a resource id usually in the form of R.id.mView and will return an object that is a reference to that View.Note that the referenced object needs to be cast to the correct type of View before you can start interacting with it" - So method is creating an object for me and this is one of the ways of creating object as u mentioned.I believe this is more like it. – rov_0 Oct 13 '17 at 06:07
  • Is anything more you want to know about? I hope my explanation helps you for understanding. – Samir Bhatt Oct 13 '17 at 06:09
  • Yes i got it :) – rov_0 Oct 13 '17 at 11:46
0

SeekBar is a widget.. It is defined in a file SeekBar.java ..essentially it is a View in android. Seekbar extends the base class View.java eventually. findViewById() method is used to find a view in android with a particular id set for the view using the setId() method. We use SeekBar timerSeekBar = (SeekBar)findViewById(R.id.timerSeekBar); so as to tell the system that we are looking for a view using findViewById() method and then casting the base View.class type object to a subclass SeekBar.class type object. (SeekBar) is used to tell the system that this view is an instance of SeekBar.

saurabh169
  • 499
  • 1
  • 4
  • 12
  • Thanks,timerSeekBar is an object right? Widget as in like a package (pardon a noob)? – rov_0 Oct 11 '17 at 10:51
  • yes... see when you are basically writing `TimerSeekBar mySeekBar` then you are creating the object of the class TimerSeekBar with name mySeekBar.. then you can access the methods of the class using objectName.methodName()... – saurabh169 Oct 11 '17 at 10:59
  • Thanks, In this TextView timerTextView=(TextView) findViewById(R.id.timerTextView); TextView's an widget too? is Widget like a package,a collection of code like a set of classes? – rov_0 Oct 11 '17 at 11:18
  • it is a collection of code.. You will find code of TextView.java file if you hold the `ctrl` key and click on TextView.. You will understand. PS- If my explanation has helped you..please mark my answer as accepted. – saurabh169 Oct 12 '17 at 06:24