Skip to content

Commit 99ac0fc

Browse files
committedApr 29, 2017
Minor fixes on attributes naming convention and start writing readme
1 parent 2bc37fa commit 99ac0fc

20 files changed

+239
-40
lines changed
 

‎README.md

+94
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,96 @@
11
# CompoundButtonGroup
2+
3+
![Alt text](https://img.shields.io/badge/license-MIT-green.svg?style=flat)
4+
25
An Android library to easily implement compound buttons
6+
7+
## Installation
8+
9+
#### Gradle
10+
Add Gradle dependency:
11+
12+
```groovy
13+
dependencies {
14+
compile 'us.belka:androidtoggleswitch:1.2.2'
15+
}
16+
```
17+
18+
#### Maven
19+
```xml
20+
<dependency>
21+
<groupId>us.belka</groupId>
22+
<artifactId>androidtoggleswitch</artifactId>
23+
<version>1.2.2</version>
24+
<type>pom</type>
25+
</dependency>
26+
```
27+
28+
## Usage
29+
30+
#### Check Box
31+
32+
![Sample of check_box](docs/screen/check_box.gif)
33+
34+
```xml
35+
<com.llollox.androidprojects.compoundbuttongroup.CompoundButtonGroup
36+
android:layout_width="match_parent"
37+
android:layout_height="wrap_content"
38+
app:entries="@array/planets"
39+
app:compoundType="check_box"/>
40+
```
41+
42+
#### Check Box Grid (2 Cols, Label After)
43+
44+
![Sample of check_box](docs/screen/check_box_2cols_label_after.gif)
45+
46+
```xml
47+
<com.llollox.androidprojects.compoundbuttongroup.CompoundButtonGroup
48+
android:layout_width="match_parent"
49+
android:layout_height="wrap_content"
50+
app:entries="@array/planets"
51+
app:compoundType="check_box"
52+
app:numCols="2"
53+
app:labelOrder="after"/>
54+
```
55+
56+
57+
#### Radio
58+
59+
![Sample of radio](docs/screen/radio.gif)
60+
61+
```xml
62+
<com.llollox.androidprojects.compoundbuttongroup.CompoundButtonGroup
63+
android:layout_width="match_parent"
64+
android:layout_height="wrap_content"
65+
app:entries="@array/planets"
66+
app:compoundType="radio"
67+
app:labelOrder="after"/>
68+
```
69+
70+
#### Radio Grid (3 Cols)
71+
72+
![Sample of check_box](docs/screen/radio_3cols.gif)
73+
74+
```xml
75+
<com.llollox.androidprojects.compoundbuttongroup.CompoundButtonGroup
76+
android:layout_width="match_parent"
77+
android:layout_height="wrap_content"
78+
app:entries="@array/planets"
79+
app:compoundType="radio"
80+
app:numCols="3"
81+
app:labelOrder="after"/>
82+
```
83+
84+
85+
86+
## Customization
87+
88+
It is possible to customize the buttons applying the following options:
89+
90+
91+
| Option Name | Format | Description |
92+
| ---------------- | ---------------------- | ----------------------------- |
93+
| app:compoundType | `check_box` or `radio` | The type of the compound button. By default is `check_box`. |
94+
| app:entries | `array` | String array of the entries of the compound button group. |
95+
| app:labelOrder | `before` or `after` | This determines if the label is before or after the compound button. By default is `before`. |
96+
| app:numCols | `int` | Setting this parameter the compound buttons are shown as a grid and it indicates the number of cols of the grid. |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.llollox.androidprojects.compoundbuttongroupsample;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.Nullable;
5+
import android.support.v4.app.Fragment;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
import android.widget.Button;
10+
import android.widget.RelativeLayout;
11+
import android.widget.Toast;
12+
13+
import com.llollox.androidprojects.compoundbuttongroup.CompoundButtonGroup;
14+
15+
import java.util.ArrayList;
16+
17+
/**
18+
* Created by lorenzorigato on 28/04/2017.
19+
*/
20+
21+
public class CompoundButtonGroupFragment extends Fragment {
22+
23+
private static class Argument {
24+
private static final String LAYOUT_ID = "layoutId";
25+
}
26+
27+
public static CompoundButtonGroupFragment newInstance(int layoutId) {
28+
Bundle args = new Bundle();
29+
args.putInt(Argument.LAYOUT_ID, layoutId);
30+
CompoundButtonGroupFragment compoundButtonGroupFragment = new CompoundButtonGroupFragment();
31+
compoundButtonGroupFragment.setArguments(args);
32+
return compoundButtonGroupFragment;
33+
}
34+
35+
private CompoundButtonGroup compoundButtonGroup;
36+
private String[] planets;
37+
38+
@Nullable
39+
@Override
40+
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
41+
42+
int layoutId;
43+
if (getArguments() != null && getArguments().containsKey(Argument.LAYOUT_ID)) {
44+
layoutId = getArguments().getInt(Argument.LAYOUT_ID);
45+
}
46+
else {
47+
throw new RuntimeException("A compound button group fragment requires a layout id as argument");
48+
}
49+
50+
planets = getResources().getStringArray(R.array.planets);
51+
52+
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
53+
54+
RelativeLayout relativeLayout = (RelativeLayout) layoutInflater.inflate(
55+
R.layout.compound_button_group_fragment, container, false);
56+
57+
compoundButtonGroup = (CompoundButtonGroup) layoutInflater.inflate(layoutId, relativeLayout, false);
58+
59+
60+
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) compoundButtonGroup.getLayoutParams();
61+
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
62+
params.addRule(RelativeLayout.ABOVE, R.id.get_value_btn);
63+
64+
compoundButtonGroup.setOnButtonSelectedListener(new CompoundButtonGroup.OnButtonSelectedListener() {
65+
@Override
66+
public void onButtonSelected(int position, boolean isChecked) {
67+
String planet = planets[position];
68+
String checked = getString(isChecked ? R.string.checked : R.string.unchecked);
69+
Toast.makeText(getActivity(), checked + ": " + planet, Toast.LENGTH_SHORT).show();
70+
}
71+
});
72+
73+
Button getValueBtn = (Button) relativeLayout.findViewById(R.id.get_value_btn);
74+
getValueBtn.setOnClickListener(new View.OnClickListener() {
75+
@Override
76+
public void onClick(View v) {
77+
String checked = getString(R.string.checked);
78+
ArrayList<String> checkedPlanets = new ArrayList<>();
79+
for (int position : compoundButtonGroup.getSelectedPositions()) {
80+
checkedPlanets.add(planets[position]);
81+
}
82+
Toast.makeText(getActivity(),
83+
checked + ": " + String.valueOf(checkedPlanets),
84+
Toast.LENGTH_SHORT).show();
85+
}
86+
});
87+
88+
relativeLayout.addView(compoundButtonGroup, 0);
89+
90+
return relativeLayout;
91+
}
92+
}

‎compoundbuttongroup-sample/src/main/java/com/llollox/androidprojects/compoundbuttongroupsample/MainActivity.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,12 @@ protected void onCreate(Bundle savedInstanceState) {
3838
/** Called when a drawer has settled in a completely closed state. */
3939
public void onDrawerClosed(View view) {
4040
super.onDrawerClosed(view);
41-
// getActionBar().setTitle(mTitle);
4241
invalidateOptionsMenu();
4342
}
4443

4544
/** Called when a drawer has settled in a completely open state. */
4645
public void onDrawerOpened(View drawerView) {
4746
super.onDrawerOpened(drawerView);
48-
// getActionBar().setTitle(mDrawerTitle);
4947
invalidateOptionsMenu();
5048
}
5149
};
@@ -78,11 +76,11 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
7876
private Fragment getFragmentByPosition(int position) {
7977
switch (position) {
8078
case 0: return CompoundButtonGroupFragment.newInstance(R.layout.check_boxes_fragment);
81-
case 1: return CompoundButtonGroupFragment.newInstance(R.layout.check_boxes_label_last_fragment);
79+
case 1: return CompoundButtonGroupFragment.newInstance(R.layout.check_boxes_label_after_fragment);
8280
case 2: return CompoundButtonGroupFragment.newInstance(R.layout.check_boxes_grid_2cols_fragment);
8381
case 3: return CompoundButtonGroupFragment.newInstance(R.layout.check_boxes_grid_3cols_fragment);
8482
case 4: return CompoundButtonGroupFragment.newInstance(R.layout.radio_fragment);
85-
case 5: return CompoundButtonGroupFragment.newInstance(R.layout.radio_label_last_fragment);
83+
case 5: return CompoundButtonGroupFragment.newInstance(R.layout.radio_label_after_fragment);
8684
case 6: return CompoundButtonGroupFragment.newInstance(R.layout.radio_grid_2cols_fragment);
8785
case 7: return CompoundButtonGroupFragment.newInstance(R.layout.radio_grid_3cols_fragment);
8886
default: throw new RuntimeException("Unsupported position");

‎compoundbuttongroup-sample/src/main/res/layout/check_boxes_fragment.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
android:layout_width="match_parent"
66
android:layout_height="wrap_content"
77
app:entries="@array/planets"
8-
app:compound_type="check_box"/>
8+
app:compoundType="check_box"/>

‎compoundbuttongroup-sample/src/main/res/layout/check_boxes_grid_2cols_fragment.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
android:layout_width="match_parent"
66
android:layout_height="wrap_content"
77
app:entries="@array/planets"
8-
app:compound_type="check_box"
9-
app:num_cols="2"
10-
app:label_order="last"/>
8+
app:compoundType="check_box"
9+
app:numCols="2"
10+
app:labelOrder="after"/>

‎compoundbuttongroup-sample/src/main/res/layout/check_boxes_grid_3cols_fragment.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
android:layout_width="match_parent"
66
android:layout_height="wrap_content"
77
app:entries="@array/planets"
8-
app:compound_type="check_box"
9-
app:num_cols="3"
10-
app:label_order="last"/>
8+
app:compoundType="check_box"
9+
app:numCols="3"
10+
app:labelOrder="after"/>

‎compoundbuttongroup-sample/src/main/res/layout/check_boxes_label_last_fragment.xml ‎compoundbuttongroup-sample/src/main/res/layout/check_boxes_label_after_fragment.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
android:layout_width="match_parent"
66
android:layout_height="wrap_content"
77
app:entries="@array/planets"
8-
app:compound_type="check_box"
9-
app:label_order="last"/>
8+
app:compoundType="check_box"
9+
app:labelOrder="after"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent">
5+
6+
<Button
7+
android:id="@+id/get_value_btn"
8+
android:layout_width="match_parent"
9+
android:layout_height="wrap_content"
10+
android:text="@string/get_value"
11+
android:layout_alignParentBottom="true"
12+
android:theme="@style/PrimaryButton"/>
13+
14+
</RelativeLayout>

‎compoundbuttongroup-sample/src/main/res/layout/radio_fragment.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
android:layout_width="match_parent"
66
android:layout_height="wrap_content"
77
app:entries="@array/planets"
8-
app:compound_type="radio"/>
8+
app:compoundType="radio"/>

‎compoundbuttongroup-sample/src/main/res/layout/radio_grid_2cols_fragment.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
android:layout_width="match_parent"
66
android:layout_height="wrap_content"
77
app:entries="@array/planets"
8-
app:compound_type="radio"
9-
app:num_cols="2"
10-
app:label_order="last"/>
8+
app:compoundType="radio"
9+
app:numCols="2"
10+
app:labelOrder="after"/>

‎compoundbuttongroup-sample/src/main/res/layout/radio_grid_3cols_fragment.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
android:layout_width="match_parent"
66
android:layout_height="wrap_content"
77
app:entries="@array/planets"
8-
app:compound_type="radio"
9-
app:num_cols="3"
10-
app:label_order="last"/>
8+
app:compoundType="radio"
9+
app:numCols="3"
10+
app:labelOrder="after"/>

‎compoundbuttongroup-sample/src/main/res/layout/radio_label_last_fragment.xml ‎compoundbuttongroup-sample/src/main/res/layout/radio_label_after_fragment.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
android:layout_width="match_parent"
66
android:layout_height="wrap_content"
77
app:entries="@array/planets"
8-
app:compound_type="radio"
9-
app:label_order="last"/>
8+
app:compoundType="radio"
9+
app:labelOrder="after"/>

‎compoundbuttongroup-sample/src/main/res/values/strings.xml

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
<string name="close_drawer">Close Drawer</string>
66

77
<string name="get_value">Get value</string>
8+
<string name="checked">Checked</string>
9+
<string name="unchecked">Unchecked</string>
810

911
<array name="menu_titles">
1012
<item>Check Boxes</item>
11-
<item>Check Boxes (Label Last)</item>
13+
<item>Check Boxes (Label After)</item>
1214
<item>Check Boxes Grid (2 cols)</item>
1315
<item>Check Boxes Grid (3 cols)</item>
1416
<item>Radio</item>
15-
<item>Radio (Label Last)</item>
17+
<item>Radio (Label After)</item>
1618
<item>Radio Grid (2 cols)</item>
1719
<item>Radio Grid (3 cols)</item>
1820
</array>

‎compoundbuttongroup/src/main/java/com/llollox/androidprojects/compoundbuttongroup/CompoundButtonGroup.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public interface OnButtonSelectedListener {
2424
}
2525

2626
private FullWidthCompoundButton.CompoundType compoundType = FullWidthCompoundButton.CompoundType.CHECK_BOX;
27-
private FullWidthCompoundButton.LabelOrder labelOrder = FullWidthCompoundButton.LabelOrder.FIRST;
27+
private FullWidthCompoundButton.LabelOrder labelOrder = FullWidthCompoundButton.LabelOrder.BEFORE;
2828
private ArrayList<FullWidthCompoundButton> buttons = new ArrayList<>();
2929
private int numCols = 1;
3030

@@ -49,13 +49,13 @@ public CompoundButtonGroup(Context context, @Nullable AttributeSet attrs) {
4949

5050
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CompoundButtonGroup, 0, 0);
5151
try {
52-
int compoundTypeInt = a.getInteger(R.styleable.CompoundButtonGroup_compound_type, 0);
52+
int compoundTypeInt = a.getInteger(R.styleable.CompoundButtonGroup_compoundType, 0);
5353
this.compoundType = getCompoundType(compoundTypeInt);
5454

55-
int labelOrderInt = a.getInteger(R.styleable.CompoundButtonGroup_label_order, 0);
55+
int labelOrderInt = a.getInteger(R.styleable.CompoundButtonGroup_labelOrder, 0);
5656
this.labelOrder = getLabelOrder(labelOrderInt);
5757

58-
numCols = a.getInteger(R.styleable.CompoundButtonGroup_num_cols, 1);
58+
numCols = a.getInteger(R.styleable.CompoundButtonGroup_numCols, 1);
5959

6060
CharSequence[] entries = a.getTextArray(R.styleable.CompoundButtonGroup_entries);
6161
if (entries != null) {
@@ -79,8 +79,8 @@ private FullWidthCompoundButton.CompoundType getCompoundType (int compoundTypeIn
7979

8080
private FullWidthCompoundButton.LabelOrder getLabelOrder (int labelOrder) {
8181
switch (labelOrder) {
82-
case 0: return FullWidthCompoundButton.LabelOrder.FIRST;
83-
case 1: return FullWidthCompoundButton.LabelOrder.LAST;
82+
case 0: return FullWidthCompoundButton.LabelOrder.BEFORE;
83+
case 1: return FullWidthCompoundButton.LabelOrder.AFTER;
8484
default: throw new RuntimeException("Unrecognized label order");
8585
}
8686
}

‎compoundbuttongroup/src/main/java/com/llollox/androidprojects/compoundbuttongroup/FullWidthCompoundButton.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public interface Listener {
2525
}
2626

2727
public enum LabelOrder {
28-
FIRST, LAST
28+
BEFORE, AFTER
2929
}
3030

3131
public enum CompoundType {
@@ -34,7 +34,7 @@ public enum CompoundType {
3434

3535
private TextView textView;
3636
private CompoundButton button;
37-
private LabelOrder labelOrder = LabelOrder.FIRST;
37+
private LabelOrder labelOrder = LabelOrder.BEFORE;
3838
private CompoundType viewType = CompoundType.CHECK_BOX;
3939
private Context context;
4040
private Listener listener;
@@ -117,7 +117,7 @@ private void refresh () {
117117

118118
private void addOrderedViews(LabelOrder labelOrder) {
119119
switch (labelOrder) {
120-
case FIRST:
120+
case BEFORE:
121121
LinearLayout.LayoutParams params = new LayoutParams(
122122
LayoutParams.MATCH_PARENT,
123123
LayoutParams.MATCH_PARENT);
@@ -128,12 +128,11 @@ private void addOrderedViews(LabelOrder labelOrder) {
128128
addView(button);
129129
break;
130130

131-
case LAST:
131+
case AFTER:
132132
addView(button);
133133
LinearLayout.LayoutParams paramss = new LayoutParams(
134134
LayoutParams.WRAP_CONTENT,
135135
LayoutParams.WRAP_CONTENT);
136-
// textView.setGravity(Gravity.CENTER_VERTICAL);
137136
textView.setLayoutParams(paramss);
138137
addView(textView);
139138
break;

‎compoundbuttongroup/src/main/res/values/attrs.xml

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
<declare-styleable name="CompoundButtonGroup">
55
<attr name="entries" format="reference" />
6-
<attr name="num_cols" format="integer" />
7-
<attr name="label_order" format="enum">
8-
<enum name="first" value="0" />
9-
<enum name="last" value="1" />
6+
<attr name="numCols" format="integer" />
7+
<attr name="labelOrder" format="enum">
8+
<enum name="before" value="0" />
9+
<enum name="after" value="1" />
1010
</attr>
11-
<attr name="compound_type" format="enum">
11+
<attr name="compoundType" format="enum">
1212
<enum name="check_box" value="0" />
1313
<enum name="radio" value="1" />
1414
</attr>

‎docs/screen/check_box.gif

231 KB
Loading
228 KB
Loading

‎docs/screen/radio.gif

223 KB
Loading

‎docs/screen/radio_3cols.gif

206 KB
Loading

0 commit comments

Comments
 (0)
Please sign in to comment.