java - Android setTextOn not working in onCheckChanged method -


these next 2 statements produce correct screen below left:

                if(ischecked) sw.settext   ("this switch is: on");                  else          sw.settext   ("this switch is: off");  

according text i'm looking at, these next 2 statements should produce screen on left. 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 book wrong settexton , settextoff? have problem in java code or xml?

the settexton , settextoff functions used set labels depending on state of switch.

the text "the switch is: on" label of switch , not convey state of switch.

to achieve result want, need call setshowtext(true):

sw = (switch)findviewbyid(r.id.swish); sw.setshowtext(true); 

or can add in 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"/> 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -