Skip to content
This repository was archived by the owner on Mar 8, 2024. It is now read-only.

Commit 58bd518

Browse files
author
Angad Singh
committed
feat: Implemented LabeledSwitchActivity in Demo App
1 parent 0b0ced7 commit 58bd518

14 files changed

Lines changed: 336 additions & 34 deletions

File tree

Readme.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ Android Library for Custom Switches.
44
### Developed by
55
[Angad Singh](https://www.github.com/angads25) ([@angads25](https://www.twitter.com/angads25))
66

7+
### Benchmark:
8+
[![API](https://img.shields.io/badge/API-9%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=9) <a href="http://www.methodscount.com/?lib=com.github.angads25%3Afilepicker%3A1.1.1"><img src="https://img.shields.io/badge/Methods and size-271 | 43 KB-e91e63.svg"/></a>
9+
10+
### Where to Find:
11+
[ ![Download](https://api.bintray.com/packages/angads25/maven/filepicker/images/download.svg) ](https://bintray.com/angads25/maven/filepicker/_latestVersion) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.angads25/filepicker/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.angads25/filepicker) [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-FilePicker-blue.svg?style=flat)](http://android-arsenal.com/details/1/3950)
12+
13+
14+
### Installation
15+
16+
* Library is also Available in MavenCentral, So just put this in your app dependencies to use it:
17+
```gradle
18+
compile 'com.github.angads25:toggle:1.0.0'
19+
```
20+
721
### Switches Available
822

9-
* Labeled Switch
23+
* Labeled Switch
24+

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
<category android:name="android.intent.category.LAUNCHER" />
1717
</intent-filter>
1818
</activity>
19+
<activity
20+
android:label="Labeled Switch"
21+
android:name=".LabeledSwitchActivity"
22+
android:parentActivityName=".MainActivity"/>
1923
<meta-data
2024
android:name="preloaded_fonts"
2125
android:resource="@array/preloaded_fonts" />
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (C) 2018 Angad Singh
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.github.angads25.toggledemo;
18+
19+
import android.graphics.Typeface;
20+
import android.os.Bundle;
21+
import android.os.Handler;
22+
import android.support.v4.content.res.ResourcesCompat;
23+
import android.support.v7.app.AppCompatActivity;
24+
25+
import com.github.angads25.toggle.LabeledSwitch;
26+
27+
import java.util.Random;
28+
import java.util.Timer;
29+
import java.util.TimerTask;
30+
31+
public class LabeledSwitchActivity extends AppCompatActivity {
32+
private Timer timers[];
33+
34+
private int[] switches = {
35+
R.id.switch1,
36+
R.id.switch2,
37+
R.id.switch4,
38+
R.id.switch5,
39+
R.id.switch7,
40+
R.id.switch8,
41+
};
42+
43+
private LabeledSwitch[] labeledSwitches;
44+
45+
private TimerTask[] timerTasks;
46+
47+
@Override
48+
protected void onCreate(Bundle savedInstanceState) {
49+
super.onCreate(savedInstanceState);
50+
setContentView(R.layout.activity_labeled_switch);
51+
52+
Typeface openSansBold = ResourcesCompat.getFont(LabeledSwitchActivity.this, R.font.open_sans_bold);
53+
54+
timers = new Timer[switches.length];
55+
timerTasks = new TimerTask[switches.length];
56+
labeledSwitches = new LabeledSwitch[switches.length];
57+
58+
for (int i = 0; i < switches.length; i++) {
59+
labeledSwitches[i] = findViewById(switches[i]);
60+
timers[i] = new Timer();
61+
62+
final int finalI = i;
63+
timerTasks[i] = new TimerTask() {
64+
@Override
65+
public void run() {
66+
runOnUiThread(new Runnable() {
67+
@Override
68+
public void run() {
69+
labeledSwitches[finalI].performClick();
70+
}
71+
});
72+
}
73+
};
74+
75+
int delay = (2 + new Random().nextInt(5)) * 1000;
76+
new Handler().postDelayed(new Runnable() {
77+
@Override
78+
public void run() {
79+
labeledSwitches[finalI].performClick();
80+
timers[finalI].schedule(timerTasks[finalI], 0, 10000);
81+
}
82+
}, delay);
83+
}
84+
85+
labeledSwitches[3].setTypeface(openSansBold);
86+
labeledSwitches[4].setTypeface(openSansBold);
87+
labeledSwitches[5].setTypeface(openSansBold);
88+
}
89+
90+
@Override
91+
protected void onStop() {
92+
super.onStop();
93+
for (int i = 0; i < switches.length; i++) {
94+
if(timers[i] != null) {
95+
timers[i].cancel();
96+
}
97+
}
98+
}
99+
}
Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,43 @@
1+
/*
2+
* Copyright (C) 2018 Angad Singh
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package com.github.angads25.toggledemo;
218

19+
import android.content.Intent;
320
import android.os.Bundle;
4-
import android.graphics.Typeface;
521
import android.support.v7.app.AppCompatActivity;
6-
import android.support.v7.widget.AppCompatTextView;
7-
import android.support.v4.content.res.ResourcesCompat;
8-
9-
import com.github.angads25.toggle.LabeledSwitch;
10-
import com.github.angads25.toggle.interfaces.OnToggledListener;
22+
import android.view.View;
1123

12-
public class MainActivity extends AppCompatActivity {
24+
public class MainActivity extends AppCompatActivity implements
25+
View.OnClickListener {
1326

1427
@Override
1528
protected void onCreate(Bundle savedInstanceState) {
1629
super.onCreate(savedInstanceState);
1730
setContentView(R.layout.activity_main);
1831

19-
LabeledSwitch labeledSwitch = findViewById(R.id.switch_demo);
20-
Typeface openSansBold = ResourcesCompat.getFont(MainActivity.this, R.font.open_sans_bold);
21-
labeledSwitch.setTypeface(openSansBold);
22-
labeledSwitch.setOnToggledListener(new OnToggledListener() {
23-
@Override
24-
public void onSwitched(LabeledSwitch labeledSwitch, boolean isOn) {
25-
((AppCompatTextView)findViewById(R.id.label_value))
26-
.setText(isOn ? labeledSwitch.getLabelOn() : labeledSwitch.getLabelOff());
32+
findViewById(R.id.switch_labeled).setOnClickListener(this);
33+
}
34+
35+
@Override
36+
public void onClick(View view) {
37+
switch (view.getId()) {
38+
case R.id.switch_labeled: {
39+
startActivity(new Intent(MainActivity.this, LabeledSwitchActivity.class));
2740
}
28-
});
41+
}
2942
}
3043
}

app/src/main/java/com/github/angads25/toggledemo/ToggleDemoApplication.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright (C) 2018 Angad Singh
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package com.github.angads25.toggledemo;
218

319
import android.app.Application;
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.v7.widget.LinearLayoutCompat
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
xmlns:tools="http://schemas.android.com/tools"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
android:background="#FFFFFF"
9+
android:orientation="vertical"
10+
tools:context="com.github.angads25.toggledemo.MainActivity">
11+
12+
<android.support.v7.widget.LinearLayoutCompat
13+
android:layout_width="match_parent"
14+
android:layout_height="wrap_content"
15+
android:orientation="horizontal"
16+
android:gravity="center">
17+
18+
<com.github.angads25.toggle.LabeledSwitch
19+
android:id="@+id/switch1"
20+
android:layout_width="wrap_content"
21+
android:layout_height="wrap_content"
22+
android:layout_margin="16dp"
23+
android:textSize="14sp"
24+
app:on="false"
25+
app:colorBorder="@color/colorAccent"/>
26+
27+
<com.github.angads25.toggle.LabeledSwitch
28+
android:id="@+id/switch2"
29+
android:layout_width="wrap_content"
30+
android:layout_height="wrap_content"
31+
android:layout_margin="16dp"
32+
android:textSize="14sp"
33+
app:on="true"
34+
app:colorBorder="@color/colorAccent"/>
35+
36+
<com.github.angads25.toggle.LabeledSwitch
37+
android:id="@+id/switch3"
38+
android:layout_width="wrap_content"
39+
android:layout_height="wrap_content"
40+
android:layout_margin="16dp"
41+
android:textSize="14sp"
42+
android:enabled="false"
43+
app:on="true"
44+
app:colorBorder="@color/colorAccent" />
45+
</android.support.v7.widget.LinearLayoutCompat>
46+
47+
<android.support.v7.widget.LinearLayoutCompat
48+
android:layout_width="match_parent"
49+
android:layout_height="wrap_content"
50+
android:orientation="horizontal"
51+
android:gravity="center">
52+
53+
<com.github.angads25.toggle.LabeledSwitch
54+
android:id="@+id/switch4"
55+
android:layout_width="wrap_content"
56+
android:layout_height="wrap_content"
57+
android:layout_margin="16dp"
58+
android:textSize="14sp"
59+
app:on="false"
60+
app:textOn="ON"
61+
app:textOff="OFF"
62+
app:colorOn="#00c4a6"
63+
app:colorBorder="#00c4a6"/>
64+
65+
<com.github.angads25.toggle.LabeledSwitch
66+
android:id="@+id/switch5"
67+
android:layout_width="wrap_content"
68+
android:layout_height="wrap_content"
69+
android:layout_margin="16dp"
70+
android:textSize="14sp"
71+
app:on="true"
72+
app:textOn="ON"
73+
app:textOff="OFF"
74+
app:colorOn="#00c4a6"
75+
app:colorBorder="#00c4a6"/>
76+
77+
<com.github.angads25.toggle.LabeledSwitch
78+
android:id="@+id/switch6"
79+
android:layout_width="wrap_content"
80+
android:layout_height="wrap_content"
81+
android:layout_margin="16dp"
82+
android:textSize="14sp"
83+
app:on="false"
84+
android:enabled="false"
85+
app:textOn="ON"
86+
app:textOff="OFF"
87+
app:colorOn="#00c4a6"
88+
app:colorBorder="#00c4a6"/>
89+
</android.support.v7.widget.LinearLayoutCompat>
90+
91+
<android.support.v7.widget.LinearLayoutCompat
92+
android:layout_width="match_parent"
93+
android:layout_height="wrap_content"
94+
android:orientation="horizontal"
95+
android:gravity="center">
96+
97+
<com.github.angads25.toggle.LabeledSwitch
98+
android:id="@+id/switch7"
99+
android:layout_width="82dp"
100+
android:layout_height="wrap_content"
101+
android:layout_margin="16dp"
102+
android:textSize="12sp"
103+
app:on="false"
104+
app:textOn="START"
105+
app:textOff="STOP"
106+
app:colorOn="@color/colorPrimary"
107+
app:colorBorder="#D3D3D3"/>
108+
109+
<com.github.angads25.toggle.LabeledSwitch
110+
android:id="@+id/switch8"
111+
android:layout_width="82dp"
112+
android:layout_height="wrap_content"
113+
android:layout_marginTop="16dp"
114+
android:layout_marginBottom="16dp"
115+
android:textSize="12sp"
116+
app:on="true"
117+
app:textOn="START"
118+
app:textOff="STOP"
119+
app:colorOn="@color/colorPrimary"
120+
app:colorBorder="@color/colorPrimary"/>
121+
122+
<com.github.angads25.toggle.LabeledSwitch
123+
android:id="@+id/switch9"
124+
android:layout_width="82dp"
125+
android:layout_height="wrap_content"
126+
android:layout_margin="16dp"
127+
android:textSize="12sp"
128+
android:enabled="false"
129+
app:on="true"
130+
app:textOn="START"
131+
app:textOff="STOP"
132+
app:colorBorder="@color/colorAccent" />
133+
</android.support.v7.widget.LinearLayoutCompat>
134+
</android.support.v7.widget.LinearLayoutCompat>
Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<FrameLayout
2+
<android.support.v7.widget.LinearLayoutCompat
33
xmlns:android="http://schemas.android.com/apk/res/android"
4-
xmlns:app="http://schemas.android.com/apk/res-auto"
54
xmlns:tools="http://schemas.android.com/tools"
65
android:layout_width="match_parent"
76
android:layout_height="match_parent"
87
android:background="#FFFFFF"
8+
android:orientation="vertical"
99
tools:context="com.github.angads25.toggledemo.MainActivity">
1010

11-
<com.github.angads25.toggle.LabeledSwitch
12-
android:id="@+id/switch_demo"
11+
<android.support.v7.widget.AppCompatButton
12+
android:id="@+id/switch_labeled"
1313
android:layout_width="wrap_content"
1414
android:layout_height="wrap_content"
15-
android:layout_margin="16dp"
16-
android:textSize="14sp"
17-
app:on="false"
18-
app:textOn="ON"
19-
app:textOff="OFF"
20-
app:colorBorder="@color/colorAccent"/>
15+
android:text="Labeled Switch"/>
2116

22-
<android.support.v7.widget.AppCompatTextView
23-
android:id="@+id/label_value"
24-
android:layout_width="wrap_content"
25-
android:layout_height="wrap_content"
26-
android:text="@string/app_name"
27-
android:layout_gravity="center"/>
28-
</FrameLayout>
17+
</android.support.v7.widget.LinearLayoutCompat>

0 commit comments

Comments
 (0)