Displaying Toast message from user defined variable - android

How do I pass a user variable as a parameter in the Toast object in android studio to display the value of the variable?
Something like this;
Toast.makeText(userVariable,R.id.textField,Toast.LENGTH_SHORT).show();
Thanks

Use getViewById() to get the Text input as a View, cast it to a TextInputView and then once you've done that, you can call getText().toString() and pass this into the toast.

Related

Android - Handling event on an add button issue

I want to insert text from field into a String array every time the add button is pressed, and when done so, the field should empty itself for a new string.
Here is my code:
Editor note: no code provided as example, only a screenshot of the code
Is the add function ever called? Otherwise you are sitting with a button without an onClickListener.
Try assigning The onClickListener in oncreate instead.

Globaly request focus on setError()

Is there any way that i can set an attribute globally and request focus whenever an error is set?
Suppose i am validating a lot of EditText fields and setting an Error for each of them, after setting each other i have to request a focus:
editText.setError("Error");
editText.requestFocus();
However there exist a lot of fields and i am required to do call .requestFocus() in every single one. Is there any way to set it globally or setting explicitly is the only way around?
you can create one function for that and call that function in your whole app by giving class reference.
public static void EdittextError(EditText editText, String Error) {
editText.setError(Error);
editText.requestFocus(); }
Pass edit text obj. and Error String in function parameter.
Is there any way to set it globally or setting explicitly is the only way around?
No, there's no special attribute in system widget, so you have to set it by hand or extend EditText and override setError() to automatically request focus when called and then use MyEditText in your layouts

Printing an object's toString not behaving as expected

I have tried to print a variable value but this is what was printed:
android.support.v7.internal.widget.TintEditText#41fb6428
My code is:
Torre objTorre = new Torre();
objTorre.setLatitude(txtLat.toString());
String torre = objTorre.getLatitude();
System.out.println(torre.toString());
just do this
objTorre.setLatitude(txtLat.getText().toString());
Change the first line to:
objTorre.setLatitude(txtLat.getText().toString());
To get the text from your EditText and not the reference to it.
getLatitude() returns an instance of android.support.v7.internal.widget.TintEditText, which AFAIK is a subclass of EditText.
If you are trying to get at what the user typed into the EditText as a String, call objTorre.getLatitude().getText().toString().

Semantics - Android EditText 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.

how to add firstvalue by default null in spinner.?

I get the list data from webservices (Json) in spinner. But before websevices data in spinner i want show null value.(Need first value is null)
please any one help me..
please give me a sample code.
Thank You.
You could add a dummy object representing your null value. But spinners are not designed for this - they should contain some data (or nothing if no data is available).
So in your case I would recommend using a ListView or something similar instead which could be opened in a new activity with startActivityForResult and return an Intent.
Are you doing whole process with single thread?
you can use your listener, first you inflate empty array/list and later you can set your data change..
A null value inside a spinner will give you a really annoying exception. It's better to add as first element of the array some object like "Please, select one" or "Nothing selected". When you create the array you give to the spinner, first add this element and then load the real data.

Categories

Resources