Android databinding setText() over model binding - android

In my xml I have a line android:text='#{entry.printTitle}'
In my adapter I set entry: binding.setEntry(item); in bind() method.
For some occasions I want to set that field manually with binding.entryTitle.setText("some other title"), but it doesn't work. How can I set that field without affecting entry, which is immutable?

You can not override values of binding variables. If you want change dependent views, you have to change its variable value.
You can take another variable for this purpose. And set value in this second variable when you want. Just make this second variable null when you have done.
android:text='#{entry.fakeTitle ?? entry.printTitle}'
Now when you want change title but not its variable then set value in fakeTitle.
binding.getEntry().setFakeTitle("testing");
When you have done make it null so text will be printTitle again.
You have to use ObservableField<String> or LiveData, if you are changing value programmatically. If you are extending by BaseObservable then you have to make fakeTitle #Bindable and notify after changing.

Related

How to Enable/Disable a button based on all editetext is not empty using viewmodel and databinding in kotlin android

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

Best practice to access property in kotlin

I comes from Java background and working first time on Kotlin. For most of the people it will be basic question, but it may help people who start working first time on Kotlin and comes from Java background
So, let say I have listadapter and I want to set list of item in that. I have two options now.
1) create a private property which stores list of items and then create a setter for it, which set the list and call notifydatasetChanged()
2) create a property with set property function and then access like instance.property
Which will be better option in Kotlin out of above two options.
A property in Kotlin is nothing else then a getter and setter for a value. If you don't want to provide a getter, you have to use a fun setData(data: List).
Otherwise it's also possible to handle everything with the property
var data = listOf()
set(data: List) {
field = data
notifydatasetChanged()
}
But eventually it's even better to use an implementation with DiffUtil.

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

Android Button integer ID

I would like to have a function that if called can be passed the name of the button and then change the text of said button. I have attempted to use the function findViewById but it wont let me pass a string as it takes a int. This is my question, how can i store the integer value of the Button id's so i can pass to this function?
You could use tags if you need to deal with Strings. Set a tag on the Button using the android:tag xml attribute or setTag. Then you can use findViewWithTag to get a reference to the button.
But it'd probably be easier to just use IDs referenced from the resources object R: R.id.button

Android : How to get the dirty(changed) properties

How to get the dirty(changed) properties(any controls subclasses of view) amongst a number of properties on a layout. Does android has a dirty flag to mark if the layout has any field that has a changed content??
Actually, i have a layout that has a few edittexts,spinners,datewidgets.Now doing a save should make request to the server only if the user has modified anything .I wanted to know that how can i check if the user has changed anything in that layout or not.Does android has any flag or something that sets itself when the user modifies any input control?
Hmm..Blckberry Does have isDirty(){boolean net.rim.device.api.ui.Manager.isDirty()}method.
The activity is not tightly coupled to the elements in your layout, so you'll have to do this yourself. You could maintain a Map where the key is the id of the layout element, and the value is a boolean that signals if the element has been modified by the user. You would probably need to set up listeners on each element (such as OnKeyListener for your EditTexts) and additionally capture their initial values.
Does android has a dirty flag to mark
if the layout has any field that has a
changed content??
No, sorry.
Bit late with my answer, but the way I do it is to store the form (activity) fields in a container object (for validation, etc). This container object implements the
java.lang.Comparable interface, where T is the same class as the container.
The compareTo(T) method then returns
0 if both objects are equal (thereby form contents haven't changed).
-1 indicates that something has changed and therefore the data is dirty
You can always return other numeric values to indicate what exactly has changed if required.

Categories

Resources