Wednesday 17 August 2016

Toggle Button


A Switch is a two-state toggle switch widget that can select between two options. The user may drag the "thumb" back and forth to choose the selected option, or simply tap to toggle as if it were a checkbox.
It is improved version of toggle button.

 
Text Box: <Switch
    android:id="@+id/sw1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

    />








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



        Switch aSwitch = new Switch(this);

        aSwitch.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        aSwitch.setTextOff("Good Bye");

        aSwitch.setTextOn("Welcome");

        aSwitch.setId(R.id.aSwitch1);

        layout1.addView(aSwitch);



    }

}
res/values/ids.xml
<?xml version="1.0" encoding="utf-8"?>

<resources>

    <item name="aSwitch1" 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
 
<Switch

    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"

<Switch

    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
 
<Switch

    android:id="@+id/toggleButton1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    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"  // default value "false"
 
<Switch

    android:id="@+id/toggleButton1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:checked="true"

    />
 
 
 
drawableBottom, drawableTop, drawableRight And drawableLeft
These attribute draw the drawable below, top, right and left of the text of ToggleButton.
<Switch

    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"

    >

    <Switch

        android:id="@+id/aSw1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        />

</LinearLayout>

Java
public class MainActivity extends AppCompatActivity {



    Switch aSwitch;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        aSwitch=(Switch)findViewById(R.id.aSw1);



        aSwitch.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();

                }

            }

        });

    }



}

Change Drawable
Change drawable image according to switch state - checked or unchecked.
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"

    >

    <Switch

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:thumb="@drawable/switch_thumb"

        />

</LinearLayout>

Java
public class MainActivity extends AppCompatActivity {





    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



    }

}

and create a Drawable XML file in res/drawable folder.Name it switch_thumb
and place this code -
switch_thumb.xml
<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

      <item android:state_checked="true"  android:drawable="@drawable/ic_zoom" />

    <item   android:drawable="@drawable/ic_delete" />



</selector>

















22


No comments:

Post a Comment