Skip to content

Commit

Permalink
Merge pull request #20 from adroitandroid/customize
Browse files Browse the repository at this point in the history
Customizations
  • Loading branch information
Fisk Debug authored Feb 13, 2017
2 parents a84cd0f + 79714bf commit f926180
Show file tree
Hide file tree
Showing 15 changed files with 384 additions and 93 deletions.
43 changes: 41 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![Release](https://jitpack.io/v/fiskurgit/ChipCloud.svg)](https://jitpack.io/#fiskurgit/ChipCloud) [![Build Status](https://travis-ci.org/fiskurgit/ChipCloud.svg?branch=master)](https://travis-ci.org/fiskurgit/ChipCloud) [![license](https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000)](https://github.com/fiskurgit/ChipCloud/blob/master/LICENSE) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/55d686ee370d494b9f7f7e6636c0c294)](https://www.codacy.com/app/fiskur/ChipCloud?utm_source=github.com&utm_medium=referral&utm_content=fiskurgit/ChipCloud&utm_campaign=Badge_Grade) [![Gitter](https://img.shields.io/gitter/room/nwjs/nw.js.svg?maxAge=2592000)](https://gitter.im/fiskurgit/fiskur)
<a href="http://www.methodscount.com/?lib=com.github.fiskurgit%3AChipCloud%3A2.1.0"><img src="https://img.shields.io/badge/Size-27 KB-e91e63.svg"/></a>

ChipCloud is an Android view (very) quickly knocked up for a larger hackathon project, it creates a wrapping cloud of '[Chips](https://www.google.com/design/spec/components/chips.html)'. Basic demo [available on the Play Store](https://play.google.com/store/apps/details?id=eu.fiskur.chipclouddemo) - the code is badly written and while I'm happy people are using it please bear in mind I have little time to maintain and refactor the source.
ChipCloud is an Android view (very) quickly knocked up for a larger hackathon project, it creates a wrapping cloud of '[Chips](https://www.google.com/design/spec/components/chips.html)'. Basic demo is [available on the Play Store](https://play.google.com/store/apps/details?id=eu.fiskur.chipclouddemo) - the code is badly written and while I'm happy people are using it please bear in mind I have little time to maintain and refactor the source.

![Trainer Sizes](images/trainer_sizes.png)

Expand Down Expand Up @@ -30,7 +30,13 @@ Configure in xml:
chipcloud:deselectTransitionMS="500"
chipcloud:selectTransitionMS="750"
chipcloud:labels="@array/labels"
chipcloud:selectMode="required"/>
chipcloud:selectMode="required"
chipcloud:allCaps="true"
chipcloud:gravity="staggered"
chipcloud:minHorizontalSpacing="32dp"
chipcloud:verticalSpacing="16dp"
chipcloud:textSize="14sp"
chipcloud:typeface="RobotoSlab-Regular.ttf"/> <!--path relative to assets folder-->
```
or in code:
```java
Expand All @@ -46,6 +52,12 @@ new ChipCloud.Configure()
.deselectTransitionMS(250)
.labels(someStringArray)
.mode(ChipCloud.Mode.MULTI)
.allCaps(false)
.gravity(ChipCloud.Gravity.CENTER)
.textSize(getResources().getDimensionPixelSize(R.dimen.default_textsize))
.verticalSpacing(getResources().getDimensionPixelSize(R.dimen.vertical_spacing))
.minHorizontalSpacing(getResources().getDimensionPixelSize(R.dimen.min_horizontal_spacing))
.typeface(Typeface.createFromAsset(getContext().getAssets(), "RobotoSlab-Regular.ttf"))
.chipListener(new ChipListener() {
@Override
public void chipSelected(int index) {
Expand All @@ -59,6 +71,10 @@ new ChipCloud.Configure()
.build();
```

Default value of textSize is equivalent to 13sp, of verticalSpacing is equivalent to 8dp, of minHorizontalSpacing is equivalent to 8dp, of allCaps is false.

All chips have a height of 32dp and a padding of 12dp on left and right within the chip, [as per material guidelines](https://www.google.com/design/spec/components/chips.html).

Add items dynamically too:
```java
chipCloud.addChip("Foo");
Expand All @@ -74,6 +90,15 @@ Set the selected index using ```chipCloud.setSelectedChip(2)```
Real-world example for shoe sizes:
![Shoe Sizes](images/wrapping_example.png)

Labels can also be updated in-place, e.g.

from ![Before](images/label_update_before.png) to ![After](images/label_update_after.png)
using ```update()``` as illustrated below

```java
new ChipCloud.Configure().chipCloud(binding.chipCloud).labels(newChipLabels).update();
```

## Modes

```java
Expand All @@ -84,6 +109,20 @@ public enum Mode {

The default mode is single choice (where it's valid to have no chip selected), if you want a RadioGroup manadatory style where once a chip is selected there must always be a selected item use ```chipCloud.setMode(ChipCloud.Mode.REQUIRED);``` (or set in xml or the builder). There's a multiple select mode too: ```chipCloud.setMode(ChipCloud.Mode.MULTIPLE);```. If you want to deactiviate selecting of chips you can set the select mode to ```chipCloud.setMode(ChipCloud.Mode.NONE);```.

## Gravity

```java
public enum Gravity {
LEFT, RIGHT, CENTER, STAGGERED
}
```

The default gravity is LEFT. CENTER and STAGGERED are similar except that CENTER leaves only minimum horizontal spacing between the chips whereas in STAGGERED chips occupy the available space equally while having at least minimum horizontal spacing between them.

| ![Left](images/gravity_left.png) | ![Right](images/gravity_right.png) | ![Center](images/gravity_center.png) | ![Staggered](images/gravity_staggered.png) |
|:---:|:---:|:---:|:---:|
| LEFT | RIGHT | CENTER | STAGGERED |

## Dependency

Add jitpack.io to your root build.gradle, eg:
Expand Down
Binary file added images/gravity_center.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/gravity_left.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/gravity_right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/gravity_staggered.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/label_update_after.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/label_update_before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 24
buildToolsVersion "23.0.3"
compileSdkVersion 25
buildToolsVersion '25.0.2'

defaultConfig {
minSdkVersion 14
targetSdkVersion 24
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
Expand All @@ -21,5 +21,5 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:appcompat-v7:25.1.0'
}
39 changes: 34 additions & 5 deletions library/src/main/java/eu/fiskur/chipcloud/Chip.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

public class Chip extends TextView implements View.OnClickListener {
public class Chip extends android.support.v7.widget.AppCompatTextView implements View.OnClickListener {

private int index = -1;
private boolean selected = false;
Expand Down Expand Up @@ -43,7 +44,9 @@ public Chip(Context context, AttributeSet attrs, int defStyleAttr) {
init();
}

public void initChip(Context context, int index, String label, int selectedColor, int selectedFontColor, int unselectedColor, int unselectedFontColor, ChipCloud.Mode mode) {
public void initChip(Context context, int index, String label, Typeface typeface, int textSizePx,
boolean allCaps, int selectedColor, int selectedFontColor, int unselectedColor,
int unselectedFontColor, ChipCloud.Mode mode) {

this.index = index;
this.selectedFontColor = selectedFontColor;
Expand Down Expand Up @@ -91,6 +94,14 @@ public void initChip(Context context, int index, String label, int selectedColor

setText(label);
unselect();

if (typeface != null) {
setTypeface(typeface);
}
setAllCaps(allCaps);
if (textSizePx > 0) {
setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizePx);
}
}

public void setLocked(boolean isLocked) {
Expand Down Expand Up @@ -167,6 +178,9 @@ public void deselect() {
public static class ChipBuilder {
private int index;
private String label;
private Typeface typeface;
private int textSizePx;
private boolean allCaps;
private int selectedColor;
private int selectedFontColor;
private int unselectedColor;
Expand Down Expand Up @@ -208,6 +222,21 @@ public ChipBuilder label(String label) {
return this;
}

public ChipBuilder typeface(Typeface typeface) {
this.typeface = typeface;
return this;
}

public ChipBuilder allCaps(boolean allCaps) {
this.allCaps = allCaps;
return this;
}

public ChipBuilder textSize(int textSizePx) {
this.textSizePx = textSizePx;
return this;
}

public ChipBuilder chipHeight(int chipHeight) {
this.chipHeight = chipHeight;
return this;
Expand Down Expand Up @@ -235,7 +264,8 @@ public ChipBuilder deselectTransitionMS(int deselectTransitionMS) {

public Chip build(Context context) {
Chip chip = (Chip) LayoutInflater.from(context).inflate(R.layout.chip, null);
chip.initChip(context, index, label, selectedColor, selectedFontColor, unselectedColor, unselectedFontColor, mode);
chip.initChip(context, index, label, typeface, textSizePx, allCaps, selectedColor,
selectedFontColor, unselectedColor, unselectedFontColor, mode);
chip.setSelectTransitionMS(selectTransitionMS);
chip.setDeselectTransitionMS(deselectTransitionMS);
chip.setChipListener(chipListener);
Expand All @@ -244,4 +274,3 @@ public Chip build(Context context) {
}
}
}

Loading

0 comments on commit f926180

Please sign in to comment.