WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

ToggleButton (On/Off) Tutorial Android


ToggleButton is used to display checked and unchecked state of a button


ToggleButton Android

ToggleButton is mainly used to on/off in sound, Bluetooth, wifi, hotspot etc.


java class:
/*Add in Oncreate() funtion after setContentView()*/
ToggleButton deToggleButton = (ToggleButton) findViewById(R.id.demoToggleButton); // initiate a toggle button
Boolean ToggleButtonState = demotoggleButton.isChecked(); // check current state of a toggle button (true or false).

Attributes ToggleButton

id id is an attribute used to unique identify a toggle button.

<ToggleButton
android:id="@+id/demoToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

checked checked is an attribute of toggle button used to set the current state of a toggle button. It is used to on/off checked. <ToggleButton android:id="@+id/demoToggleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" /> set up java class
/*Add in Oncreate() funtion after setContentView()*/
ToggleButton demoToggleButton = (ToggleButton) findViewById(R.id.demoToggleButton); // initiate a toggle button
simpleToggleButton.setChecked(true); // set the current state of a toggle button
textColor textColor attribute is used to set the text color of a toggle button

<ToggleButton
    android:id="@+id/demoToggleButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="false"
    android:textOff="On State"
    android:textOn="Off State"
    android:layout_centerHorizontal="true"
    android:textColor="#00f" />
set up in java class
/*Add in Oncreate() funtion after setContentView()*/
ToggleButton demoToggleButton = (ToggleButton) findViewById(R.id.demoToggleButton);// initiate toggle button
simpleToggleButton.setTextColor(Color.BLUE); //blue color for displayed text of toggle button
textSize textSize attribute set the size of the text of a toggle button
<ToggleButton
    android:id="@+id/simpleToggleButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="false"
    android:textOff="On State"
    android:textOn="Off State"
    android:layout_centerHorizontal="true"
    android:textColor="#f00"
    android:textSize="25sp"/> 
setting java class
/*Add in Oncreate() funtion after setContentView()*/
ToggleButton demoToggleButton = (ToggleButton) findViewById(R.id.demoToggleButton); // initiate toggle button
demoToggleButton.setTextSize(25); // set 25sp displayed text size of toggle button
textStyle textStyle attribute is used to set the text style of the text of a Toggle button
<ToggleButton
    android:id="@+id/demoToggleButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:textOff="Off State"
    android:textOn="On State"
    android:textSize="28sp"
    android:layout_centerHorizontal="true"
    android:textColor="#00f"
    android:textStyle="bold|italic"/> 
background background attribute is used to set the background of a toggle button
< ToggleButton
    android:id="@+id/simpleToggleButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:textOff="Off State"
    android:textOn="On State"
    android:textSize="28sp"
    android:layout_centerHorizontal="true"
    android:textStyle="bold|italic"
    android:textColor="#00f"
    android:background="#f00"/>

set up in java class
/*Add in Oncreate() funtion after setContentView()*/
ToggleButton demoToggleButton = (ToggleButton) findViewById(R.id.demoToggleButton);
demoToggleButton.setBackgroundColor(Color.RED);

padding padding attribute is used to set the padding from left, right, top or bottom.
 
drawable drawableBottom, drawableTop, drawableRight And drawableLeft



ToggleButton Android Example

open android studio

Select File -> New -> New Project -> Fill the forms and click "Finish" button.
Open res -> layout -> activity_main.xml (or) main.xml


    

        

        
    

    
Open app -> java -> package -> MainActivity.java
package example.welookups.togglebuttonexample;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    ToggleButton simpleToggleButton1, simpleToggleButton2;
    Button submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // initiate toggle button's
        simpleToggleButton1 = (ToggleButton) findViewById(R.id.simpleToggleButton1);
        simpleToggleButton2 = (ToggleButton) findViewById(R.id.simpleToggleButton2);
        submit = (Button) findViewById(R.id.submitButton);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String status = "ToggleButton1 : " + simpleToggleButton1.getText() + "\n" + "ToggleButton2 : " + simpleToggleButton2.getText();
                Toast.makeText(getApplicationContext(), status, Toast.LENGTH_SHORT).show(); // display the current state of toggle button's
            }
        });
    }

    
}