Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.fullproject.beanibazarhelpline.intro;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.support.design.widget.TabLayout;
- import android.support.v4.view.ViewPager;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.Button;
- import com.fullproject.beanibazarhelpline.R;
- import com.fullproject.beanibazarhelpline.model.ScreenItem;
- import com.fullproject.beanibazarhelpline.categories.CategoriesActivity;
- import java.util.ArrayList;
- import java.util.List;
- public class IntroActivity extends AppCompatActivity {
- private ViewPager mViewPager;
- private IntroViewPagerAdapter mIntroViewPagerAdapter;
- private List<ScreenItem> mScreenItemList;
- private TabLayout mTabIndicator;
- private Button mNextButton;
- private Button mGetStartedBtn;
- int position = 0;
- Animation mBtnAnimation;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- setTheme(R.style.AppTheme);
- super.onCreate(savedInstanceState);
- //make the activity full screen remove status bar
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- //when the activity is about to lunch
- //we need to check boolean value true or false and this activity is opened before or not
- if (restorePrefData()) {
- Intent intent = new Intent(getApplicationContext(), CategoriesActivity.class);
- startActivity(intent);
- finish();
- }
- setContentView(R.layout.activity_intro);
- //init views
- mGetStartedBtn = findViewById(R.id.btn_getStarted);
- mNextButton = findViewById(R.id.btn_next);
- mTabIndicator = findViewById(R.id.tab_indicator);
- mBtnAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.button_animation);
- //fill list screen
- mScreenItemList = new ArrayList<>();
- mScreenItemList.add(new ScreenItem(getResources().getString(R.string.needElectrician), getResources().getString(R.string.demoText), R.drawable.electrician));
- mScreenItemList.add(new ScreenItem(getResources().getString(R.string.needPlumber), getResources().getString(R.string.demoText), R.drawable.plumber));
- mScreenItemList.add(new ScreenItem(getResources().getString(R.string.needBuilder), getResources().getString(R.string.demoText), R.drawable.construction));
- mIntroViewPagerAdapter = new IntroViewPagerAdapter(this, mScreenItemList);
- //setup viewpager
- mViewPager = findViewById(R.id.intro_viewpager);
- mViewPager.setAdapter(mIntroViewPagerAdapter);
- //setup tablayout with viewpager
- mTabIndicator.setupWithViewPager(mViewPager);
- //next button
- mNextButton.setOnClickListener(v -> {
- position = mViewPager.getCurrentItem();
- if (position < mScreenItemList.size()) {
- position++;
- mViewPager.setCurrentItem(position);
- }
- //when we reacher last screen
- if (position == mScreenItemList.size() - 1) {
- //todo : show GETSTARTED button and hide the indicator also next button
- loadLastScreen();
- }
- });
- //tab layout change listener
- mTabIndicator.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
- @Override
- public void onTabSelected(TabLayout.Tab tab) {
- if (tab.getPosition() == mScreenItemList.size() - 1) {
- loadLastScreen();
- } else {
- mNextButton.setVisibility(View.VISIBLE);
- mGetStartedBtn.setVisibility(View.INVISIBLE);
- mTabIndicator.setVisibility(View.VISIBLE);
- }
- }
- @Override
- public void onTabUnselected(TabLayout.Tab tab) {
- }
- @Override
- public void onTabReselected(TabLayout.Tab tab) {
- }
- });
- //getstarted button clicked
- mGetStartedBtn.setOnClickListener(v -> {
- //open category activity
- startActivity(new Intent(IntroActivity.this, CategoriesActivity.class));
- //we have to save a boolean value to storage so next time when the user run the app
- //we could know that he is already chaked the intro activity
- //use shared pref for that
- savePrefData();
- finish();
- });
- }
- private boolean restorePrefData() {
- SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("mypref", MODE_PRIVATE);
- Boolean inIntroActivityOpenedBefore = sharedPreferences.getBoolean("isIntroOpened", false);
- return inIntroActivityOpenedBefore;
- }
- private void savePrefData() {
- SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("mypref", MODE_PRIVATE);
- SharedPreferences.Editor editor = sharedPreferences.edit();
- editor.putBoolean("isIntroOpened", true);
- editor.apply();
- }
- //show GETSTARTED button and hide the indicator also next button
- private void loadLastScreen() {
- mNextButton.setVisibility(View.INVISIBLE);
- mGetStartedBtn.setVisibility(View.VISIBLE);
- mTabIndicator.setVisibility(View.INVISIBLE);
- //todo: add an animation to getstarted button
- mGetStartedBtn.setAnimation(mBtnAnimation);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement