Android Data Binding comparison in xml - android

im wondering whether i can do this comparison with databinding or not .. i want the field to have an inputmask based on the lenght of the numbers im getting so this is what im trying to do >
app:inputMask="#{state.capacity.length() > 3 ? InputMask.TON : InputMask.KG}"
but this isnt quite working as i planned out , i wanted to do liek an IF , if capacity lenght >3 then do this if not do that ..
but i think the ? means if its null and not true.. so any clue on how to achieve that ?

You can use databinding adapter.
#BindingAdapter("myInputMask")
fun setMyInputMask(maskedEditText: MaskedEditText?, capacity: Int?) {
if (capacity > 3) {
editText.setMask(InputMask.TON)
} else {
editText.setMask(InputMask.KG)
}
}
app:myInputMask="#{state.capacity.length()}"

Related

Cant check if my textview that inside of an array is visible or not in Kotlin

I have a 2D array (matrix) of Textviews called Board. Board has 16 Textviews in it, only one of them is invisible. In this given lines of code, I tried to find the invisible one between all the rest. For some reason, the line with the If condition collapses my app every time. I don't understand what my problem is, can someone help me?
P.S. Sorry for my English, it's not my native language.
Here is my code:
var i = 0
for (i in 0..4) {
var j = 0
for (j in 0..4) {
var tvtemp = board[i][j]
if (tvtemp.visibility == View.INVISIBLE) {
Toast.makeText(applicationContext,board[i][j].text, Toast.LENGTH_SHORT).show()
}
}
}
== operator is used to compare the data of two variables.
Please don’t misunderstand this equality operator with the Java == operator as both are different. == operator in Kotlin only compares the data or variables, whereas in Java or other languages == is generally used to compare the references.
Solution:
if(!tvtemp.isVisible){
....
}
You could try using the isShown method. It determines if the view is visible to the user. If a view's visibility is set to be visible, but it it crossed off the screen then isShown will equal false. If you have no scrollview and all views are fitted in the activity then this could work for you
if (!tvtemp.isShown) {
Toast.makeText(applicationContext,board[i][j].text, Toast.LENGTH_SHORT).show()
}

Unable to get a "null" value from Firebase snapshot in Unity on android

I want to do x when FirebaseRef snapshot.Value == null. This works in the Unity Editor everytime but when I do this on the Android device, it fails every time. The app doesn't crash, it just doesn't do anything.
auth is working fine.
database is working fine (I'm getting all other values when the values exist) but when the value doesn't exist, that's where I have the issue.
Here is my sudo code, any help would be appreciated.
ref.Child("Location/That/Doesnt/Exist").GetValueAsync().ContinueWithTask(Task => {
if (task.IsFaulted){
//Do something
}
else if (task.IsCompleted){
DataSnapshot snap = task.Result;
if(snap.Value == null){
//This is what hangs. I get here and it just does nothing.
}
}
});
I'm so lost. I'm not sure how to figure this out. I get no errors but it's like the function just stops there.
Thanks all!
EDIT
So what I'm doing to get around this is to create a DB entry in the path with key == "0" and value == 0. Then I say if the ChildCount is greater than 1 and that child has more than 1 child, proceed.
I feel like this is a really poor approach.
I was getting the same error. I caught an exception of this, and i got NullReference, it's seems that on android when it's not found on database return Null, so you have to convert this:
if(snap.Value == null){
}
to this:
if (snap==null && snap.Value==null)
You are trying to access to snap.Value when snap object is null.
Sorry my english, i hope this helps you.
try trailing slash
"Location/That/Doesnt/Exist/"

Android data binding nested ternary on expression

In android's data binding syntax is there anyway to do a nested ternary on the result of an expression without calculating the expression twice:
For the single ternary case I have the following:
android:textColor="#{stock.mStockDelta.compareTo(BigDecimal.ZERO) < 0 ?
#color/red : #color/green}"
I'm wondering if there's a way I can set the color for each one of three compareTo results {-1, 0, 1} just using the xml?
Possible duplicate (Switch case in Data Binding)
For the advanced logic in the data binding, you should put your own logic into the method and call it in the xml
android:textColor="#{stock.mStockDelta.getColor}"
Do your own logic in the getColor method instead of put a lot of condition in the xml. You should remain your xml fully-readable. Leave the logic flow for the java class.
android:textColor="#{stock.mStockDelta.getColor > 2 ?( stock.mStockDelta.getColor ==3 ? #color/green :#color/colorPrimary): #color/colorAccent }"
you can try this
android:textColor="#{stock.mStockDelta.compareTo(BigDecimal.ZERO) < 0 ?
#color/red : (stock.mStockDelta.compareTo(BigDecimal.ZERO) > 0 ? #color/green : #color/blue)}"
But this is not recommended, you should put this logic in view model as George Mount suggested in this comment

Android Exception: UnknownFormatConversionException

Hi I am setting some text inside a RobotoText that is positioned inside of a ViewHolder I am calling it like this:
viewHolder.txtSimilarAds.setText((((Property) ads.get(position)).getSimilar_items_count() == 1 ? context.getString(R.string.ad_data_similar) : context.getString(R.string.ad_data_similar_plural, ads.get(position).getImagesCount())));
However (sometimes NOT always)for some reason I keep getting this error exception UnknownFormatConversionException and it points to this line inside of the class. What could be the problem? Am I doing anything wrong?
Check that the string in R.string.ad_data_similar_plural contains a valid placeholder for an integer. It should be something like "Here is my number: %d".
String format specification
As an aside, one-liner like these are harder to understand, and make debugging more difficult. A more readable approach would have given the erroneous line more easily:
String similarAdsText;
Property adsProperty = ads.get(position);
if (adsProperty.getSimilar_items_count() == 1) {
similarAdsText = context.getString(R.string.ad_data_similar);
}
else {
similarAdsText = context.getString(R.string.ad_data_similar_plural, adsProperty.getImagesCount());
}
viewHolder.txtSimilarAds.setText(similarAdsText);
You are trying too many methods in single line try splitting it. The problem looks like you are comparing Property object with 1 which is wrong.
(Property)ads.get(position)).getSimilar_items_count() == 1
change this to
Property property = (Property) ads.get(position));
property.getSimilar_items_count() == 1

How to get all InputTypes in EditText dynamically?

I want to fetch all possible InputTypes available for EditText dynamically.
I have checked out couple of links, I know how to setInputTypes through xml or dynamically, how to fetch all types of InputTypes?
I have checked the InputType.class too, didnt't find any such method in it. Still is it possible to get all the InputTypes?
Thanks
I can't imagine why you need this, and probably there is a better solution to your problem, but here is an example using reflection, sans the exception handling:
for (Field f : InputType.class) {
nt mod = f.getModifiers();
if (Modifier.isStatic(mod) && Modifier.isPublic(mod) && Modifier.isFinal(mod)) {
//do something with this
f.getName();
}
}

Categories

Resources