Skip to content

Commit

Permalink
Created and implemented in library circle indicator view, updated sam…
Browse files Browse the repository at this point in the history
…ple app
  • Loading branch information
Dzmitry Chyrta committed Apr 5, 2016
1 parent 4d7adea commit 5473a29
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 24 deletions.
28 changes: 17 additions & 11 deletions onboarder/src/main/java/com/chyrta/onboarder/OnboarderActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.chyrta.onboarder.utils.ColorsArrayBuilder;
import com.chyrta.onboarder.views.CircleIndicatorView;

import java.util.List;

public class OnboarderActivity extends AppCompatActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {
public abstract class OnboarderActivity extends AppCompatActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {

private Integer[] colors;
private LinearLayout llIndicators;
private CircleIndicatorView circleIndicatorView;
private ViewPager vpOnboarderPager;
private OnboarderAdapter onboarderAdapter;
private ImageButton ibNext;
Expand All @@ -31,10 +30,8 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_onboarder);
setStatusBackgroundColor();

getSupportActionBar().hide();

llIndicators = (LinearLayout) findViewById(R.id.ll_indicators);
circleIndicatorView = (CircleIndicatorView) findViewById(R.id.circle_indicator_view);
ibNext = (ImageButton) findViewById(R.id.ib_next);
btnSkip = (Button) findViewById(R.id.btn_skip);
btnFinish = (Button) findViewById(R.id.btn_finish);
Expand All @@ -49,14 +46,16 @@ protected void onCreate(Bundle savedInstanceState) {
public void setOnboardPagesReady(List<OnboarderPage> pages) {
onboarderAdapter = new OnboarderAdapter(pages, getSupportFragmentManager());
vpOnboarderPager.setAdapter(onboarderAdapter);
setPageIndicators(pages.size());
colors = ColorsArrayBuilder.getPageBackgroundColors(this, pages);
circleIndicatorView.setPageIndicators(pages.size());
}

public void setPageIndicators(int quantity) {
for (int i = 0; i < quantity; i++) {
llIndicators.addView(new CircleIndicatorView(this));
}
public void setInactiveIndicatorColor(int color) {
circleIndicatorView.setInactiveIndicatorColor(color);
}

public void setActiveIndicatorColor(int color) {
circleIndicatorView.setActiveIndicatorColor(color);
}

public void setStatusBackgroundColor() {
Expand All @@ -75,6 +74,9 @@ public void onClick(View v) {
vpOnboarderPager.setCurrentItem(vpOnboarderPager.getCurrentItem() + 1);
} else if (i == R.id.btn_skip) {
vpOnboarderPager.setCurrentItem(onboarderAdapter.getCount());
onSkipButtonPressed();
} else if (i == R.id.btn_finish) {
onFinishButtonPressed();
}
}

Expand All @@ -90,6 +92,7 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse
@Override
public void onPageSelected(int position) {
int lastPagePosition = onboarderAdapter.getCount() - 1;
circleIndicatorView.setCurrentPage(position);
ibNext.setVisibility(position == lastPagePosition ? View.GONE : View.VISIBLE);
btnFinish.setVisibility(position == lastPagePosition ? View.VISIBLE : View.GONE);
}
Expand All @@ -99,4 +102,7 @@ public void onPageScrollStateChanged(int state) {

}

abstract public void onSkipButtonPressed();
abstract public void onFinishButtonPressed();

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,117 @@

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.shapes.OvalShape;
import android.support.annotation.ColorRes;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.chyrta.onboarder.R;

public class CircleIndicatorView extends View {

OvalShape oval;
private Paint activeIndicatorPaint;
private Paint inactiveIndicatorPaint;
private int radius;
private int size;
private int position;
private int indicatorsCount;

public CircleIndicatorView(Context context) {
super(context);
Canvas canvas = new Canvas();
Paint paint = new Paint();
oval = new OvalShape();
oval.draw(canvas, paint);
init();
}

public CircleIndicatorView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public CircleIndicatorView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

private void init() {
activeIndicatorPaint = new Paint();
activeIndicatorPaint.setColor(getResources().getColor(R.color.active_indicator, null));
activeIndicatorPaint.setAntiAlias(true);
inactiveIndicatorPaint = new Paint();
inactiveIndicatorPaint.setColor(getResources().getColor(R.color.inactive_indicator, null));
inactiveIndicatorPaint.setAntiAlias(true);
radius = getResources().getDimensionPixelSize(R.dimen.indicator_size);
size = radius * 2;
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
for (int i = 0; i < indicatorsCount; i++) {
canvas.drawCircle(radius + (size * i), radius, radius / 2, inactiveIndicatorPaint);
}
canvas.drawCircle(radius + (size * position), radius, radius / 2, activeIndicatorPaint);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}

@Override
public void setLayoutParams(ViewGroup.LayoutParams params) {
super.setLayoutParams(params);
params.height = 8;
params.width = 8;
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
}

public void setCurrentPage(int position) {
this.position = position;
invalidate();
}

public void setPageIndicators(int size) {
this.indicatorsCount = size;
invalidate();
}

private int measureWidth(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);

if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = size * indicatorsCount;
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}

private int measureHeight(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);

if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = 2 * radius + getPaddingTop() + getPaddingBottom();
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}

public void setInactiveIndicatorColor(@ColorRes int color) {
inactiveIndicatorPaint.setColor(getResources().getColor(color, null));
invalidate();
}

public void setActiveIndicatorColor(@ColorRes int color) {
activeIndicatorPaint.setColor(getResources().getColor(color, null));
invalidate();
}

}
5 changes: 5 additions & 0 deletions onboarder/src/main/res/layout/activity_onboarder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
android:layout_gravity="center"
android:orientation="horizontal">

<com.chyrta.onboarder.views.CircleIndicatorView
android:id="@+id/circle_indicator_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

<Button
Expand Down
2 changes: 2 additions & 0 deletions onboarder/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
<resources>
<color name="white">@android:color/white</color>
<color name="black_transparent">#14000000</color>
<color name="active_indicator">@android:color/white</color>
<color name="inactive_indicator">#4D000000</color>
</resources>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.chyrta.androidonboarder;

import android.os.Bundle;
import android.widget.Toast;

import com.chyrta.onboarder.OnboarderActivity;
import com.chyrta.onboarder.OnboarderAdapter;
Expand All @@ -27,14 +28,30 @@ protected void onCreate(Bundle savedInstanceState) {
onboarderPage3.setTitleColor(R.color.colorPrimaryDark);
onboarderPage3.setDescriptionColor(R.color.colorAccent);
onboarderPage3.setBackgroundColor(R.color.colorAccent);
OnboarderPage onboarderPage4 = new OnboarderPage("Title 4", "Description 4");
onboarderPage4.setTitleColor(R.color.colorPrimaryDark);
onboarderPage4.setDescriptionColor(R.color.colorAccent);
onboarderPage4.setBackgroundColor(R.color.colorPrimaryDark);

List<OnboarderPage> pages = new ArrayList<>();

pages.add(onboarderPage1);
pages.add(onboarderPage2);
pages.add(onboarderPage3);
pages.add(onboarderPage4);

setOnboardPagesReady(pages);

}

@Override
public void onSkipButtonPressed() {
Toast.makeText(this, "Skip button was pressed!", Toast.LENGTH_SHORT).show();
}

@Override
public void onFinishButtonPressed() {
Toast.makeText(this, "Finish button was pressed", Toast.LENGTH_SHORT).show();
}

}

0 comments on commit 5473a29

Please sign in to comment.