Updated 10 June 2017
In this example, I used data binding on the navigation drawer to update the layout whenever a user logged in the navigation drawer change its layout. Make a class which extends a BaseObservable class and put a key in shared preference so whenever the value changes on the particular key shared preference listener notify the login variable.
Here is my public class LoginChecker:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
public class LoginChecker extends BaseObservable { private boolean login; private static SharedPreferences.OnSharedPreferenceChangeListener listener; public LoginChecker(Context context) { SharedPreferences shared = context.getSharedPreferences("customerData", MODE_PRIVATE); login = shared.getBoolean("isLoggedIn", false); listener = new SharedPreferences.OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { login = sharedPreferences.getBoolean("isLoggedIn", false); notifyPropertyChanged(BR.login); } }; shared.registerOnSharedPreferenceChangeListener(listener); } @Bindable public boolean isLogin() { return login; } public void setLogin(boolean login) { this.login = login; notifyPropertyChanged(BR.login); } @Bindable({"login"}) public boolean isUserLogin() { return isLogin(); } } |
1 2 3 4 |
<data> <import type="android.view.View" /> <variable name="data" type="webkul.opencart.mobikul.Checker.LoginChecker"/> </data> |
1 |
android:visibility="@{data.userLogin ? View.GONE : View.VISIBLE}" |
In my login.xml, data is an instance of LoginChecker class. Here the magic happens whenever the user logged in there is visibility condition so whenever the new value update on the index login in shared preference previous layout change its visibility.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.