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.
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
I have a GuidedStepFragment and I want to get view of one its actions (which is editable),cast it to EditText and hence set some of its parameters like TextColor programmatically.
So I should put this code at somewhere in the code:
View v0 = getActionItemView(0);
View v1 = v0.findViewById(R.id.guidedactions_item_title);
final GuidedActionEditText v2 = (GuidedActionEditText) v1;
v2.setTextColor(Color.RED);
I tried OnCreate , OnCreateView , ...(very trials actually) and each time I am facing to a NullPointerException saying the getActionItemView are returning Null!
At which function should I put this snippet to satisfy my need (i.e. change text color at fragment startup)?
I found the solution
Just putted above code at onGuidedActionFocused method !
I am new in android.I have a simple doubt,Is there any way to get id of a widget from some layout without set it as content view ?? I am looking to invisible a view from a class .I used the code
View b = findViewById(R.id.id2);
b.setVisibility(View.GONE);
But error in "id",is it possible to get ids if widget from a java class without set as contentview ?? please help me. Thanks in advance :)
You can inflate the views separately and then use findViewById on the root of the inflated layout.
// inside of Activity, you can use 'this' for the context
LayoutInflater inflater = LayoutInflater.from(context);
View root = inflater.inflate(R.layout.some_layout);
// from your example
View b = root.findViewById(R.id.id2);
b.setVisibility(View.GONE);
At this point however these views are not part of your Activity's view hierarchy and will not be seen by the user. You will have to add them either by using setContentView(root) or by finding a ViewGroup in the current view hierarchy and calling viewGroup.addView(root).
You can do it this way
Button filterButton = new Button(YourActivity.this);
filterButton.setVisibility(filterButton.GONE);
onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(view) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.
See below link :-
Why findViewById() is returning null if setcontentview() is not called?
'But error in "id",is it possible to get ids if widget from a java class without set as contentview ?? '
You have to create dynamic view then you do not have need to call setcontentview.
how to set setContentView?
Is there a way to set setContentView(int id) dynamically?
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.