Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom width to each toggle button #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ dependencies {
custom:textToggleLeft="OR"
custom:textToggleRight="AND"/>
```
Width of each toggle button can be given as below.

```xml
<belka.us.androidtoggleswitch.widgets.ToggleSwitch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:textToggleCenter="XOR"
custom:textToggleLeft="OR"
custom:textToggleRight="AND"
custom:widthToggleCenter="100dp"
custom:widthToggleLeft="50dp"
custom:widthToggleRight="100dp"/>
```
This width overrides the default width given by `custom:toggleWidth="120dp"`

![Sample of libray with 3 items](docs/3_items.gif)

Expand All @@ -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<String> labels = new ArrayList<>();
ArrayList<Float> 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.
Expand Down
2 changes: 1 addition & 1 deletion androidtoggleswitch/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
apply from: 'https://raw.githubusercontent.com/blundell/release-android-library/master/android-release-aar.gradle'
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.widget.LinearLayout;
import android.widget.TextView;


import java.util.ArrayList;

import belka.us.androidtoggleswitch.R;
Expand Down Expand Up @@ -55,6 +56,7 @@ protected static class Default {
private LayoutInflater mInflater;
private LinearLayout toggleSwitchesContainer;
private ArrayList<String> mLabels;
private ArrayList<Float> mSizes; //List of Width of toggleButtons
private Context mContext;

public BaseToggleSwitch(Context context) {
Expand All @@ -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));
Expand All @@ -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 {
Expand Down Expand Up @@ -173,23 +187,25 @@ 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);

toggleSwitchButton.getSeparator().setBackgroundColor(separatorColor);

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

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -271,6 +287,21 @@ public void setLabels(ArrayList<String> 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<String> labels,ArrayList<Float> 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();
}
Expand Down
4 changes: 4 additions & 0 deletions androidtoggleswitch/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<attr name="textToggleLeft" format="string" localization="suggested" />
<attr name="textToggleRight" format="string" localization="suggested" />

<attr name="widthToggleCenter" format="dimension" localization="suggested" />
<attr name="widthToggleLeft" format="dimension" localization="suggested" />
<attr name="widthToggleRight" format="dimension" localization="suggested" />

<attr name="activeBgColor" format="color"/>
<attr name="activeTextColor" format="color"/>
<attr name="inactiveBgColor" format="color"/>
Expand Down