testPreconditions for EditText hint field - android

I am trying to test that my EditText hint field is not null in junit's
testPreconditions()
There are examples online for asserting that a TextView is not null, but using EditText in the following way
private EditText editTextField;
public void testPreconditions() {
assertNotNull(editTextField);
}
does not work (tests fail).
How do I access the android:hint field of EditText in the assert?
And while this question targets the hint field, answers that also work for similar situations (text of a button, title / elements of a spinner, etc) would be greatly appreciated.
Thanks!

To get hint from EditText use getHint function.
But it looks like your problem is different. Your test fails because your editTextField is null. You should read "Activity Testing" article. Shortly, JUnit is not enough, you need to setup a special Android Test project in order to access UI views of Activity under test.

If you want to check for not null of hint you can use the following code
EditText et = new EditText(this);
if((et.getHint().toString() != null) && (et.getHint().toString().trim().length()>0)){
}

Related

empty EditText caused by wrong view?

I'm new to android studio and programming. I create second table to my database and have almost identical onClickListener class first one work perfect but second give me error. EditText .getText().toString() is empty so I can't convert it to dobule. I think this problem may be caused by View but I dont know java that well and I cant find solution.
EDIT: I manage to solve this problem by adding AlertDialog to my second OnClick listener. This is not exactly what I want but it works.
Could you try setting the input type on the edittext as numberDecimal
android:inputType="numberDecimal"
That way, the user will have to input decimal number always if thats what you are expecting the user to enter.
Also, you could do a null check
if(stringWzrost != null && !stringWzrost.trim().equals("")) {
bmiWzrost = Double.valueOf(stringWzrost);
}
Right now I see that its commented out.

Use EditText in a DialogFragment to change TextView in Main Activity?

So I'm working on my first real attempt at an Android app, just a simple scorekeeper for softball games. I've got it tallying scores, outs, etc, and right now it just displays "Home" and "Away." I'd like to give users the chance to enter in actual team names. To this effect, I added a custom AlertDialog that pops up with an EditText, so when you hit "OK" it'll update the Home/Away team name.
The problem is I've been Googling this for most of the week and I've not found a single way to actually do this. I've tried tagging the fragment's layout XML so I can find the EditText, but it always gives me a null reference and crashes the app. I added a TextWatcher that presumably watched the fragment's text, but once changed and hit "OK," nothing happened. Tried adding the TextWatch to the fragment, that crashed I think, it was about two hours ago and I'm exhausted.
Really, I think I need a way to have the fragment find the TextView with the team name and change it to the value of the EditText when I positive click, but I don't know how to do that. Or maybe I've seen it and don't understand it, I've only been doing this about two months. I'd post my code, but I deleted out all the stuff that didn't work because it was taking up most of the screen real estate. Any ideas?
EDIT: So I followed advice below on defining views, that found the value of the EditText presumably, but hitting "OK" just made it set the TextView to a blank value. I think this is because the EditText's contents went away as the dialog was closed. Either that or this is all wrong. View dialogname = getActivity().getLayoutInflater().inflate(R.layout.fragment_home, null);
EditText mEtName = (EditText) dialogname.findViewById(R.id.homeName);
View mainAct = getActivity().getLayoutInflater().inflate(R.layout.activity_softball, null);
TextView oTextView = (TextView) mainAct.findViewById(R.id.teamOne);
newName = mEtName.getText().toString();
oTextView.setText(newName)
I think you are doing wrong at time of defining id for EditText. You need to give it dialog reference.
rather than doing
Edittext edittext = (EditText) findViewById(R.id.editText);
do like
Edittext edittext = (EditText) dialog.findViewById(R.id.editText);
Been through this issue a while ago. I created my own class which extended DialogFragment. When I tried to initialize EditText which was in the dialog, I got null like
mEtName = (EditText)dialog.findViewById(R.id.editText);
So, I initialized it in this way and it worked:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
...
View dialogName = getActivity().getLayoutInflater().inflate(R.layout.dialog_name, null);
mEtName = (EditText) dialogName.findViewById(R.id.dialog_etxt_name);
}
Where R.layout.dialog_name is my custom dialog layout.

EditText methods in java android

Hello I'm new to android developing.
Is there a method in java that equals to #.gotFocus?
Is there in java an events list that I can watch and select like in c# visual studio?
I tried to do #.Focus or something similar but had no success.
I want to reproduce the following scheme:
1- EditText has a certain hint => "Enter a value"
2- The user clicks the edit text and the hint disappears => ""
3- The user fills a certain value => "certain value"
Thank's for helpers :)
Ron Yamin, If I understand your doubt correctly what you want is:
1- Have a field of text for the user to type words/numbers etc --> It is called EditText in android
2- Have an hint so the user knows what to type --> Eg. "Type your name"
3- And react to focus in some way.
The first one you will achieve either through XML or by code. If you have a main.xml in your layouts folder (assuming you are using eclipse/android studio to develop), you can use the interface to drag an edit text to the android screen.
The second one you will achieve still through the XML. If you right click on it, right side of the screen there will be a little window called Proprieties that you can change things like height and width and a hint. Type there your hint.
Finally the last one you need to go to your code in .java and get a reference of your edit text (findViewById).
Either through setOnClickListener or setOnFocusChangeListener.
More info you can checkout here:
http://developer.android.com/guide/topics/ui/controls/text.html
I have googled a tutorial you can check with more detailed information and step by step guide.
Hope it helps:
http://examples.javacodegeeks.com/android/core/widget/edittext/android-edittext-example/
It seems that you changed your question quite a bit, and my C# ignorance got the best of me.
It seems that what you really want is an EditText, the example text you are looking for is the hint.
You can set the hint in the xml file or by code with .setHint(string) method.
Here's where to start:http://developer.android.com/guide/topics/ui/controls/text.html
edit 3 - events in android are dealt with by using listeners. You can use an onClickListener to achieve what you want.
textView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(){
//dostuff
}
}
Assuming your textfield is an instance of EditText (which it probably should be), you can do the following:
textfield.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// this is where you would put your equivalent #.gotFocus logic
}
}
});
It's worth noting that the behavior you've described can be achieved by using textfield.setHint. The hint is text that is cleared automatically when the user selects the EditText. It's designed specifically for the case you describe, e.g. textfield.setHint("Enter a Value")
I'm not familiar with c# but I'm guessing you want event fired when edittext get focus. Try this
EditText txtEdit= (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// do the job here when edittext get focus
}
}
});

required field validation on android

In android how do I create a required field validation? like on a page where use can enter some data into some EditText. I wanted to make sure user enter something before continuing, like if the user forgot to enter some data, the app would not crash. I have done other validation like number only using those input-type provided. but so far I research I only found ways to validate content entered but not whether there is something entered.
So I should put something in the onCreate method like
if(EditText text is !=null){ do the stuff} else {error message}
But if I did that, the moment the app is run there will be error displayed.
And how do I write the "text of EditText" in c# I believe is TextBox Text. But I do not know how to do that in java android. I know setText but do not know how to refer to the content without changing it.
To get text user entered in the EditText you can call getText() method. I recommend you to perform validation after user clicks some button. Validating content of EditText inside onCreate() method is useless.
It's always better to tell the user that they need to put the correct information the earliest possible. Why? Because it allows the user to quickly identify the problem and fix it. That being said, I would recommend checking if its null or whatever you want the textfield to contain as soon as it looses focus.
textView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus)
//We get the text and then convert it to a string and check
if(v.getText().toString() == ""){
//Do what you want in this area
}
}
});
String str = textview.getText().toString().trim()

setText on EditText and TextView not working

We have a bigger Android 2.1 project, and for some reason, only at two places calling setText() on an EditText or a TextView object just doesn't work. It does not crashes, does not throw an exception, only the TextView/EditText is not updating.
Here's the code for setting the text:
EditText etzip_a = (EditText)activity.findViewById(R.id.editTextZip_a);
etzip.setText(m_addressA.zip);
etzip.addTextChangedListener(new onInputChanged(120));
this code runs on the GUI thread
it parses the R.id.editTextZip_a object successfully from the layout
a totally similar code for other tens of EditText are working fine
Does anybody encountered a problem like this?
try using this code:
You have create a object etzip_a of the EditText. Now after that you are using different object etzip.
EditText etzip_a = (EditText)activity.findViewById(R.id.editTextZip_a);
etzip_a.setText(m_addressA.zip);
etzip_a.addTextChangedListener(new onInputChanged(120));
EditText etzip = (EditText)activity.findViewById(R.id.editTextZip_a);
replace this line of code

Categories

Resources