Set/Change Toggle Button above ON-OFF text in android using XML

How to change toggle button ON,OFF text using android:textOn=””, android:textOff=”” attributes.

In this tutorial we are creating toggle button and changing the toggle button above default text to some else text. This will completely modify the toggle button. So here is the complete step by step tutorial for Set/Change Toggle Button above ON-OFF text in android using XML.

android-project-download-code-button

How to Set/Change Toggle Button above ON-OFF text in android using XML.

Code for MainActivity.java file.

 package com.changetogglebuttonabovetext_android_examples.com;

import android.app.Activity;
import android.os.Bundle;


public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }
}

Code for activity_main.xml layout 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="com.changetogglebuttonabovetext_android_examples.com.MainActivity" >

 <ToggleButton
 android:id="@+id/toggleButton1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:textOn="Toggle Button ON"
 android:textOff="Toggle Button OFF" />

</RelativeLayout>

Screenshots:

Set/Change Toggle Button above ON-OFF text in android using XML

 

ToggleButton-1

Click here to download Set/Change Toggle Button above ON-OFF text in android using XML project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *