Intro Screen | Android App Development | Lecture#52 | Hive Learners

Words
345
Reading
2 min
Listen
Play
4y

饾摉饾摶饾摦饾摦饾摻饾摬饾摲饾摪饾摷

Hi dear Hive Learners, I hope you all are well. In this lecture, we will add some more things to the Intro activity. We will change the design of the Intro Activity and add some colors and the most important thing we will check whether the intro is already displayed or not so that users will not see the intro again and again. We will use the sharedPreferences to store a variable and update this variable when a user go through the intro for the first time. Let's jump into it.

alt

GitHub Link

Use this GitHub project to clone into your directory. The following lecture will update it so you will never miss the latest code. Happy Coding!

What Should I Learn

  • How to save the state of Intro Class

Assignment

  • Show intro only for the first time

Procedure

First, we need to set the colors for the pages. We will add two pages. I also add a new logo of lazy-panda to use on the second page. Also, declare and initialize the SharedPreferences. I also set the Icon Layout Params and I use the height and width of 500. You can always change these parameters.

// First Page
        page1.setBackgroundColor(R.color.red);
        page1.setIconLayoutParams(500, 500, 0, 0, 0, 0);
        page1.setDisplaySkip(true);
        page1.setTitleColor(R.color.black);
        page1.setDescriptionColor(R.color.black);

        // Second Page
        page2.setBackgroundColor(R.color.black);
        page2.setIconLayoutParams(500, 500, 0, 0, 0, 0);
        page2.setDisplaySkip(true);
        page1.setTitleColor(R.color.black);
        page1.setDescriptionColor(R.color.black);

alt

Now we need to create a list and add these pages to the list and set it in the library method setOnboardPages. We also override the on finish button click and we will use it to save the state of into class and also redirect the user to the Sign in screen.

alt

I add a boolean value is_intro_done in the SharedPreference when the user clicks on the finish button. Also, open the Sign in screen, show a welcome message, and finish the intro screen.

alt

Now we need to check for the is_intro_done in the Splash activity. Declare and initialize the SharedPreference and set a check for the intro. If the intro the save boolean is false then we will open the Intro screen after the Splash screen else we will open the Login screen.

 sharedPreferences = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // After two seconds 1000ms = 1 Second
                if (!sharedPreferences.getBoolean("is_intro_done", false))
                    startActivity(new Intent(Splash_Activity.this, App_Intro.class));
                else startActivity(new Intent(Splash_Activity.this, SignIn_Activity.class));
                
                finish();

            }
        }, 2000);

alt

Let's run the app and if it is working or not.

alt

hl_divider.png

Thank You

hl_footer_banner.png

Intro Screen | Android App Development | Lecture#52 | Hive Learners | Ecency