--

Showing posts with label Ready-made UI libraries. Show all posts
Showing posts with label Ready-made UI libraries. Show all posts

Monday, 30 April 2018

Kotlin - Chapter 3 - Showcasing Viewpager using Kotlin


ViewPager in Android allows the user to flip left and right through pages of data. Screen slides are transitions between one entire screen to another and are common with UIs like setup wizards or slideshows.
This post shows you how to do screen slides with a ViewPager provided by the support library in Kotlin. ViewPagers can animate screen slides automatically.
You’ll divide the ViewPager implementation into three parts:
  1. Adding the ViewPager
  2. Creating an Adapter for the ViewPager
  3. Wiring up the ViewPager and the Adapter
We have to add android.support.design.widget.TabLayout & android.support.v4.view.ViewPager in layout file same as Java.


ViewPager is only available through the Android Support Library. The Android Support Library is actually a set of libraries that provide backward compatible implementations of widgets and other standard Android functionality.
These libraries provide a common API that often allow the use of newer Android SDK features on devices that only support lower API levels. You should familiarize yourself with the Support Library and Support Library Packages.
We need to create our PageAdapter. This is a class that inherits from the FragmentPageAdapater class. In creating this class, we have two goals in mind:
  1. Make sure the Adapter has our fragment list
  2. Make sure it gives the Activity the correct fragment
By extending FragmentPagerAdapter, we can add multiple fragments to the ViewPager. We need to implement setupViewPager method in the ViewPagerActivity.

supportFragmentManager is equivalent to the getSupportFragmentManager() method you would use in Java and viewPager.adapter = pagerAdapter is the same as viewPager.setAdapter(pagerAdapter).
Note : The FragmentPagerAdapter is the more general PageAdapter to use. This version does not destroy Fragments it has as long as the user can potentially go back to that Fragment. The idea is that this PageAdapter is used for mainly "static" or unchanging Fragments. If you have Fragments that are more dynamic and change frequently, you may want to look into the FragmentStatePagerAdapter. This Adapter is a bit more friendly to dynamic Fragments and doesn't consume nearly as much memory as the FragmentPagerAdapter.
The output must be,

FragmentPagerAdapter or FragmentStatePagerAdapter?
There are two types of standard PagerAdapters that manage the lifecycle of each fragment: FragmentPagerAdapter and FragmentStatePagerAdapter. Both of them work well with fragments, but they are better suited for different scenarios:
  1. The FragmentPagerAdapter stores the fragments in memory as long as the user can navigate between them. When a fragment is not visible, the PagerAdapter will detach it, but not destroy it, so the fragment instance remains alive in the FragmentManager. It will release it from memory only when the Activity shuts down. This can make the transition between pages fast and smooth, but it could cause memory issues in your app if you need many fragments.
  2. The FragmentStatePagerAdapter makes sure to destroy all the fragments the user does not see and only keep their saved states in the FragmentManager, hence the name. When the user navigates back to a fragment, it will restore it using the saved state. This PagerAdapter requires much less memory, but the process of switching between pages can be slower.

Now save and run the App. You can download the full source code for this project on GitHub

Happy Coding !!!

Monday, 28 November 2016

FIREBASE - Started with Authentication

           Firebase allows an app to securely save user data in the cloud and provide the same personalized experience across all of the user's devices. Firebase Authentication provides secure backend services, easy-to-use SDKs, and ready-made UI libraries to authenticate users to our app.

          The ready-made Authentication methods support developers to enhance the functionality and usability of the application. I have shared the work flow video of Firebase Authentication App for better understanding,




This video portrays the user registration, updating user information like display name, profile picture, reset password, and user login using Firebase Authentication backend services. I elucidate the complete Authentication implementation further.

Step 1: User registration
Get Firebase auth instance in OnCreate()
authRegister = FirebaseAuth.getInstance(), then
and call authRegister.createUserWithEmailAndPassword(email,password).



authRegister.createUserWithEmailAndPassword(email,password).
        addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                showProgress(false);
                if (!task.isSuccessful()) {
                    Log.e(TAG,"Register exception:"+task.getException());
                   } else {
                    finish();
                    startActivity(new Intent(RegisterActivity.this,HomeActivity.class));
                }
            }
        });

Step 2 : User signin
By calling signInWithEmailAndPassword(email,password) when user click Sign in button. 


 authLogin.signInWithEmailAndPassword(email,password).
        addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                showProgress(false);
                if (!task.isSuccessful()) {
                    mPasswordView.setError(getString(R.string.messgae_login_fail));
                    mPasswordView.requestFocus();
                } else {
                    finish();
                    startActivity(new Intent(LoginActivity.this, HomeActivity.class));
                }
            }
        });

Step 3: Update user information
Creating UserProfileChangeRequest by adding display name and profile picture.

UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
        .setDisplayName(etxtDisplayName.getText().toString())
        .setPhotoUri(photoUri)
        .build();



By calling updateProfile(UserProfileChangeRequest) method to update user information.
existingUser.updateProfile(profileUpdates)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    showProgress(false);
                    Log.d(TAG, "User profile updated.");
                 }
            }
        });

Step 4: Reset Password
By calling sendPasswordResetEmail() to reset instructions sending to our given valid mail id.

  We have to click on the given link from received mail to proceed further.


Once user clicked, 'Reset your password' window opens to enter your new password.



Step 5: Update Email & password
By calling updateEmail(email) & updatePassword(password) methods to update credentials respectively. 

Step 6: Delete user account & Sign out
By calling auth.signOut() method to close existing user session.
Initialize FirebaseUser existingUserFirebaseAuth.getInstance().getCurrentUser(); in OnCreate()
By calling  existingUser.delete() to eradicate user details from cloud completely.

I have shared the complete source code in Github, 
https://github.com/JayaprakashR-Zealot/FirebaseAuthentication

Kindly share your queries in the comment section.

Happy Coding !!!
Thanks..