Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,18 @@ proguard/

# Android Studio captures folder
captures/
.idea/.name
.idea/compiler.xml
.idea/encodings.xml
.idea/gradle.xml
.idea/misc.xml
.idea/modules.xml
.idea/runConfigurations.xml
.idea/workspace.xml
.idea/copyright/profiles_settings.xml
.idea/libraries/appcompat_v7_22_0_0.xml
.idea/libraries/support_annotations_22_0_0.xml
.idea/libraries/support_v4_22_0_0.xml
android-segmented-control.iml
demo/demo.iml
library/library.iml
5 changes: 4 additions & 1 deletion demo/src/main/res/layout/fragment_sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
segmentedgroup:sc_border_width="1dp"
segmentedgroup:sc_corner_radius="10dp"
segmentedgroup:sc_tint_color="#FFEB3B"
segmentedgroup:sc_checked_text_color="#7C4DFF">
segmentedgroup:sc_checked_text_color="#7C4DFF"
segmentedgroup:sc_text_color="#FF0000"
segmentedgroup:sc_unchecked_tint_color="#33FF55"
>

<RadioButton
android:id="@+id/button21"
Expand Down
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apply plugin: 'android-library'
apply plugin: 'com.android.library'

android {
compileSdkVersion 21
Expand All @@ -23,4 +23,4 @@ dependencies {
}

// Used to push in maven
apply from: '../maven_push.gradle'
//apply from: '../maven_push.gradle'
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ public class SegmentedGroup extends RadioGroup {
private int mMarginDp;
private Resources resources;
private int mTintColor;
private int mUnCheckedTintColor;
private int mTextColor = Color.CYAN;
private int mCheckedTextColor = Color.WHITE;
private LayoutSelector mLayoutSelector;
private Float mCornerRadius;
private boolean mDrawCheckedDrawable = true;

public SegmentedGroup(Context context) {
super(context);
resources = getResources();
mTintColor = resources.getColor(R.color.radio_button_selected_color);
mUnCheckedTintColor = resources.getColor(R.color.radio_button_unselected_color);
mMarginDp = (int) getResources().getDimension(R.dimen.radio_button_stroke_border);
mCornerRadius = getResources().getDimension(R.dimen.radio_button_conner_radius);
mLayoutSelector = new LayoutSelector(mCornerRadius);
Expand All @@ -54,6 +58,14 @@ private void initAttrs(AttributeSet attrs) {
R.styleable.SegmentedGroup_sc_tint_color,
getResources().getColor(R.color.radio_button_selected_color));

mUnCheckedTintColor = typedArray.getColor(
R.styleable.SegmentedGroup_sc_unchecked_tint_color,
getResources().getColor(R.color.radio_button_unselected_color));

mTextColor = typedArray.getColor(
R.styleable.SegmentedGroup_sc_text_color,
getResources().getColor(android.R.color.holo_blue_bright));

mCheckedTextColor = typedArray.getColor(
R.styleable.SegmentedGroup_sc_checked_text_color,
getResources().getColor(android.R.color.white));
Expand All @@ -67,6 +79,7 @@ public SegmentedGroup(Context context, AttributeSet attrs) {
super(context, attrs);
resources = getResources();
mTintColor = resources.getColor(R.color.radio_button_selected_color);
mUnCheckedTintColor = resources.getColor(R.color.radio_button_unselected_color);
mMarginDp = (int) getResources().getDimension(R.dimen.radio_button_stroke_border);
mCornerRadius = getResources().getDimension(R.dimen.radio_button_conner_radius);
initAttrs(attrs);
Expand All @@ -80,6 +93,12 @@ protected void onFinishInflate() {
updateBackground();
}

public void setTextColor(int textColor)
{
mTextColor = textColor;
updateBackground();
}

public void setTintColor(int tintColor) {
mTintColor = tintColor;
updateBackground();
Expand All @@ -91,6 +110,18 @@ public void setTintColor(int tintColor, int checkedTextColor) {
updateBackground();
}


public void setUncheckedTintColor(int uncheckTintColor, int textColor) {
mUnCheckedTintColor = uncheckTintColor;
mTextColor = textColor;
updateBackground();
}

public void setDrawCheckedDrawable(boolean b) {
mDrawCheckedDrawable = b;
updateBackground();
}

public void updateBackground() {
int count = super.getChildCount();
for (int i = 0; i < count; i++) {
Expand Down Expand Up @@ -120,15 +151,23 @@ private void updateBackground(View view) {
{android.R.attr.state_pressed},
{-android.R.attr.state_pressed, -android.R.attr.state_checked},
{-android.R.attr.state_pressed, android.R.attr.state_checked}},
new int[]{Color.GRAY, mTintColor, mCheckedTextColor});
new int[]{Color.GRAY, mTextColor, mCheckedTextColor});
((Button) view).setTextColor(colorStateList);

//Redraw with tint color
Drawable checkedDrawable = resources.getDrawable(checked).mutate();
Drawable uncheckedDrawable = resources.getDrawable(unchecked).mutate();
((GradientDrawable) checkedDrawable).setColor(mTintColor);

if(mDrawCheckedDrawable) {
((GradientDrawable) checkedDrawable).setColor(mTintColor);
}

//sets the uncheckedTintColor
((GradientDrawable) uncheckedDrawable).setColor(mUnCheckedTintColor);

((GradientDrawable) checkedDrawable).setStroke(mMarginDp, mTintColor);
((GradientDrawable) uncheckedDrawable).setStroke(mMarginDp, mTintColor);

//Set proper radius
((GradientDrawable) checkedDrawable).setCornerRadii(mLayoutSelector.getChildRadii(view));
((GradientDrawable) uncheckedDrawable).setCornerRadii(mLayoutSelector.getChildRadii(view));
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/res/drawable/button_text_color.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#DDd4d4d4" />
<item android:state_pressed="false" android:state_checked="false" android:color="@color/radio_button_selected_color" />
<item android:state_pressed="false" android:state_checked="false" android:color="@color/radio_button_text_color" />
<item android:state_pressed="false" android:state_checked="true" android:color="#fff2f2f2" />
</selector>
7 changes: 6 additions & 1 deletion library/src/main/res/drawable/radio_checked.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
android:shape="rectangle"
android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
<solid android:color="@color/radio_button_selected_color" />
<!--<solid android:color="@color/radio_button_selected_color" />-->
<gradient
android:type="linear"
android:startColor="#ff4dd512"
android:endColor="#ff2a9b02"
android:angle="270"/>
<stroke
android:width="@dimen/radio_button_stroke_border"
android:color="@color/radio_button_selected_color" />
Expand Down
12 changes: 8 additions & 4 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SegmentedGroup">
<attr name="sc_corner_radius" format="dimension" />
<attr name="sc_border_width" format="dimension" />
<attr name="sc_tint_color" format="color" />
<attr name="sc_checked_text_color" format="color" />
<attr name="sc_corner_radius" format="dimension"/>
<attr name="sc_border_width" format="dimension"/>
<attr name="sc_unchecked_tint_color" format="color"/>
<attr name="sc_tint_color" format="color"/>
<attr name="sc_checked_text_color" format="color"/>
<attr name="sc_text_color" format="color"/>


</declare-styleable>

</resources>
1 change: 1 addition & 0 deletions library/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="radio_button_selected_color">#ff33b5e5</color>
<color name="radio_button_text_color">#ff33b5e5</color>
<color name="radio_button_unselected_color">@android:color/transparent</color>
</resources>