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.
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.
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.
I can't understand the logic behind the following code in android studio when we create an instance of a Button:
Button btn = (Button) findViewById(R.id.btnRegister);
From what I know an instance of a class is defined like:
MyClass myInstance = new MyClass(MyParameter)
If we apply the above logic the button code should be like this:
Button btn = new Button(findViewById(R.id.btnRegister));
But in fact the above code does not work. Why is "button" code so complicated to understand and does not relate to the code of a mere class? What does "(Button)" in brackets mean in the above example????
Thank you
The find view line doesn't create a button. Your views are all created using the standard new syntax by the framework when you call setContentView. The setContentView function parses your xml and creates the appropriate classes. What findViewById does is search the views that it created in setContentView checking the ids for one with a matching id and returns it.
As for what (Button) does- its a standard Java cast. findViewById returns a View object. But some subclasses of View have advanced functionality not found on view. To use them, you have to have an object of the correct type. So you cast it to the correct type (which will throw an exception if it isn't the right type). For example you need to have a TextView to call setText, a normal View doesn't have that function.
I is there way to set input type on a View object? Note it is not TextView or any derived class like EditText, Button etc.
I is there way to set input type on a View object?
No.
In my case, I have constraints that require me to extend View and not TextView, yet I need to show the soft keyboard of differing types based on focus changes to objects that accept text (the objects accepting text are part of a custom, cross-platform framework and are therefore not EditText objects, etc.). Obviously, this is fighting the framework, but that's the situation.
In this case, you would have already needed to override onCreateInputConnection, which gives you an opportunity to set up the input type. It's kind of a hack, but you can get onCreateInputConnection called again by doing the following:
InputMethodManager mgr = (InputMethodManager)context.
getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.restartInput(this); // 'this' is the View
I would receive a notification from my custom objects that the keyboard needed to be opened with a certain intent, cached that intent, then performed the above couple lines. That would in turn result in a call to onCreateInputConnection, which allows you to set the input type to the cached value.
I have a class that creates a view to gather data via a function getView() that provides a view with an EditText.
This class has also has variable answer.
When the user chances the EditText I want to store the content of the EditText in answer.
If I would use an onKeyListener I fear that the answer will probably get stored before the last letter is entered.
Is there a good way to handle this in the getView() function via some other listener?
You should addTextChangedListener to your EditText and implement in your class TextWatcher
Then you will just take the text from the methods and store in your answer