Skip to content

Commit

Permalink
fix an issue with selected circle color programmatically
Browse files Browse the repository at this point in the history
  • Loading branch information
shuhart committed Sep 12, 2018
1 parent 1a29a0e commit 613535e
Show file tree
Hide file tree
Showing 10 changed files with 367 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Usage
-----

1. Add jcenter() to repositories block in your gradle file.
2. Add `implementation 'com.shuhart.stepview:stepview:1.3.2'` to your dependencies.
2. Add `implementation 'com.shuhart.stepview:stepview:1.3.3'` to your dependencies.
3. Add `StepView` into your layouts or view hierarchy.

Supported animations:
Expand Down
3 changes: 2 additions & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ android {
compileSdkVersion 27
defaultConfig {
applicationId "com.shuhart.stepview"
minSdkVersion 14
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
Expand All @@ -23,6 +23,7 @@ dependencies {
implementation "com.android.support:appcompat-v7:$supportVersion"
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation "com.android.support:recyclerview-v7:$supportVersion"
implementation "com.github.skydoves:colorpickerview:2.0.0"
implementation project(':stepview')
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
Expand Down
1 change: 1 addition & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<activity android:name=".examples.recyclerview.RecyclerViewExampleActivity" />
<activity android:name=".examples.scrollview.ScrollViewExampleActivity" />
<activity android:name=".examples.simple.SimpleActivity" />
<activity android:name=".examples.customise.CustomiseActivity"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
package com.shuhart.stepview.sample.examples.customise;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.shuhart.stepview.StepView;
import com.shuhart.stepview.sample.R;
import com.skydoves.colorpickerview.ColorEnvelope;
import com.skydoves.colorpickerview.ColorPickerDialog;
import com.skydoves.colorpickerview.listeners.ColorEnvelopeListener;

import java.util.ArrayList;
import java.util.List;

public class CustomiseActivity extends AppCompatActivity {
private int currentStep = 0;
private StepView stepView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customise);
stepView = findViewById(R.id.step_view);
setupStepView();
setupCustomisation();
}

private void setupStepView() {
stepView.setOnStepClickListener(new StepView.OnStepClickListener() {
@Override
public void onStepClick(int step) {
Toast.makeText(CustomiseActivity.this, "Step " + step, Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (currentStep < stepView.getStepCount() - 1) {
currentStep++;
stepView.go(currentStep, true);
} else {
stepView.done(true);
}
}
});
findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (currentStep > 0) {
currentStep--;
}
stepView.done(false);
stepView.go(currentStep, true);
}
});
List<String> steps = new ArrayList<>();
for (int i = 0; i < 5; i++) {
steps.add("Step " + (i + 1));
}
stepView.setSteps(steps);
}

private void setupCustomisation() {
setupSelectCircleColorCustomisation();
setupSelectTextColorCustomisation();
}

private void setupSelectCircleColorCustomisation() {
final EditText selectedCircleColorEditText = findViewById(R.id.selected_circle_color_hex);
final ImageView selectedCircleColorSampleImageView = findViewById(R.id.selected_circle_color_sample);

selectedCircleColorEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// empty
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// empty
}

@Override
public void afterTextChanged(Editable s) {
String candidateColorHex = s.toString();
if (!candidateColorHex.contains("#")) {
candidateColorHex = "#" + candidateColorHex;
}
try {
int color = Color.parseColor(candidateColorHex);
selectedCircleColorSampleImageView.setImageDrawable(new ColorDrawable(color));
stepView.getState().selectedCircleColor(color).commit();
} catch (IllegalArgumentException ignore) {
}
}
});

int color = ContextCompat.getColor(this, R.color.stepview_circle_selected);
String hex = Integer.toHexString(color).toUpperCase().substring(2);
selectedCircleColorEditText.setText(hex);

selectedCircleColorSampleImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showColorPickerDialog(new ColorPickListener() {
@Override
public void onColorPicked(String hex) {
selectedCircleColorEditText.setText(hex);
}
});
}
});
}

private void setupSelectTextColorCustomisation() {
final EditText selectedTextColorEditText = findViewById(R.id.selected_text_color_hex);
final ImageView selectedTextColorSampleImageView = findViewById(R.id.selected_text_color_sample);

selectedTextColorEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// empty
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// empty
}

@Override
public void afterTextChanged(Editable s) {
String candidateColorHex = s.toString();
if (!candidateColorHex.contains("#")) {
candidateColorHex = "#" + candidateColorHex;
}
try {
int color = Color.parseColor(candidateColorHex);
selectedTextColorSampleImageView.setImageDrawable(new ColorDrawable(color));
stepView.getState().selectedTextColor(color).commit();
} catch (IllegalArgumentException ignore) {
}
}
});

int color = ContextCompat.getColor(this, R.color.stepview_circle_selected);
String hex = Integer.toHexString(color).toUpperCase().substring(2);
selectedTextColorEditText.setText(hex);

selectedTextColorSampleImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showColorPickerDialog(new ColorPickListener() {
@Override
public void onColorPicked(String hex) {
selectedTextColorEditText.setText(hex);
}
});
}
});
}

private void showColorPickerDialog(final ColorPickListener listener) {
ColorPickerDialog.Builder builder = new ColorPickerDialog.Builder(CustomiseActivity.this, AlertDialog.THEME_DEVICE_DEFAULT_DARK);
builder.setTitle("ColorPicker Dialog");
builder.setPositiveButton(getString(R.string.confirm), new ColorEnvelopeListener() {
@Override
public void onColorSelected(ColorEnvelope envelope, boolean fromUser) {
listener.onColorPicked(envelope.getHexCode());
}
});
builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
builder.show();
}

interface ColorPickListener {
void onColorPicked(String hex);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package com.shuhart.stepview.sample.main;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ScrollView;
import android.widget.Toast;

import com.shuhart.stepview.StepView;
import com.shuhart.stepview.sample.R;
import com.shuhart.stepview.sample.examples.customise.CustomiseActivity;
import com.shuhart.stepview.sample.examples.recyclerview.RecyclerViewExampleActivity;
import com.shuhart.stepview.sample.examples.scrollview.ScrollViewExampleActivity;
import com.shuhart.stepview.sample.examples.simple.SimpleActivity;
Expand All @@ -20,16 +16,16 @@

public class MainActivity extends AppCompatActivity {

@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MainAdapter adapter = new MainAdapter();
adapter.items = new ArrayList<MainAdapter.Item>() {{
add(MainAdapter.Item.SIMPLE);
add(MainAdapter.Item.RECYCLER_VIEW);
add(MainAdapter.Item.SCROLL_VIEW);
add(MainAdapter.Item.SIMPLE);
add(MainAdapter.Item.RECYCLER_VIEW);
add(MainAdapter.Item.SCROLL_VIEW);
add(MainAdapter.Item.CUSTOMISE);
}};
adapter.listener = new MainAdapter.ItemClickListener() {
@Override
Expand All @@ -44,6 +40,9 @@ public void onClick(MainAdapter.Item item) {
case SCROLL_VIEW:
startActivity(new Intent(MainActivity.this, ScrollViewExampleActivity.class));
break;
case CUSTOMISE:
startActivity(new Intent(MainActivity.this, CustomiseActivity.class));
break;
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ public int getItemCount() {
return items == null ? 0 : items.size();
}

public static class MainViewHolder extends RecyclerView.ViewHolder {
static class MainViewHolder extends RecyclerView.ViewHolder {

ItemClickListener listener;
Item item;

private TextView titleTextView;
private TextView subtitleTextView;

public MainViewHolder(View itemView) {
MainViewHolder(View itemView) {
super(itemView);
titleTextView = itemView.findViewById(R.id.title);
subtitleTextView = itemView.findViewById(R.id.subtitle);
Expand All @@ -83,6 +83,10 @@ void bind(final Item item) {
subtitle = subtitleTextView.getContext().getString(R.string.main_list_item_scrollview_subtitle);
break;
}
case CUSTOMISE:
title = titleTextView.getContext().getString(R.string.main_list_item_customise_title);
subtitle = subtitleTextView.getContext().getString(R.string.main_list_item_customise_subtitle);
break;
}
Assert.assertNotNull(title);
Assert.assertNotNull(subtitle);
Expand All @@ -106,6 +110,7 @@ public interface ItemClickListener {
public enum Item {
SIMPLE,
RECYCLER_VIEW,
SCROLL_VIEW
SCROLL_VIEW,
CUSTOMISE
}
}
Loading

0 comments on commit 613535e

Please sign in to comment.