Search code examples
javaandroidsettext

Android setTextOn not working in onCheckChanged method


These next two statements produce correct screen below left:

                if(isChecked) sw.setText   ("This switch is: On"); 
                else          sw.setText   ("This switch is: Off"); 

According to the text I'm looking at, these next two statements should produce screen on left. But they produce incorrect screen below right:

                if(isChecked) sw.setTextOn ("This switch is: On"); 
                else          sw.setTextOff("This switch is: Off"); 

enter image description here enter image description here

The code:

package com.dslomer64.checkbox;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Switch;


public class MainActivity extends ActionBarActivity{//} implements CompoundButton.OnCheckedChangeListener {
    CheckBox cb;
    Switch sw;

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

        cb = (CheckBox)findViewById(R.id.check);
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    cb.setText("checked");
                else
                    cb.setText("Unchecked");
            }
        });

        sw = (Switch)findViewById(R.id.swish);
        sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    sw.setTextOn("This switch is: On"); ////////////
                else
                    sw.setTextOff("This switch is: Off"); //////////

            }
        });
    }
}

The xml:

<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:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivity" android:weightSum="1">

    <Switch
        android:layout_width="453dp"
        android:layout_height="100dp"
        android:id="@+id/swish"
        android:layout_gravity="center_vertical"
        android:layout_alignParentTop="true" android:layout_centerHorizontal="true"/>

    <CheckBox
        android:text="Unchecked"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:id="@+id/check"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

</RelativeLayout>

Is the book wrong about setTextOn and setTextOff? Do I have a problem in java code or xml?


Solution

  • The setTextOn and setTextOff functions are to used to set the labels depending on the state of the Switch.

    The text "The switch is: On" is just the label of your Switch and does not convey the state of your Switch.

    To achieve the result that you want, you need to call setShowText(true):

    sw = (Switch)findViewById(R.id.swish);
    sw.setShowText(true);
    

    or you can add it in your XML.

    <Switch
            android:layout_width="453dp"
            android:layout_height="100dp"
            android:id="@+id/swish"
            android:layout_gravity="center_vertical"
            android:layout_alignParentTop="true"     
            android:showText="true"/>