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.
Related
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.
This is probably a mistake or lack of comprehension on my part, but I am quite confused right now. I'm trying to set a TextView in my Xamarin Android application programmatically. Here's my code:
TextView currentCharacterName =
FindViewById(Resource.Id.characterName);
currentCharacterName.SetText("test");
Unfortunately, this does not work, as I get the error "Argument 1: cannot convert from 'string' to 'int'". After reading in the available methods for SetText, I noticed the method I'm trying to call demands a ResId. I don't really understand why I would need a ResId to modify the text of a TextView.
I tried searching on Google for answers, and I came across this answer from 2014 that had the exact same problem as I do. The solution was to use the Text() method instead to set the TextView. Unfortunately, when I try this solution, I get the error "Non-invocable member 'TextView.Text' cannot be used like a method". When I try to check the Text method description, I see "string TextView {get/set} To be added."
Does this mean there's no implementation yet to set the text of a TextView? I am really reluctant to believe this, as it baffles me that such a big framework like Xamarin wouldn't even have get/set functions for something as simple as setting the text of TextView. I feel like there's a very simple solution for my problem, but I can't seem to find it.
TextView.SetText(X) allows you to set the text from a Resource id:
currentCharacterName.SetText(Resources.Id.MyString);
You are looking for the Text property:
currentCharacterName.Text = "test";
Xamarin: TextView class
Android.Widget.TextView.Text Property
Syntax:
public String Text { get; set; }
Test this code:
TextView currentCharacterName = FindViewById<TextView>(Resource.Id.characterName);
currentCharacterName.Text = "Your Text";
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 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.