Pilldroid/app/src/main/java/net/foucry/pilldroid/WelcomeActivity.java

230 lines
7.2 KiB
Java
Raw Normal View History

2021-01-22 21:17:04 +01:00
package net.foucry.pilldroid;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
2021-01-22 21:17:04 +01:00
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowInsets;
import android.view.WindowInsetsController;
2021-01-22 21:17:04 +01:00
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
2021-01-22 21:17:04 +01:00
import androidx.appcompat.app.AppCompatActivity;
2021-02-07 21:51:19 +01:00
import androidx.core.content.ContextCompat;
2021-01-22 21:17:04 +01:00
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);
2021-03-01 18:53:22 +01:00
boolean DEBUG = false;
2021-02-07 21:51:19 +01:00
2021-01-22 21:17:04 +01:00
// Checking for first time launch - before calling setContentView()
prefManager = new PrefManager(this);
2021-02-07 21:51:19 +01:00
if (DEBUG) {
prefManager.setFirstTimeLaunch(true);
}
2021-01-22 21:17:04 +01:00
if (!prefManager.isFirstTimeLaunch()) {
finish();
}
2021-02-07 21:51:19 +01:00
prefManager.setFirstTimeLaunch(false);
2021-01-22 21:17:04 +01:00
setContentView(R.layout.activity_welcome);
setFullScreen();
2021-03-01 18:53:22 +01:00
viewPager = findViewById(R.id.view_pager);
dotsLayout = findViewById(R.id.layoutDots);
btnSkip = findViewById(R.id.btn_skip);
btnNext = findViewById(R.id.btn_next);
2021-01-22 21:17:04 +01:00
// 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();
2021-01-22 21:17:04 +01:00
if (current < layouts.length) {
// move to next screen
viewPager.setCurrentItem(current);
2021-04-04 20:14:52 +02:00
overridePendingTransition(R.anim.slide_from_left, R.anim.slide_to_right);
2021-01-22 21:17:04 +01:00
} else {
launchHomeScreen();
}
}
});
}
private void addBottomDots(int currentPage) {
TextView[] dots = new TextView[layouts.length];
dotsLayout.removeAllViews();
for (int i = 0; i < dots.length; i++) {
dots[i] = new TextView(this);
dots[i].setText("");
2021-03-28 23:15:00 +02:00
dots[i].setTextSize(65);
2021-02-07 21:51:19 +01:00
dots[i].setTextColor(ContextCompat.getColor(this, R.color.dot_dark));
2021-01-22 21:17:04 +01:00
dotsLayout.addView(dots[i]);
}
if (dots.length > 0)
2021-02-07 21:51:19 +01:00
dots[currentPage].setTextColor(ContextCompat.getColor(this, R.color.dot_light));
2021-01-22 21:17:04 +01:00
}
private int getItem() {
return viewPager.getCurrentItem() + 1;
2021-01-22 21:17:04 +01:00
}
void launchHomeScreen() {
prefManager.setFirstTimeLaunch(false);
2021-04-12 21:11:50 +02:00
startActivity(new Intent(WelcomeActivity.this, DrugListActivity.class));
2021-01-22 21:17:04 +01:00
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);
}
private void setFullScreen(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
getWindow().setDecorFitsSystemWindows(false);
WindowInsetsController controller = getWindow().getInsetsController();
if(controller != null) {
controller.hide(WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars());
controller.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
}
}
else {
2022-01-21 18:20:25 +01:00
//noinspection deprecation
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
}
}
2021-01-22 21:17:04 +01:00
/**
* View pager adapter
*/
public class MyViewPagerAdapter extends PagerAdapter {
public MyViewPagerAdapter() {
}
@NonNull
2021-01-22 21:17:04 +01:00
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
2021-01-22 21:17:04 +01:00
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(@NonNull View view, Object obj) {
2021-01-22 21:17:04 +01:00
return view == obj;
}
@Override
public void destroyItem(ViewGroup container, int position, @NonNull Object object) {
2021-01-22 21:17:04 +01:00
View view = (View) object;
container.removeView(view);
}
}
}