Monday, October 15, 2018

Android: 3 Way switch

Hello friends,

Today I will share with you a three-way switch that I made for my own needs!



Android doesn't provide a three way switch. However, I have created a 3 way switch, following the stack overflow page here.

XML:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@color/md_green_100"
    android:gravity="center"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="10dp"
        android:text="@string/send"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="18sp"
        android:textStyle="bold" />

    <SeekBar
        style="@style/Widget.AppCompat.SeekBar.Discrete"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:backgroundTint="@color/md_green_100"
        android:id="@+id/transferSwitch"
        android:elevation="5dp"
        android:foregroundTint="@color/md_green_100"
        android:indeterminateTint="@color/md_green_100"
        android:max="2"
        android:padding="5dp"
        android:progress="1"
        android:progressBackgroundTint="@color/md_green_100"
        android:progressTint="@color/md_green_100"
        android:secondaryProgressTint="@color/md_green_100"
        android:thumb="@android:drawable/presence_online"
        android:thumbTint="@color/md_green_800"
        app:tickMarkTint="@color/md_green_800" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|end"
        android:gravity="center"
        android:padding="10dp"
        android:text="@string/receive"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="18sp"
        android:textStyle="bold" />

</LinearLayout>


There you go! That's it,
To use it, try something like


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
SeekBar seekBar = findViewById(SEEKBAR);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
       //Do something here!
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
});


That's all for now! Thank you!

No comments:

Post a Comment