Toggle Button is two state button checked and unchecked. It randomaly
change their state when press by user. It work like home bulb, when bulb ON so it called checked state otherside
OFF state called Unchecked state. It use in android phone to start or stop service
like -
![]() |
|||
![]() |
|||

Programattically Add button
XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/ll1" > </LinearLayout>
Java
public class MainActivity extends AppCompatActivity { LinearLayout layout1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); layout1=(LinearLayout)findViewById(R.id.ll1); ToggleButton toggleButton=new ToggleButton(this); toggleButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); toggleButton.setTextOff("Good Bye"); toggleButton.setTextOn("Welcome"); toggleButton.setId(R.id.reservedNamedId); layout1.addView(toggleButton); } }
res/values/ids.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <item name="reservedNamedId" type="id"/> </resources>
-------------------------------------------------------------------------------------------------------
Note : To add a resource
id programmatically, You do these thing
If your
ViewGroup cannot be defined via XML (or you don't
want it to be) you can reserve the id via XML to ensure it remains unique:
Here, values/ids.xml defines a custom
id :<?xml version="1.0" encoding="utf-8"?> <resources> <item name="reservedNamedId" type="id"/> </resources>
Then
once the ViewGroup or View has been created, you can attach the custom id
myViewGroup.setId(R.id.reservedNamedId); |
Toggle Button Text
To change text of toggle button, we use two attribute -
textOff and textOn
TextOff - When
Button in disable or unchecked state then textOff attribute text display.
Syntax : android:textOff="Good Bye"
TextOn - When
Button in enable or checked state then textOn attribute text display.
Syntax : android:textOn="Welcome"
Program
<ToggleButton android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="Service Stopped" android:textOn="Service Started" />
alpha
The alpha to apply to the
indicator when disabled.
|
Full
Transparent
android:alpha="@android:color/transparent"
<ToggleButton android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="Service Stopped" android:textOn="Service Started" android:alpha="@android:color/transparent" />
Set Transparent Level
<ToggleButton android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:alpha="1.2" />
Checked
Android default state of toggle button is unchecked or
disable but you can change and make it enable using "checked"
attribute.
Value must be a boolean - "true" or
"false".
Syntax - android:checked="true/false"
drawableBottom,
drawableTop, drawableRight And drawableLeft
These attribute draw the drawable below, top, right and left
of the text of ToggleButton.
<ToggleButton android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:drawableTop="@drawable/signin" android:drawableBottom="@drawable/b_default" android:drawableLeft="@drawable/ic_zoom" android:drawableRight="@drawable/ic_delete" />
CheckedChangeListener
Interface definition for a callback to be invoked when the
checked state of a compound button changed.
XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <ToggleButton android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
Java
public class MainActivity extends AppCompatActivity { ToggleButton toggleButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toggleButton=(ToggleButton)findViewById(R.id.toggleButton1); toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) { Toast.makeText(getApplicationContext(),"Checked",Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(),"Unchecked",Toast.LENGTH_SHORT).show(); } } }); } }
22
No comments:
Post a Comment