0% found this document useful (0 votes)
6 views12 pages

MAD microproject

The document provides a comprehensive guide on implementing a toggle button in Android applications, including its introduction, XML layout code, and Java activity code. It details how to create a toggle button that changes images based on its state, with corresponding XML files for the button's appearance. The document concludes with references for further reading on toggle buttons in Android development.

Uploaded by

Onkar Talekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views12 pages

MAD microproject

The document provides a comprehensive guide on implementing a toggle button in Android applications, including its introduction, XML layout code, and Java activity code. It details how to create a toggle button that changes images based on its state, with corresponding XML files for the button's appearance. The document concludes with references for further reading on toggle buttons in Android development.

Uploaded by

Onkar Talekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

CLICKING ON TOGGLE BUTTON

INDEX

SR.NO. CONTENT PAGE NO.


1) Introduction 2
2) Activity_main.xml code 3
3) Selecter.xml 4
4) Switch_off.xml 5
5) Switch_on.xml 6
6) Main activity.java 7
7) Output 9
8) Conclusion 11
9) References 12

1
CLICKING ON TOGGLE BUTTON

INTRODUCTION
A toggle button allows the user to change a setting between two states. You can add a basic
toggle button to your layout with the Toggle Button object. Android 4.0 (API level 14) introduces
another kind of toggle button called a switch that provides a slider control, which you can add
with a Switch object.
Toggles may replace two radio buttons or a single checkbox to allow users to choose between
two opposing states

2
CLICKING ON TOGGLE BUTTON

ACTVITY_MAIN.XML CODE OF TOGGLE BUTTON

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity" android:orientation="vertical"
android:gravity="center"
android:padding="10dp">

<ToggleButton
android:id="@+id/toggle" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_marginTop="40dp"
android:drawableStart="@drawable/selector"android:textColor="#455A64"
android:textOff="switch_off" android:textOn="switch_on"
tools:ignore="TextContrastCheck" />

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"android:id="@+id/imageview"
android:layout_marginTop="20dp" android:contentDescription="todo"/>
</LinearLayout>

3
CLICKING ON TOGGLE BUTTON

SELECTER.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/switch_on"/>
<item android:state_checked="false"
android:drawable="@drawable/switch_off"/>
</selector>

4
CLICKING ON TOGGLE BUTTON

SWITCH_OFF.XML

<vector android:height="24dp" android:tint="#FF0015"


android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<path android:fillColor="@android:color/white"
android:pathData="M13,3h-2v10h2L13,3zM17.83,5.17l-
1.42,1.42C17.99,7.86 19,9.81 19,12c0,3.87 -3.13,7 -7,7s-
7,-3.13 -7,-7c0,-2.19 1.01,-4.14 2.58,-
5.42L6.17,5.17C4.23,6.82 3,9.26 3,12c0,4.97 4.03,9 9,9s9,-
4.03 9,-9c0,-2.74 -1.23,-5.18 -3.17,-6.83z"/>
</vector>

5
CLICKING ON TOGGLE BUTTON

SWITCH_ON.XML

<vector android:height="24dp" android:tint="#44FF00"


android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<path android:fillColor="@android:color/white"
android:pathData="M13,3h-2v10h2L13,3zM17.83,5.17l-
1.42,1.42C17.99,7.86 19,9.81 19,12c0,3.87 -3.13,7 -7,7s-
7,-3.13 -7,-7c0,-2.19 1.01,-4.14 2.58,-
5.42L6.17,5.17C4.23,6.82 3,9.26 3,12c0,4.97 4.03,9 9,9s9,-
4.03 9,-9c0,-2.74 -1.23,-5.18 -3.17,-6.83z"/>
</vector>

6
CLICKING ON TOGGLE BUTTON

MAINACTIVITY.JAVA

package com.example.togglebutton;

import androidx.appcompat.app.AppCompatActivity;import

android.os.Bundle;
import android.os.TokenWatcher; import
android.view.View; import
android.widget.ImageView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity


{
ToggleButton togglebutton;
ImageView imageview;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
togglebutton=findViewById(R.id.toggle);
imageview=findViewById(R.id.imageview);

imageview.setImageDrawable(getResources().getDrawable(R.drawable.switch_off));
togglebutton.setOnClickListener(new
View.OnClickListener(){
public void onClick(View View)
{
if (togglebutton.isChecked()) {

imageview.setImageDrawable(getResources().getDrawable(R.drawable.switch_on));
}
else {

7
CLICKING ON TOGGLE BUTTON

imageview.setImageDrawable(getResources().getDrawable(R.dr
awable.switch_off));
}

});
}
}

8
CLICKING ON TOGGLE BUTTON

OUTPUT

Fig. Image before clicking Button

9
CLICKING ON TOGGLE BUTTON

Fig. Image After clicking Button

10
CLICKING ON TOGGLE BUTTON

CONCLUSION

A toggle button allows the user to change a setting between two states. By this button
we can change the images on the mobile. In mobile This button issued as Switch on / off
button

11
CLICKING ON TOGGLE BUTTON

REFERANCE

 https://developer.android.com/android/widget/ToggleButton
 https://developer.android.com/guide/topics/ui/controls/togglebutton
 Text book of Mobile application

12

You might also like