0% found this document useful (0 votes)
34 views6 pages

05 7 ToggleButtonControl

Uploaded by

Produktify
The document discusses the ToggleButton control in Android. It is an on/off button with a light indicator that displays checked and unchecked states. It inherits attributes from TextView like text, font size, etc. and View like background, visibility. An example app is provided using ToggleButtons and a Button in a RelativeLayout. Clicking the Button displays a Toast with the text of the checked ToggleButtons. The app demonstrates basic usage of ToggleButtons and handling click events in Android.

Copyright:

© All Rights Reserved

Available Formats

Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
34 views6 pages

05 7 ToggleButtonControl

Uploaded by

Produktify
The document discusses the ToggleButton control in Android. It is an on/off button with a light indicator that displays checked and unchecked states. It inherits attributes from TextView like text, font size, etc. and View like background, visibility. An example app is provided using ToggleButtons and a Button in a RelativeLayout. Clicking the Button displays a Toast with the text of the checked ToggleButtons. The app demonstrates basic usage of ToggleButtons and handling click events in Android.

Copyright:

© All Rights Reserved

Available Formats

Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

http://www.tutorialspoint.com/android/android_togglebutton_control.

htm

Android ToggleButton Control


A ToggleButton displays checked/unchecked states as a button. It is basically an on/off button with a
light indicator.

Toggle Button

ToggleButton Attributes
Following are the important attributes related to ToggleButton control. You can check Android
official documentation for complete list of attributes and related methods which you can use to
change these attributes are run time.

Attribute Description

android:disabledAlpha This is the alpha to apply to the indicator when disabled.

android:textOff This is the text for the button when it is not checked.

android:textOn This is the text for the button when it is checked.

Inherited from android.widget.TextView Class −

Attribute Description

If set, specifies that this TextView has a textual input method and
android:autoText
automatically corrects some common spelling errors.

android:drawableBottom This is the drawable to be drawn below the text.

android:drawableRight This is the drawable to be drawn to the right of the text.

android:editable If set, specifies that this TextView has an input method.

android:text This is the Text to display.


Inherited from android.view.View Class:

Attribute Description

android:background This is a drawable to use as the background.

android:contentDescription This defines text that briefly describes content of the view.

android:id This supplies an identifier name for this view,

This is the name of the method in this View's context to invoke when
android:onClick
the view is clicked.

android:visibility This controls the initial visibility of the view.

Example
This example will take you through simple steps to show how to create your own Android
application using Linear Layout and ToggleButton.

Step Description

You will use Android studio IDE to create an Android application and name it as My Application
1 under a package com.example.saira_000.myapplication as explained in the Hello World
Example chapter.

2 Modify src/MainActivity.java file to add a click event.

2 Modify the default content of res/layout/activity_main.xml file to include Android UI control.

No need to declare default constants.Android studio takes care of default constants at


3
string.xml

Run the application to launch Android emulator and verify the result of the changes done in
4
the application.

Following is the content of the modified main activity file src/MainActivity.java. This file can include
each of the fundamental lifecycle methods.
package com.example.saira_000.myapplication;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends ActionBarActivity {
ToggleButton tg1,tg2;
Button b1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tg1=(ToggleButton)findViewById(R.id.toggleButton);
tg2=(ToggleButton)findViewById(R.id.toggleButton2);

b1=(Button)findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer result = new StringBuffer();
result.append("You have clicked first ON Button-:)
").append(tg1.getText());
result.append("\You have clicked Second ON Button -:)
").append(tg2.getText());
Toast.makeText(MainActivity.this,
result.toString(),Toast.LENGTH_SHORT).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Following will be the content of res/layout/activity_main.xml file −

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_above="@+id/imageButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp" />

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/abc"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="On"
android:id="@+id/toggleButton"
android:checked="true"
android:layout_below="@+id/imageButton"
android:layout_toLeftOf="@+id/imageButton"
android:layout_toStartOf="@+id/imageButton" />

<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Off"
android:id="@+id/toggleButton2"
android:checked="true"
android:layout_alignTop="@+id/toggleButton"
android:layout_alignRight="@+id/textView1"
android:layout_alignEnd="@+id/textView1" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:text="ClickMe"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />

</RelativeLayout>

Following will be the content of res/values/strings.xml to define these new constants −

<?xml version="1.0" encoding="utf-8"?>


<resources>
<string name="app_name">My Application</string>
<string name="action_settings">Settings</string>
</resources>
Following is the default content of AndroidManifest.xml −

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.My Application"
android:versionCode="1"
android:versionName="1.0" >

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<activity
android:name="com.example.My Application.MainActivity"
android:label="@string/app_name" >

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity>

</application>
</manifest>

Let's try to run your My Application application. I assume you had created your AVD while doing
environment setup. To run the app from Android studio, open one of your project's activity files and
click Run icon from the toolbar. Android studio installs the app on your AVD and starts it and if
everything is fine with your setup and applicatio , it will display followi g E ulator wi dow −

The followi g scree will appear −


If you have clicked first on Button, you would get a message on Toast as You have clicked
first ON Button-:) or else if you clicked on second on button, you would get a message on
Toast as You have clicked Second ON Button -:)

Exercise

I will recommend to try above example with different attributes of ToggleButton in Layout
XML file as well at programming time to have different look and feel of the ToggleButton.
Try to make it editable, change to font color, font family, width, textSize etc and see the
result. You can also try above example with multiple ToggleButton controls in one activity.

You might also like