From 466f42e8a834e01f699ba21c0a4fb0f15f9976e6 Mon Sep 17 00:00:00 2001 From: jacques Date: Fri, 22 Jan 2021 21:17:04 +0100 Subject: [PATCH] Welcome tuto code --- .../net/foucry/pilldroid/WelcomeActivity.java | 202 ++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 app/src/main/java/net/foucry/pilldroid/WelcomeActivity.java diff --git a/app/src/main/java/net/foucry/pilldroid/WelcomeActivity.java b/app/src/main/java/net/foucry/pilldroid/WelcomeActivity.java new file mode 100644 index 0000000..a5f69e6 --- /dev/null +++ b/app/src/main/java/net/foucry/pilldroid/WelcomeActivity.java @@ -0,0 +1,202 @@ +package net.foucry.pilldroid; + +import android.content.Intent; +import android.graphics.Color; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; +import android.widget.Button; +import android.widget.LinearLayout; +import android.widget.TextView; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.viewpager.widget.PagerAdapter; +import androidx.viewpager.widget.ViewPager; + +public class WelcomeActivity extends AppCompatActivity { + + private ViewPager viewPager; + private LinearLayout dotsLayout; + private int[] layouts; + private Button btnSkip, btnNext; + private PrefManager prefManager; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // Checking for first time launch - before calling setContentView() + prefManager = new PrefManager(this); + if (!prefManager.isFirstTimeLaunch()) { + launchHomeScreen(); + finish(); + } + + // Making notification bar transparent + getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); + + setContentView(R.layout.activity_welcome); + + viewPager = findViewById(R.id.view_pager); + dotsLayout = findViewById(R.id.layoutDots); + btnSkip = findViewById(R.id.btn_skip); + btnNext = findViewById(R.id.btn_next); + + + // layouts of all welcome sliders + // add few more layouts if you want + layouts = new int[]{ + R.layout.welcome1, + R.layout.welcome2, + R.layout.welcome3, + R.layout.welcome4, + R.layout.welcome5, + R.layout.welcome6, + R.layout.welcome7, + R.layout.welcome8, + R.layout.welcome9, + R.layout.welcome10, + R.layout.welcome11, + R.layout.welcome12}; + + // adding bottom dots + addBottomDots(0); + + // making notification bar transparent + changeStatusBarColor(); + + MyViewPagerAdapter myViewPagerAdapter = new MyViewPagerAdapter(); + viewPager.setAdapter(myViewPagerAdapter); + viewPager.addOnPageChangeListener(viewPagerPageChangeListener); + + btnSkip.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + launchHomeScreen(); + } + }); + + btnNext.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + // checking for last page + // if last page home screen will be launched + int current = getItem(+1); + if (current < layouts.length) { + // move to next screen + viewPager.setCurrentItem(current); + } else { + launchHomeScreen(); + } + } + }); + } + + private void addBottomDots(int currentPage) { + TextView[] dots = new TextView[layouts.length]; + + int[] colorsActive = getResources().getIntArray(R.array.array_dot_active); + int[] colorsInactive = getResources().getIntArray(R.array.array_dot_inactive); + + dotsLayout.removeAllViews(); + for (int i = 0; i < dots.length; i++) { + dots[i] = new TextView(this); + dots[i].setText("∙"); + //dots[i].setTextSize(35); + dots[i].setTextColor(colorsInactive[currentPage]); + dotsLayout.addView(dots[i]); + } + + if (dots.length > 0) + dots[currentPage].setTextColor(colorsActive[currentPage]); + } + + private int getItem(int i) { + return viewPager.getCurrentItem() + i; + } + + void launchHomeScreen() { + prefManager.setFirstTimeLaunch(false); + startActivity(new Intent(WelcomeActivity.this, MedicamentListActivity.class)); + finish(); + } + + // viewpager change listener + ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() { + + @Override + public void onPageSelected(int position) { + addBottomDots(position); + + // changing the next button text 'NEXT' / 'GOT IT' + if (position == layouts.length - 1) { + // last page. make button text to GOT IT + btnNext.setText(getString(R.string.start)); + btnSkip.setVisibility(View.GONE); + } else { + // still pages are left + btnNext.setText(getString(R.string.next)); + btnSkip.setVisibility(View.VISIBLE); + } + } + + @Override + public void onPageScrolled(int arg0, float arg1, int arg2) { + + } + + @Override + public void onPageScrollStateChanged(int arg0) { + + } + }; + + /** + * Making notification bar transparent + */ + private void changeStatusBarColor() { + Window window = getWindow(); + window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); + window.setStatusBarColor(Color.TRANSPARENT); + } + + /** + * View pager adapter + */ + public class MyViewPagerAdapter extends PagerAdapter { + + public MyViewPagerAdapter() { + } + + @Override + public Object instantiateItem(ViewGroup container, int position) { + //LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); + LayoutInflater layoutInflater = getLayoutInflater(); + + View view = layoutInflater.inflate(layouts[position], container, false); + container.addView(view); + + return view; + } + + @Override + public int getCount() { + return layouts.length; + } + + @Override + public boolean isViewFromObject(View view, Object obj) { + return view == obj; + } + + + @Override + public void destroyItem(ViewGroup container, int position, Object object) { + View view = (View) object; + container.removeView(view); + } + } +} \ No newline at end of file