I want to clear a field text before tap a text inside. I'm trying to do an Android Test using Kotlin.
The code:
suspend fun navegarMenu(){
clickSafe(R.id.nav_menu)
waitElementAndClick(R.id.tvProfile)
delay(4000)
// I WANT TO CLEAR THE TEXT INSIDE "et.NickName" FIELD BEFORE EDIT THE TEXT
fillEditText(R.id.etNickName, "digdindigdin")
delay(2000)
clickSafe(R.id.btnAlterNickName)
delay(10000)
}
Im this case, I need to clear the field before put "digdindigdin" inside the field. I'm using Espresso JUnit on Android Studio.
onView(withIdR.id.etNickName()).perform(clearText());
Related
I have a fragment screen where there is a form that is used to create Questions and Answers for my app. For this fragment, I use data binding, then I created many functions to validate the form, and check other stuffs.
Now, I'm creating a different fragment screen, where I'll be able to edit this Questions and Answers that were created, and for this, I want to use the same functions that were used when I created on the other fragment, for example to validate the fields that the user is editing.
I thought about implementing an Interface, and put these common functions there, so I could use it on both fragments. However, in these functions I use DataBinding, and I don't know how I can use it on the interface, so it would get the correct XML variables regarding to one fragment, or the other one.
On the screenshot bellow, it shows that I'm trying to use the binding, however I can't specify which one I'm using, otherwise the code will work only for a fragment, and not for both. Consequently, I tried to declare as DataBindingUtil but it didn't work.
Screenshot interface
If you want to go with this approach you could add the views as fields in the interface:
interface IQuestionForm {
var newQuestionTextInput: EditText
var answer1TextField: EditText
fun validateAllParametersToCreateNewQuestion(){
var allTextInputSet = true
if (newQuestionTextInput.text.isNullOrEmpty()){
newQuestionTextInput.error = "You have to enter the question"
allTextInputSet = false
}
if (answer1TextField.text.isNullOrEmpty()){
answer1TextField.error = "You have to enter an answer"
allTextInputSet = false
}
....
}
}
Then initialise those fields after creating the binding in the fragment.
I've been using rxjava2 for a while but mostly kind of a boilerplate, I didn't clearly know its features.
I supposed to create a search Edittext which call method after sometime. I got it working by the following code:
RxTextView.textChanges(edittextSearch)
.filter(charSequence -> charSequence.length() > 3)
.debounce(800, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(string -> {
search(string.toString());
}, error -> {
Log.e("ERROR","ERROR LISTENING: " + error.getMessage());
});
However, I need one more feature for this EditText, which is as soon as I type, even with just one character, I would display an "X" icon to clear the input, or hide it if user manually delete all text.
I don't know if the textChanges method above could do that or I have to add another text watcher for the edittextSearch
Thank you for your time.
You can achieve what you want by using a TextInputLayout and adding app:endIconMode="clear_text". It's part of the material design library, there are plenty of guides on how to use them.
This will automatically add a clear icon when the user begins typing, clicking the icon will clear the text and update your textChanges observable.
I need to create a Login Activity in an Android app using Data Binding with ViewModel using Kotlin. I want to enable/disable a Button based on the content of an EditText field. The expected behavior that I'm trying to achieve is the button should get enabled only when none of theEditText fields is empty.
Step 1: In ViewModel declare
var isEnabled : ObservableBoolean? = null and initialize it in init block.
Step 2: Set value of isEnabled in your Text Change Listener like
isEnabled?.set(!isLoginFormValid())
Step 3: bind variable in xml file like
android:enabled="#{viewmodel.isEnabled}"
Add a boolean MediatorLiveData in your ViewModel and bind this to the enabled attribute of the button.
You should have MutableLiveData fields that's two-way bound to your EditTexts. Add these as MutableLiveData as sources to the boolean MediatorLiveData so that it can observe changes to the EditTexts as user enters values.
Add whatever logic in the MediatorLiveData observers to set it's value to true/false depending on whatever logic you want (e.g. values of the EditTexts should not be null or empty)
you can put validations in the on click method of the login button.like:-
if(edittext_one!=null){then do the code here for the onclick }
like this you can put as much validations as you want on the edittext using if else statements.i hope this helps
So is my existing code:
fun getAllPeople(): List<People> {
return peopleDao.getAll()
...
}
and I want to wrap the List<People> in a LiveData object.
When I start typing in the front, autocomplete gives me the suggestion for LiveData here,
but then when I hit enter it completes to this.
I know I can then type < and move to the end and type >.
But isn't there an easy way to wrap something with another object correctly?
You can do this for your current selection with a custom live template:
Go to Settings -> Editor -> Live Templates
Under Android, add a new template (Alt+Insert or the green 'plus' button on the right)
Give an abbreviation and a description to your template
Set the template text to LiveData<$SELECTION$>
Set the applicable contexts to Java and Kotlin
Click Apply
Now when you select your List<People> and use "Surround with Live Template" (Ctrl+Alt+J on Windows/Linux, Cmd+Alt+J on Mac by default), you can choose your custom template from the list and watch as the selected declaration becomes LiveData<List<People>>.
Tip: You can also use the "Extend Selection" shortcut to more easily select the declaration (Ctrl+W on Win/Linux, Alt+Up on Mac by default).
here is my beginner problem.
Ive just been introduced to android programming, and now im just playing with a simple calculator program, only to see how i connect my xaml files with my source code. all the program does really is ask for two integers, then add them together, and, if the "equals" button is pressed, displays the result in an empty EditText.
1) is an EditText the right empty box to display a result?
2) how do i get the EditText to display my result when the equal button is pressed? here is the bit of code where im lacking the "if-declaration":
//output result
if //button result is clicked:
{
res.Text = res.ToString();
}
can you hint me towards the right if declaration?
thanks!
I would suggest using a Textbox
If you are using Xamarin for Android. First you have to locate the button
Get a reference to the controls that were created in the layout file via the Android Designer. Then add the following code inside the OnCreate method, after the call to SetContentView
Button buttonEqual = FindViewById<Button>(Resource.Id.CallButton);
Then add the following code to response to the button click
buttonEqual.Click += (object sender, EventArgs e) =>
{
// When the button is clicked,
// display the result in the textbox
textBox.Text = res.ToString();
}