Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
chungtv committed Mar 24, 2016
2 parents b4059af + c02bdd9 commit 46c6308
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 53 deletions.
52 changes: 49 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,52 @@
# Created by .ignore support plugin (hsz.mobi)
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio

*.iml

## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries

# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml

# Gradle:
# .idea/gradle.xml
# .idea/libraries

# Mongo Explorer plugin:
# .idea/mongoSettings.xml

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
### Android template
# Built application files
*.apk
*.ap_
Expand All @@ -15,7 +64,6 @@ gen/
# Gradle files
.gradle/
build/
*/build/

# Local configuration file (sdk path, etc)
local.properties
Expand All @@ -29,5 +77,3 @@ proguard/
# Android Studio Navigation editor temp files
.navigation/

# Android Studio captures folder
captures/
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {
}
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.1'
classpath 'com.android.tools.build:gradle:1.5.0'
}
}

Expand Down
17 changes: 4 additions & 13 deletions demo/build.gradle
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.1'
}
}

apply plugin: 'com.android.application'

android {
compileSdkVersion 21
buildToolsVersion '22.0.1'
compileSdkVersion 23
buildToolsVersion '23.0.2'
defaultConfig {
minSdkVersion 8
targetSdkVersion 21
targetSdkVersion 23
versionCode 1
versionName '1.0'
}
Expand All @@ -29,7 +20,7 @@ android {
}

dependencies {
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.android.support:appcompat-v7:23.1.1'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':library')
// compile 'info.hoang8f:android-segmented:1.0.6'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -14,7 +15,7 @@
import info.hoang8f.android.segmented.AwesomeRadioButton;
import info.hoang8f.android.segmented.SegmentedGroup;

public class SampleActivity extends ActionBarActivity {
public class SampleActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -75,19 +76,19 @@ public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.button21:
Toast.makeText(getActivity(), "One", Toast.LENGTH_SHORT).show();
return;
break;
case R.id.button22:
Toast.makeText(getActivity(), "Two", Toast.LENGTH_SHORT).show();
return;
break;
case R.id.button31:
Toast.makeText(getActivity(), "One", Toast.LENGTH_SHORT).show();
return;
break;
case R.id.button32:
Toast.makeText(getActivity(), "Two", Toast.LENGTH_SHORT).show();
return;
break;
case R.id.button33:
Toast.makeText(getActivity(), "Three", Toast.LENGTH_SHORT).show();
return;
break;
default:
// Nothing to do
}
Expand All @@ -98,10 +99,10 @@ public void onClick(View v) {
switch (v.getId()) {
case R.id.add_segmented:
addButton(segmented5);
return;
break;
case R.id.remove_segmented:
removeButton(segmented5);
return;
break;
default:
// Nothing to do
}
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/res/layout/fragment_sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
segmentedgroup:sc_border_width="1dp"
segmentedgroup:sc_corner_radius="10dp"
segmentedgroup:sc_tint_color="#FFEB3B"
segmentedgroup:sc_unchecked_tint_color="#ff0000"
segmentedgroup:sc_checked_text_color="#7C4DFF">

<RadioButton
Expand All @@ -52,7 +53,6 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="80dp"
android:background="#455A64"
android:gravity="center">

<info.hoang8f.android.segmented.SegmentedGroup
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apply plugin: 'android-library'
apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "22.0.1"
buildToolsVersion "23.0.2"

defaultConfig {
minSdkVersion 8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,51 @@
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.StateSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import java.util.HashMap;

public class SegmentedGroup extends RadioGroup {

private int mMarginDp;
private Resources resources;
private int mTintColor;
private int mUnCheckedTintColor;
private int mCheckedTextColor = Color.WHITE;
private LayoutSelector mLayoutSelector;
private Float mCornerRadius;
private OnCheckedChangeListener mCheckedChangeListener;
private HashMap<Integer, TransitionDrawable> mDrawableMap;
private int mLastCheckId;

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);
}

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);
mLayoutSelector = new LayoutSelector(mCornerRadius);
}

Expand Down Expand Up @@ -58,21 +79,14 @@ private void initAttrs(AttributeSet attrs) {
R.styleable.SegmentedGroup_sc_checked_text_color,
getResources().getColor(android.R.color.white));

mUnCheckedTintColor = typedArray.getColor(
R.styleable.SegmentedGroup_sc_unchecked_tint_color,
getResources().getColor(R.color.radio_button_unselected_color));
} finally {
typedArray.recycle();
}
}

public SegmentedGroup(Context context, AttributeSet attrs) {
super(context, attrs);
resources = getResources();
mTintColor = resources.getColor(R.color.radio_button_selected_color);
mMarginDp = (int) getResources().getDimension(R.dimen.radio_button_stroke_border);
mCornerRadius = getResources().getDimension(R.dimen.radio_button_conner_radius);
initAttrs(attrs);
mLayoutSelector = new LayoutSelector(mCornerRadius);
}

@Override
protected void onFinishInflate() {
super.onFinishInflate();
Expand All @@ -91,7 +105,13 @@ public void setTintColor(int tintColor, int checkedTextColor) {
updateBackground();
}

public void setUnCheckedTintColor(int unCheckedTintColor, int unCheckedTextColor) {
mUnCheckedTintColor = unCheckedTintColor;
updateBackground();
}

public void updateBackground() {
mDrawableMap = new HashMap<>();
int count = super.getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
Expand All @@ -117,10 +137,9 @@ private void updateBackground(View view) {
int unchecked = mLayoutSelector.getUnselected();
//Set text color
ColorStateList colorStateList = new ColorStateList(new int[][]{
{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});
{-android.R.attr.state_checked},
{android.R.attr.state_checked}},
new int[]{mTintColor, mCheckedTextColor});
((Button) view).setTextColor(colorStateList);

//Redraw with tint color
Expand All @@ -129,30 +148,65 @@ private void updateBackground(View view) {
((GradientDrawable) checkedDrawable).setColor(mTintColor);
((GradientDrawable) checkedDrawable).setStroke(mMarginDp, mTintColor);
((GradientDrawable) uncheckedDrawable).setStroke(mMarginDp, mTintColor);
((GradientDrawable) uncheckedDrawable).setColor(mUnCheckedTintColor);
//Set proper radius
((GradientDrawable) checkedDrawable).setCornerRadii(mLayoutSelector.getChildRadii(view));
((GradientDrawable) uncheckedDrawable).setCornerRadii(mLayoutSelector.getChildRadii(view));

//Create drawable
GradientDrawable maskDrawable = (GradientDrawable) resources.getDrawable(unchecked).mutate();
maskDrawable.setStroke(mMarginDp, mTintColor);
maskDrawable.setColor(mUnCheckedTintColor);
maskDrawable.setCornerRadii(mLayoutSelector.getChildRadii(view));
int maskColor = Color.argb(50, Color.red(mTintColor), Color.green(mTintColor), Color.blue(mTintColor));
maskDrawable.setColor(maskColor);
LayerDrawable pressedDrawable = new LayerDrawable(new Drawable[] {uncheckedDrawable, maskDrawable});

Drawable[] drawables = {uncheckedDrawable, checkedDrawable};
TransitionDrawable transitionDrawable = new TransitionDrawable(drawables);

StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[]{-android.R.attr.state_checked}, uncheckedDrawable);
stateListDrawable.addState(new int[]{android.R.attr.state_checked}, checkedDrawable);
stateListDrawable.addState(new int[] {-android.R.attr.state_checked, android.R.attr.state_pressed}, pressedDrawable);
stateListDrawable.addState(StateSet.WILD_CARD, transitionDrawable);

mDrawableMap.put(view.getId(), transitionDrawable);

//Set button background
if (Build.VERSION.SDK_INT >= 16) {
view.setBackground(stateListDrawable);
} else {
view.setBackgroundDrawable(stateListDrawable);
}

super.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
TransitionDrawable current = mDrawableMap.get(checkedId);
current.reverseTransition(200);
if (mLastCheckId != 0) {
TransitionDrawable last = mDrawableMap.get(mLastCheckId);
last.reverseTransition(200);
}
mLastCheckId = checkedId;

if (mCheckedChangeListener != null) {
mCheckedChangeListener.onCheckedChanged(group, checkedId);
}
}
});
}

@Override
public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
mCheckedChangeListener = listener;
}

/*
* This class is used to provide the proper layout based on the view.
* Also provides the proper radius for corners.
* The layout is the same for each selected left/top middle or right/bottom button.
* float tables for setting the radius via Gradient.setCornerRadii are used instead
* of multiple xml drawables.
*/
* This class is used to provide the proper layout based on the view.
* Also provides the proper radius for corners.
* The layout is the same for each selected left/top middle or right/bottom button.
* float tables for setting the radius via Gradient.setCornerRadii are used instead
* of multiple xml drawables.
*/
private class LayoutSelector {

private int children;
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<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_unchecked_tint_color" format="color"/>
</declare-styleable>

<declare-styleable name="AwesomeRadioButton">
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="radio_button_selected_color">#ff33b5e5</color>
<color name="radio_button_unselected_color">@android:color/transparent</color>
<color name="radio_button_unselected_color">#e0e0e0</color>
</resources>

0 comments on commit 46c6308

Please sign in to comment.