I am totally new in android.So may be my question will be like very funny for someone but still i should know the answer of my question-
TextView tv;
tv = (TextView) findViewById(R.id.anyname);
This is the code that i write in java for idtentify the resource id from xml file.As i know (TextView) is a object of View class and findViewById() is a method.Here my confusion is arising.
When we invoke any method with the object then we use dot operator(.)-
obj1`.methodName()`
But for the first case there is no dot operator between object and method.So my question is why?
JavaExperts i really need help and suggestion regrading my confusion.
But for the first case there is no dot operator between object and method.So my question is why?
That is what you called down-casting a View to TextView.
findViewById will return a View which textView inherits from, what it is doing is that it is down-casting the View to a textView that inherits from it.
Also (TextView) is not an object it is a class.
Related
What is the difference? Aren't they actually the same thing?
I'm getting "Cannot resolve method 'getText' in 'View'"
string = findViewById(R.id.signup_email).getText().toString();
But it's working perfectly.
textView = findViewById(R.id.signup_email);
string = textView.getText().toString();
First of all findViewById(R.id.signup_email) returns the View object, so when you write this statement findViewById(R.id.signup_email).getText() here getText() applies on view object (apparently View class does not contain this method).
But when you separate in two lines, here textView = findViewById(R.id.signup_email); the View object will be type cast to TextView or EditText (which you defined) object. so from here you will get this method.
If you want to keep in single line you can use
string = ((TextView) findViewById(R.id.signup_email)).getText().toString();
you can do something like this
((TextView)findViewById(R.id.signup_email)).getText().toString();
if you do just findViewById you will get View as the returned Object which is the parent of TextView but have a limited method to work with. So if we know what type of our view is we should cast the Object to the correct view intended So we can perform a variety of operation specific to that view.
In this case, it's TextView that is the reason we cast it to it and then we are able to use the getText() method of TextView Class which was not available in the View class hence you were getting compilation error for the usage of that method.
findViewById() returns a View. So the view does not heve getText() method. So if you modify the first option as follows:
string = ((TextView)findViewById(R.id.signup_email)).getText().toString();
With this, it will cast view to TextView and than you can call get text method.
Since we don't have to cast anymore, I expected findViewById to return the correct type, but it doesn't seem to do that. I'm obviously making a very simple mistake here, can you point it out?
I have a TextView's ID (since I created it dynamically) and want to change the text size of that item, this snippet works fine:
TextView tmpView = findViewById(chain.getIngredientNameId());
tmpView.setTextSize(8);
But this one doesn't:
findViewById(chain.getIngredientNameId()).setTextSize(8);
So I assume I have to case it to TextView but none of my attempts seems to work (using () or <>), what obvious thing am I missing?
You should cast it if do not save to variable.
TextView tmpView = findViewById(chain.getIngredientNameId()); //TextVeiw
((TextView) findViewById(chain.getIngredientNameId())).setTextSize(8); // TextVeiw
findViewById(chain.getIngredientNameId()); // View
EditText text1;
text1 = (EditText) findViewById(R.id.editText1);
text1.getText().toString();
Hi im new to android programming an need a little help. :) I just want to clarify if text1 is an object? Because it can call a method. But if text1 is an object how come that there is no "new" keyword. Thanks in advance for any response. :)
It is not necessary that all variables do initialization with new keyword.
Like if you write
String s = "";
Your String has been initialized without new keyword.
Same like this EditText is provided initialization in findViewById(). Here findViewById returned Edittext instance.
I suggest you complete Java Tutorial before continue work on Android. Because Android is based on Java language.
EditText is derived from the Super class View. Here findViewById method is returning an an object of the View class. You are explicitly typecasting it to EditText and assigning it to text1. So new is not required. It is being managed in findViewById method. Alternatively you can do this as:
EditText text1;
text1 = new EditText(An instance of Context); //Create an object of Edittext class
Now do whatever with this object text1.
It's me 2 years after, Now I want to answer your question bud. I know you just started and had a dream of creating apps that will be used by others, and guess what? You already achieved that and you can now also create apps not just for android but for IOS since you're using flutter now, you also have your own account on google play store now. Listen Bud, on the first line you declare what type is the text1. on the second line that's where you declare the text1 as an object now, then on the third line you are then using that object's capabilities like getting text. You've learned so much in this journey, and still learning not just in programming but in life.
Im using Android Studio, trying set text to Text View but every time i do this
TextView test = (TextView) findViewById(R.id.testView);
test.setText("fds");
on Android Studio i see Cannot resolve symbol 'setText'.
Can someone tell me what im doing wrong?
setText() will accept String which you have passed, so there is no issue in that.
One possible reason for this might be you have not written your code inside onCreate() or onCreateView().
If it is Activity you need to use these lines after setContentView() in onCreate().
If it is Fragment you need to use these lines after inflating your view in onCreateView().
Second reason, you might be having one more test variable of different type like String or something else
I will start by saying this, while I have some Java training, is my first foray into development for Android.
What I have done is created a custom ImageButton called MapCell that is basically an ImageButton that holds a few extra pieces of information, and it compiles fine.
My problem comes when I want to procedurally create a MapCell in the relevant Activity and add it to my TableLayout which is defined in the xml and has the id 'mapTable'. The relevant bit looks like this:
Random randy = new Random();
MapCell n = new MapCell(randy.nextInt(4), this); //the random number is part of my extra info
findViewById(R.id.mapTable).addView((View)n, 20, 20); //add cell to display
I get one error out of that:
The method addView(View, int, int) is undefined for the type View
Which to me sounds like utter nonsense. I put that View cast in there as desperation after I got this same error with n sitting by itself and nothing changed (Obviously my MapCell is already a View since it extends ImageButton).
I hope a new pair of eyes can tell me what this is about, since I've checked for similar problems and I didn't find any quite like this. Let me now if you need to see more code.
The method findViewById returns a View and the View class doesn't have the method addView(this method is implemented in the ViewGroup and its subclasses). Instead you should write:
((TableLayout)findViewById(R.id.mapTable)).addView(n, 20, 20);
I've cast the return of the findViewById method in a class that actually has the addView method.
You got this problem because method findViewById(R.id.mapTable) returns View object.
In android you can't add one View to another.
You can use addView function with ViewGroup, and all LinearLayout (etc.) objects.