8

I want to call my Util class method in layout.xml file like

<TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@{PreferenceUtil.getSavedUser().fullName}"/>

I have imported PreferenceUtil

<import type="com.amelio.utils.PreferenceUtil"/>

And PreferenceUtil.class has some methods.

public class PreferenceUtil {

    public static LoginResponse getSavedUser() {
        SharedPreferences sf = Amelio.getInstance().getSharedPreferences(PREF, Context.MODE_PRIVATE);
        String userJson = sf.getString(PREF_USER_DATA, null);
        if (userJson == null || userJson.equals("")) {
            return null;
        }
        return new Gson().fromJson(userJson, LoginResponse.class);
    }
}

Issue

    Found data binding errors.
****/ data binding error ****msg:cannot find method getSavedUser() in class com.amelio.utils.PreferenceUtil
file:D:\Khemraj\_AndroidStudioWorkspace_\amelioFinal\app\src\main\res\layout\activity_cart.xml
loc:94:40 - 94:68
****\ data binding error ****

Is this even possible, also suggest if this is recommended or not.

5
  • 1
    Calling static methods from data binding expressions definitely works -- I use Html.fromHtml() in various book examples. Is LoginResponse marked as public? Also, please do not do I/O (getSharedPreferences()) and data parsing (new Gson().fromJson()) on the main application thread, as you are here. Commented May 11, 2018 at 13:48
  • Why does it say can not find getSavedUser Commented May 11, 2018 at 13:50
  • For example, if LoginResponse is not public, it is possible that the data binding framework cannot really resolve that method reference. Commented May 11, 2018 at 14:08
  • @CommonsWare Am I able to access getSavedUser() from layout file if PreferenceUtil is a final class and not publicly instantiable. I mean the class is public and getSavedUser() method is public and static Commented Jul 3, 2019 at 7:19
  • @user3135923: Use an <import> element in the <data> to declare the PreferenceUtil type, using the fully-qualified class name. Then, your binding expressions can refer to the static methods on that class. Commented Jul 3, 2019 at 10:48

2 Answers 2

8

I hope you must have found the answer in case if you are still struggling then find the answer below.

    <data>
      <import type="com.amelio.utils.PreferenceUtil"/>
 </data>


<TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text='@{PreferenceUtil.getSavedUser()}' />

Please make sure your LoginResponse is marked public to access the values.

Sign up to request clarification or add additional context in comments.

Comments

1

in case you haven't found the answer yet:
you need to import the object type and cast it to that type like this:

<data>
  <import type="com.amelio.utils.PreferenceUtil"/>
  <import type="yourdirectory.LoginResponse"/>

</data>


<TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text='@{((LoginResponse)(PreferenceUtil.getSavedUser()).fullName}' />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.