Android Toggle Button Custom Look

How to customize toggle button in android?

Is that what you're looking for :

Sample Image

First create a ToggleButton XML :

<ToggleButton
android:id="@+id/follow"
android:layout_width="120dp"
android:layout_height="35dp"
android:layout_centerInParent="true"
android:background="#61849f"
android:checked="false"
android:drawableStart="@drawable/ic_baseline_star_24"
android:elevation="0dp"
android:gravity="center"
android:padding="8dp"
android:textColor="#fff"
android:textOff=" FOLLOW "
android:textOn=" FOLLOWING " />

Java :

toggleButton = findViewById(R.id.follow);
toggleButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) setUnfollow();
else setFollow();
});

The setFollow & setUnfollow methods :

private void setUnfollow() {
toggleButton.setChecked(true);
toggleButton.setTextColor(Color.BLACK);
toggleButton.setBackgroundColor(Color.WHITE);
toggleButton.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
toggleButton.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.border));

}

private void setFollow() {
toggleButton.setChecked(false);
toggleButton.setTextColor(Color.WHITE);
toggleButton.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.fill));
toggleButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_baseline_star_24, 0, 0, 0);
}

Finally The fill.XML and bordre.XML (create these file in the drawable folder)

Border.XML :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="#61849f" />
</shape>

Fill.XML

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#61849f" />
</shape>

How to custom switch button?

However, I might not be taking the best approach, but this is how I have created some Switch like UIs in few of my apps.
Here is the code -

<RadioGroup
android:checkedButton="@+id/offer"
android:id="@+id/toggle"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="@dimen/margin_medium"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="@dimen/margin_medium"
android:background="@drawable/pink_out_line"
android:orientation="horizontal">

<RadioButton
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:id="@+id/search"
android:background="@drawable/toggle_widget_background"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="Search"
android:textColor="@color/white" />

<RadioButton
android:layout_marginRight="1dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:id="@+id/offer"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/toggle_widget_background"
android:button="@null"
android:gravity="center"
android:text="Offers"
android:textColor="@color/white" />
</RadioGroup>

pink_out_line.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="2dp" />
<solid android:color="#80000000" />
<stroke
android:width="1dp"
android:color="@color/pink" />
</shape>

toggle_widget_background.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/pink" android:state_checked="true" />
<item android:drawable="@color/dark_pink" android:state_pressed="true" />
<item android:drawable="@color/transparent" />
</selector>

And here is the output -Sample Image

How to create custom ToggleButton style in Android?

This works for me.

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="buttonStyle">@style/MyButtonStyle</item>
<item name="android:buttonStyleToggle">@style/MyToggleButtonStyle</item>
</style>

<style name="MyToggleButtonStyle" parent="android:Widget.Button.Toggle">
<item name="android:minHeight">@dimen/button_min_height</item>
<item name="android:textSize">@dimen/button_text_size</item>
</style>

change color of my ToggleButton when clicked

You have to create new file in drawable directory with list of states.

You can name it toggle_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- WHEN IS CHECKED -->
<item android:drawable="@color/colorPrimary" android:state_checked="false" />

<!-- WHEN IS NOT CHECKED -->
<item android:drawable="@color/colorAccent" android:state_checked="true" />

</selector>

Above file you can use as background of ToggleButton:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ToggleButton
android:id="@+id/toggleButton_monday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/toggle_background"
android:textOff="ON"
android:textOn="OFF" />

</LinearLayout>

By default button has grey border. When would you like to remove it just add:

style="?android:attr/borderlessButtonStyle"

to your ToggleButton in xml file.

You can also add OnCheckedChangeListener. If you have many buttons you can add all of them to the list, and in loop add that same listener for all of them:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private List<ToggleButton> listOfButtons = new ArrayList<>();

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Add ToggleButtons to list
listOfButtons.add(findViewById(R.id.toggleButton_monday));

// Create listener for all of them
CompoundButton.OnCheckedChangeListener listener = (buttonView, isChecked) -> {
// Do something
};

// Add listener to all od buttons
for (ToggleButton button : listOfButtons) {
button.setOnCheckedChangeListener(listener);
}
}
}

Change toggleButton color

try this way.

ToggleButton :

<ToggleButton 
android:layout_width="50dp"
android:layout_height="50dp"
android:textOn="On"
android:textOff="Off"
android:textSize="20sp"
android:background="@drawable/toggle_day_bg_selector" />

toggle_day_bg_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/toggle_off"
android:state_checked="false"/>
<item android:drawable="@drawable/toggle_on"
android:state_checked="true"/>
</selector>

toggle_on.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>
<solid android:color="@color/red" />

</shape>

toggle_off.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/green" />

</shape>

Hope this will help.

EDIT :

use this drawable files for showing images on ToggleButton

toggle_off.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="@android:color/holo_green_dark" />
</shape>
</item>
<item android:drawable="@drawable/ic_launcher">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="@android:color/holo_green_dark" />
</shape>
</item>

</layer-list>

toggle_on.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="@android:color/holo_red_dark" />
</shape>
</item>
<item android:drawable="@drawable/ic_launcher">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="@android:color/holo_red_dark" />
</shape>
</item>

</layer-list>

happy coding..

Drawable of a custom toggle button not centered

I think the issue was your 'button' attribute. I had luck with this:

   <ToggleButton
android:id="@+id/contacts_subscribe_button"
android:layout_width="32dp"
android:layout_height="32dp"
android:background="@drawable/checkbox_bg"
android:src="@drawable/checkbox_src"
android:checked="true"
android:gravity="center"
android:minHeight="0dp"
android:minWidth="0dp"
android:textOff="@null"
android:textOn="@null" />

checkbox_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false" android:drawable="@color/transparent" />
<item android:state_checked="true" android:drawable="@drawable/ic_checked" />
<item android:drawable="@color/transparent" />

checkbox_src.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/brikky1"/>
<corners
android:radius="2dp"
android:bottomLeftRadius="2dp"
android:bottomRightRadius="2dp"
android:topLeftRadius="2dp"
android:topRightRadius="2dp" />
<size android:height="32dp" android:width="32dp"/>
</shape>

Sample Image
Sample Image

customize toggle button on Android

I did the same task this way, the button:

        <ToggleButton  android:id="@+id/mlAbout"
android:textOn="@string/about"
android:textOff="@string/about"
android:background="@drawable/ml_about" />

@drawable/ml_about:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/list_bg_top"></item>
<item android:left="10dp">
<bitmap android:src="@drawable/waiting_status_btn"
android:gravity="center_vertical|left" />
</item>
</layer-list>

@drawable/list_bg_top background image that will be stretched and @drawable/waiting_status_btn is the icon the will not be stretched with the widget



Related Topics



Leave a reply



Submit