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.
Related
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
I have an android app that I have decided to rewrite, one of the reasons for the rewrite is because I could have 10+ TextViews with text set based on a variable in a class e.g.:
MyClass myClass = new MyClass();
myClass.myNumber = 5; // inside MyClass - public int myNumber;
LinearLayout mainLayout = (LinearLayout) view.findViewById(R.id.mainLayout);
TextView myTextView = new TextView(getActivity()); //In a fragment
myTextView.setText(String.format("myNumber currently has a value of %d", myClass.myNumber));
mainLayout.addView(myTextView);
return view;
Up until now I have been using .setOnClickListener on the buttons/views that change myNumber, to set the text of the view again when the value of myNumber changes, which then calls .invalidate() to redraw the TextView, this has worked fine, but I use this method very heavily and code is getting quite repetitive and changing one integer can affect quite a lot of views (all of which use it differently - such as different wording, or a calculation (e.g. myNumber * 2)). I guess it's because it's made an immutable string in the TextView.
I have tried to create a custom TextView that implements Observer (making MyClass extend Observable) and in the update method I can get it to invalidate itself for the refresh, but it still has the same text. I have also tried creating single element arrays, in an attempt to pass the reference not the value in the hope that when it is changed and then the view is invalidated it will pick up the new value but the text still ends up remaining the same.
How can I get a TextView that will auto update when the value of myNumber has changed? Some sort of binding?
Bindroid works perfectly for this, just a note for users, using fragments the sample application is using this from an Activity so the bind method using Activity is called, so in the fragment I was using getActivity() which caused it to not work properly, digging around in the library I found a bind method that takes a View and passed in my view which gets inflated in the fragment and it works great!!! This is super easy to integrate btw it was just me not getting it!
I have noticed a weird thing in Android. I can't use EditText functions like getText() - for example, when I type:
final EditText txtUser = (EditText) inflater.inflate(R.layout.layout_dialog, null).findViewById(R.id.txtUsername);
And when I use:
View v = inflater.inflate(R.layout.layout_dialog, null);
final EditText txtUser = (EditText) v.findViewById(R.id.txtUsername);
everything works fine. I noticed that inflater.inflate(R.layout.layout_dialog, null); returns view, so I don't understand why I can't use getText() function when I don't use a view instance.
Thank You.
You are inflating Custom dialog. So in order to use its component, you first need to initialise it as View.
If you want to use method of other class, you must create instance of that class first.
In first code you have not declared inflater.inflate(R.layout.layout_dialog,null); as type View, so compiler doesn't know what you are intended to use.
But in second code, you have declared your inflater.inflate(R.layout.layout_dialog,null); as View type so you can use method that is applicable for View class.
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.
I'm struggling a bit with some semantics on some basic Android/Java development. If I have the following code (which works) to gather user input from a textfield:
final EditText userInput=(EditText) findViewById(R.id.txtUserInput);
Is userInput an object or a variable? My understanding that it is an object being instantiated form the *EditText * class. What does the (EditText) do to the left of the findViewById. When I see open parens, I think casting. Can anyone provide some simple clarity?
You are correct in saying that userinput is an EditText Object, to be more specific it is an object that is a subclass of View. Everything you get back from the findViewbyId() method will be a View, which you then need to cast to the proper Object. The (EditText) is casting the View you got back from your xml to an EditText. This allows you to access methods from the EditText that are available to the EditText class in particular.
So whenever you use findViewById() you also need to cast the View you get to the Object that it represents.
Let me know if you need further help.
-Dejan
userinput is an object.
findViewById(xxx) returns a View object, but in your case you know that it will return an EditText. Therefore its possible to cast it with (EditText). And you can cast it from a View to EditText since EditText extends View.
When you have cast it to EditText you are able to find all methods exposed by EditText instead of only the methods exposed in View.