Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get String value from Edit Text with databinding

I am trying to use some databinding to put in place a MVVM structure.

I have created the following xml file (sample) as for the view:

<data>
    <variable
        name="LoginVM"
        type="org.ledeme.animekeeper.LogginMV"/>

<EditText
        android:id = "@+id/input_loggin"
        android:text="@{LoginVM.username}"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_marginTop="200sp"
        android:layout_centerHorizontal="true"
        android:hint="@string/login"
        android:width="200sp"
        android:inputType="text"
        android:textAlignment="center"
        android:singleLine="true"
        android:lines="1"
        android:maxLines="1"
        />

In the View Model (mine is called LogginMV), I defined a

private ObservableField<String> username = new ObservableField<>("");

to bind the text entered by the user to username

I use username.get() to try and get the username but I only get "" (I know this is due to the value between the parenthesis in new ObservableField<>(""), if it was "test", I would get "test")

I did create a getter and setter as follow:

public String getUsername(){
    return username.get();
}
public void setUsername(ObservableField<String> username) {
    this.username = username;
    this.username.notifyChange();
}

My problem is that I can't figure out how to correctly do the binding so I get what the user inputted instead of what I define in new ObservableField<>("").

like image 514
fire frost Avatar asked Jun 06 '26 03:06

fire frost


2 Answers

I found a tweak that works great :

in the view (xml file) add this :

android:afterTextChanged="@{(edtitable)->LoginVM.afterUserNameChange(edtitable)}"

this will trigger the function afterUserNameChange in the view model.

public void afterUserNameChange(CharSequence s)
{
    //Log.i("truc", s.toString());
    this.usrNm = s.toString();
}

this function is triggered after each input the user do in the EditText

like image 139
fire frost Avatar answered Jun 08 '26 15:06

fire frost


It is better to use LiveData and MutableLiveData pattern. In your ViewModel LoginVM declare:

private var _username = MutableLiveData<String?>()
var username: LiveData<String?> = null
   get() = _username

Then in your xml for EditText add this:

android:afterTextChanged="@{LoginVM::setUsername}"
android:text="@{LoginVM.username}"

So also define setUsername function in yout LoginVM viemodel:

fun setUsername(s: Editable){
   _username.value = s.toString()
}

That's it. Now your username changes as something makes it change in viewmodel. Also if you change text in EditText it automatically updates your _username field in viewmodel. Now you can make onClick functions in your layout and use your value you typed in editText(_username) and make some useful actions.

like image 39
Leontsev Anton Avatar answered Jun 08 '26 17:06

Leontsev Anton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!