diff --git a/README.md b/README.md index a1662af..f7d015d 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,20 @@ dependencies { custom:textToggleLeft="OR" custom:textToggleRight="AND"/> ``` +Width of each toggle button can be given as below. + +```xml + +``` +This width overrides the default width given by `custom:toggleWidth="120dp"` ![Sample of libray with 3 items](docs/3_items.gif) @@ -83,6 +97,27 @@ labels.add("NOT"); labels.add("OFF"); toggleSwitch.setLabels(labels); ``` + +Add custom width (optional) + +JAVA code +```java +ToggleSwitch toggleSwitch = (ToggleSwitch) findViewById(R.id.multiple_switches); +ArrayList labels = new ArrayList<>(); +ArrayList sizes = new ArrayList<>(); +labels.add("AND"); +labels.add("OR"); +labels.add("XOR"); +labels.add("NOT"); +labels.add("OFF"); +sizes.add(80f); +sizes.add(100f); +sizes.add(120f); +sizes.add(-1f); //Default width from toggleWidth +sizes.add(-1f); //Default width from toggleWidth +toggleSwitch.setLabels(labels,sizes); +``` + ![Sample of libray with 3 items](docs/n_items.gif) NOTE: Providing a custom array of labels, the attributes textToggle[Left/Center/Right] will be ignored. diff --git a/androidtoggleswitch/build.gradle b/androidtoggleswitch/build.gradle index 471af99..e5965cb 100644 --- a/androidtoggleswitch/build.gradle +++ b/androidtoggleswitch/build.gradle @@ -29,4 +29,4 @@ dependencies { compile 'com.android.support:appcompat-v7:24.2.1' } -apply from: 'https://raw.githubusercontent.com/blundell/release-android-library/master/android-release-aar.gradle' \ No newline at end of file +apply from: 'https://raw.githubusercontent.com/blundell/release-android-library/master/android-release-aar.gradle' diff --git a/androidtoggleswitch/src/main/java/belka/us/androidtoggleswitch/widgets/BaseToggleSwitch.java b/androidtoggleswitch/src/main/java/belka/us/androidtoggleswitch/widgets/BaseToggleSwitch.java index 56eeb1d..e28fef0 100644 --- a/androidtoggleswitch/src/main/java/belka/us/androidtoggleswitch/widgets/BaseToggleSwitch.java +++ b/androidtoggleswitch/src/main/java/belka/us/androidtoggleswitch/widgets/BaseToggleSwitch.java @@ -14,6 +14,7 @@ import android.widget.LinearLayout; import android.widget.TextView; + import java.util.ArrayList; import belka.us.androidtoggleswitch.R; @@ -55,6 +56,7 @@ protected static class Default { private LayoutInflater mInflater; private LinearLayout toggleSwitchesContainer; private ArrayList mLabels; + private ArrayList mSizes; //List of Width of toggleButtons private Context mContext; public BaseToggleSwitch(Context context) { @@ -78,6 +80,10 @@ public BaseToggleSwitch(Context context, AttributeSet attrs) { String leftToggleText = attributes.getString(R.styleable.ToggleSwitchOptions_textToggleLeft); String rightToggleText = attributes.getString(R.styleable.ToggleSwitchOptions_textToggleRight); + float centerToggleWidth = attributes.getDimension(R.styleable.ToggleSwitchOptions_widthToggleCenter,-1); + float leftToggleWidth = attributes.getDimension(R.styleable.ToggleSwitchOptions_widthToggleLeft,-1); + float rightToggleWidth = attributes.getDimension(R.styleable.ToggleSwitchOptions_widthToggleRight,-1); + this.activeBgColor = attributes.getColor(R.styleable.ToggleSwitchOptions_activeBgColor, ContextCompat.getColor(context, BaseToggleSwitch.Default.ACTIVE_BG_COLOR)); this.activeTextColor = attributes.getColor(R.styleable.ToggleSwitchOptions_activeTextColor, ContextCompat.getColor(context, BaseToggleSwitch.Default.ACTIVE_TEXT_COLOR)); this.inactiveBgColor = attributes.getColor(R.styleable.ToggleSwitchOptions_inactiveBgColor, ContextCompat.getColor(context, BaseToggleSwitch.Default.INACTIVE_BG_COLOR)); @@ -90,10 +96,18 @@ public BaseToggleSwitch(Context context, AttributeSet attrs) { if (leftToggleText != null && !leftToggleText.isEmpty() && rightToggleText != null && !rightToggleText.isEmpty()) { mLabels = new ArrayList<>(); + mSizes = new ArrayList<>(); + mLabels.add(leftToggleText); - if (centerToggleText != null && !centerToggleText.isEmpty()) + mSizes.add(leftToggleWidth); + + if (centerToggleText != null && !centerToggleText.isEmpty()){ mLabels.add(centerToggleText); + mSizes.add(centerToggleWidth); + } + mLabels.add(rightToggleText); + mSizes.add(rightToggleWidth); buildToggleButtons(); } } finally { @@ -173,15 +187,17 @@ public void setToggleWidth(float toggleWidth) { protected void activate(int position) { setColors(getToggleSwitchButton(position), activeBgColor, activeTextColor); } - - private void addToogleBtn(String text) { + private void addToggleBtn(String text) { + addToggleBtn(text,-1); + } + private void addToggleBtn(String text, float width) { ToggleSwitchButton toggleSwitchButton = new ToggleSwitchButton(mContext); TextView toggleBtnTxt = toggleSwitchButton.getTextView(); toggleBtnTxt.setText(text); toggleBtnTxt.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); - LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) toggleWidth, LayoutParams.WRAP_CONTENT); + LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) (width <0 ? toggleWidth:width), LayoutParams.WRAP_CONTENT); if (toggleWidth == 0f) params.weight = 1f; toggleBtnTxt.setLayoutParams(params); @@ -189,7 +205,7 @@ private void addToogleBtn(String text) { toggleSwitchButton.getTextView().setOnClickListener(this); - LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((int) toggleWidth, LayoutParams.MATCH_PARENT); + LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((int) (width <0 ? toggleWidth:width), LayoutParams.MATCH_PARENT); if (toggleWidth == 0f) layoutParams.weight = 1f; toggleSwitchesContainer.addView(toggleSwitchButton.getView(), layoutParams); @@ -217,7 +233,7 @@ else if (isLast(toggleSwitchButton)) protected void buildToggleButtons() { for (String label : mLabels) - addToogleBtn(label); + addToggleBtn(label,mSizes.get(mLabels.indexOf(label))); } protected void disable(int position) { @@ -271,6 +287,21 @@ public void setLabels(ArrayList labels) { if (labels == null || labels.isEmpty()) throw new RuntimeException("The list of labels must contains at least 2 elements"); mLabels = labels; + mSizes = new ArrayList<>(); + for (String label : labels){ + mSizes.add(-1f); + } + toggleSwitchesContainer.removeAllViews(); + buildToggleButtons(); + } + public void setLabels(ArrayList labels,ArrayList sizes) { + if (labels == null || labels.isEmpty()) + throw new RuntimeException("The list of labels must contains at least 2 elements"); + if(labels.size()!=sizes.size()) + throw new RuntimeException("The size of labels list and the size of sizes list must be equal"); + + mLabels = labels; + mSizes = sizes; toggleSwitchesContainer.removeAllViews(); buildToggleButtons(); } diff --git a/androidtoggleswitch/src/main/res/values/attrs.xml b/androidtoggleswitch/src/main/res/values/attrs.xml index 0518983..f463924 100644 --- a/androidtoggleswitch/src/main/res/values/attrs.xml +++ b/androidtoggleswitch/src/main/res/values/attrs.xml @@ -7,6 +7,10 @@ + + + +