Android togglebutton state sync










0















I am develop an app that can control a reading light, this reading light can be controlled via a built in control app or my app. The issue is I can not sync the toggle button state between the two. I have tried isChecked(), setText(), setTextOn/setTextOff, none works. Thanks



Here is my MainActivity:



import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class MainActivity extends AppCompatActivity implements PacioWebSocketListener.MessageListener

private static final boolean True = true;
private static final boolean False = false;
private final String TAG = "===MainActivity===";

private PacioWebSocketListener pacioWebSocketListener = new PacioWebSocketListener();


private JSONObject obj;
private JSONObject argsObj;
private JSONArray paramsArray;

private String appName;
private String commandName;
private String eventName;


private int READING_LIGHT_CHANGE;

private Button sbCtrlButton;

private FragmentManager fragmentManager = getFragmentManager();

private SeatbackControlFragment seatbackControlFragment = new SeatbackControlFragment();


@Override
public void onMessageReceived(String mMessage)

try
obj = new JSONObject(mMessage);
argsObj = new JSONObject(obj.getString("args"));

if(argsObj.has("params"))
paramsArray = new JSONArray(argsObj.getString("params"));
eventName = argsObj.getString("name");


appName = obj.getString("app");
commandName = obj.getString("command");

if (appName.equals("events"))
if (commandName.equals("handleEvents"))

if(eventName.equals("PSS_READING_LIGHT_CHANGE"))
READING_LIGHT_CHANGE = paramsArray.getInt(0);
if(PSS_READING_LIGHT_CHANGE == 1)
seatbackControlFragment.readingLight.setChecked(True);
else
seatbackControlFragment.readingLight.setChecked(False);





catch (JSONException e)
e.printStackTrace();



@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
pacioWebSocketListener.setPacioWebSocketListener(this);

sbCtrlButton = findViewById(R.id.button);
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(android.R.id.content,seatbackControlFragment);
fragmentTransaction.commit();

sbCtrlButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(android.R.id.content, seatbackControlFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

);




And this is my fragment:



import android.app.Activity;
import android.content.Context;
import android.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ToggleButton;


public class SeatbackControlFragment extends Fragment

public ToggleButton readingLight;
private View view;
private PacioWebSocketListener pacioWebSocketListener = new PacioWebSocketListener();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
view = inflater.inflate(R.layout.fragment_seatback_control, container, false);
readingLight = view.findViewById(R.id.reading_light);

readingLight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
if(isChecked)
pacioWebSocketListener.ws.send(""app":"seatServices", "command":"readingLight", "args":"event":"on"");
else
pacioWebSocketListener.ws.send(""app":"seatServices", "command":"readingLight", "args":"event":"off"");


);

return view;











share|improve this question


























    0















    I am develop an app that can control a reading light, this reading light can be controlled via a built in control app or my app. The issue is I can not sync the toggle button state between the two. I have tried isChecked(), setText(), setTextOn/setTextOff, none works. Thanks



    Here is my MainActivity:



    import android.app.FragmentManager;
    import android.app.FragmentTransaction;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.view.WindowManager;
    import android.widget.Button;
    import android.widget.TextView;

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;


    public class MainActivity extends AppCompatActivity implements PacioWebSocketListener.MessageListener

    private static final boolean True = true;
    private static final boolean False = false;
    private final String TAG = "===MainActivity===";

    private PacioWebSocketListener pacioWebSocketListener = new PacioWebSocketListener();


    private JSONObject obj;
    private JSONObject argsObj;
    private JSONArray paramsArray;

    private String appName;
    private String commandName;
    private String eventName;


    private int READING_LIGHT_CHANGE;

    private Button sbCtrlButton;

    private FragmentManager fragmentManager = getFragmentManager();

    private SeatbackControlFragment seatbackControlFragment = new SeatbackControlFragment();


    @Override
    public void onMessageReceived(String mMessage)

    try
    obj = new JSONObject(mMessage);
    argsObj = new JSONObject(obj.getString("args"));

    if(argsObj.has("params"))
    paramsArray = new JSONArray(argsObj.getString("params"));
    eventName = argsObj.getString("name");


    appName = obj.getString("app");
    commandName = obj.getString("command");

    if (appName.equals("events"))
    if (commandName.equals("handleEvents"))

    if(eventName.equals("PSS_READING_LIGHT_CHANGE"))
    READING_LIGHT_CHANGE = paramsArray.getInt(0);
    if(PSS_READING_LIGHT_CHANGE == 1)
    seatbackControlFragment.readingLight.setChecked(True);
    else
    seatbackControlFragment.readingLight.setChecked(False);





    catch (JSONException e)
    e.printStackTrace();



    @Override
    protected void onCreate(Bundle savedInstanceState)
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    pacioWebSocketListener.setPacioWebSocketListener(this);

    sbCtrlButton = findViewById(R.id.button);
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(android.R.id.content,seatbackControlFragment);
    fragmentTransaction.commit();

    sbCtrlButton.setOnClickListener(new View.OnClickListener()
    @Override
    public void onClick(View v)
    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    fragmentTransaction.replace(android.R.id.content, seatbackControlFragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

    );




    And this is my fragment:



    import android.app.Activity;
    import android.content.Context;
    import android.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.CompoundButton;
    import android.widget.ToggleButton;


    public class SeatbackControlFragment extends Fragment

    public ToggleButton readingLight;
    private View view;
    private PacioWebSocketListener pacioWebSocketListener = new PacioWebSocketListener();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    view = inflater.inflate(R.layout.fragment_seatback_control, container, false);
    readingLight = view.findViewById(R.id.reading_light);

    readingLight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    if(isChecked)
    pacioWebSocketListener.ws.send(""app":"seatServices", "command":"readingLight", "args":"event":"on"");
    else
    pacioWebSocketListener.ws.send(""app":"seatServices", "command":"readingLight", "args":"event":"off"");


    );

    return view;











    share|improve this question
























      0












      0








      0








      I am develop an app that can control a reading light, this reading light can be controlled via a built in control app or my app. The issue is I can not sync the toggle button state between the two. I have tried isChecked(), setText(), setTextOn/setTextOff, none works. Thanks



      Here is my MainActivity:



      import android.app.FragmentManager;
      import android.app.FragmentTransaction;
      import android.support.v7.app.AppCompatActivity;
      import android.util.Log;
      import android.view.View;
      import android.view.WindowManager;
      import android.widget.Button;
      import android.widget.TextView;

      import org.json.JSONArray;
      import org.json.JSONException;
      import org.json.JSONObject;


      public class MainActivity extends AppCompatActivity implements PacioWebSocketListener.MessageListener

      private static final boolean True = true;
      private static final boolean False = false;
      private final String TAG = "===MainActivity===";

      private PacioWebSocketListener pacioWebSocketListener = new PacioWebSocketListener();


      private JSONObject obj;
      private JSONObject argsObj;
      private JSONArray paramsArray;

      private String appName;
      private String commandName;
      private String eventName;


      private int READING_LIGHT_CHANGE;

      private Button sbCtrlButton;

      private FragmentManager fragmentManager = getFragmentManager();

      private SeatbackControlFragment seatbackControlFragment = new SeatbackControlFragment();


      @Override
      public void onMessageReceived(String mMessage)

      try
      obj = new JSONObject(mMessage);
      argsObj = new JSONObject(obj.getString("args"));

      if(argsObj.has("params"))
      paramsArray = new JSONArray(argsObj.getString("params"));
      eventName = argsObj.getString("name");


      appName = obj.getString("app");
      commandName = obj.getString("command");

      if (appName.equals("events"))
      if (commandName.equals("handleEvents"))

      if(eventName.equals("PSS_READING_LIGHT_CHANGE"))
      READING_LIGHT_CHANGE = paramsArray.getInt(0);
      if(PSS_READING_LIGHT_CHANGE == 1)
      seatbackControlFragment.readingLight.setChecked(True);
      else
      seatbackControlFragment.readingLight.setChecked(False);





      catch (JSONException e)
      e.printStackTrace();



      @Override
      protected void onCreate(Bundle savedInstanceState)
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
      pacioWebSocketListener.setPacioWebSocketListener(this);

      sbCtrlButton = findViewById(R.id.button);
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
      fragmentTransaction.add(android.R.id.content,seatbackControlFragment);
      fragmentTransaction.commit();

      sbCtrlButton.setOnClickListener(new View.OnClickListener()
      @Override
      public void onClick(View v)
      FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
      fragmentTransaction.replace(android.R.id.content, seatbackControlFragment);
      fragmentTransaction.addToBackStack(null);
      fragmentTransaction.commit();

      );




      And this is my fragment:



      import android.app.Activity;
      import android.content.Context;
      import android.app.Fragment;
      import android.support.v4.app.FragmentActivity;
      import android.util.Log;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.CompoundButton;
      import android.widget.ToggleButton;


      public class SeatbackControlFragment extends Fragment

      public ToggleButton readingLight;
      private View view;
      private PacioWebSocketListener pacioWebSocketListener = new PacioWebSocketListener();

      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
      view = inflater.inflate(R.layout.fragment_seatback_control, container, false);
      readingLight = view.findViewById(R.id.reading_light);

      readingLight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
      if(isChecked)
      pacioWebSocketListener.ws.send(""app":"seatServices", "command":"readingLight", "args":"event":"on"");
      else
      pacioWebSocketListener.ws.send(""app":"seatServices", "command":"readingLight", "args":"event":"off"");


      );

      return view;











      share|improve this question














      I am develop an app that can control a reading light, this reading light can be controlled via a built in control app or my app. The issue is I can not sync the toggle button state between the two. I have tried isChecked(), setText(), setTextOn/setTextOff, none works. Thanks



      Here is my MainActivity:



      import android.app.FragmentManager;
      import android.app.FragmentTransaction;
      import android.support.v7.app.AppCompatActivity;
      import android.util.Log;
      import android.view.View;
      import android.view.WindowManager;
      import android.widget.Button;
      import android.widget.TextView;

      import org.json.JSONArray;
      import org.json.JSONException;
      import org.json.JSONObject;


      public class MainActivity extends AppCompatActivity implements PacioWebSocketListener.MessageListener

      private static final boolean True = true;
      private static final boolean False = false;
      private final String TAG = "===MainActivity===";

      private PacioWebSocketListener pacioWebSocketListener = new PacioWebSocketListener();


      private JSONObject obj;
      private JSONObject argsObj;
      private JSONArray paramsArray;

      private String appName;
      private String commandName;
      private String eventName;


      private int READING_LIGHT_CHANGE;

      private Button sbCtrlButton;

      private FragmentManager fragmentManager = getFragmentManager();

      private SeatbackControlFragment seatbackControlFragment = new SeatbackControlFragment();


      @Override
      public void onMessageReceived(String mMessage)

      try
      obj = new JSONObject(mMessage);
      argsObj = new JSONObject(obj.getString("args"));

      if(argsObj.has("params"))
      paramsArray = new JSONArray(argsObj.getString("params"));
      eventName = argsObj.getString("name");


      appName = obj.getString("app");
      commandName = obj.getString("command");

      if (appName.equals("events"))
      if (commandName.equals("handleEvents"))

      if(eventName.equals("PSS_READING_LIGHT_CHANGE"))
      READING_LIGHT_CHANGE = paramsArray.getInt(0);
      if(PSS_READING_LIGHT_CHANGE == 1)
      seatbackControlFragment.readingLight.setChecked(True);
      else
      seatbackControlFragment.readingLight.setChecked(False);





      catch (JSONException e)
      e.printStackTrace();



      @Override
      protected void onCreate(Bundle savedInstanceState)
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
      pacioWebSocketListener.setPacioWebSocketListener(this);

      sbCtrlButton = findViewById(R.id.button);
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
      fragmentTransaction.add(android.R.id.content,seatbackControlFragment);
      fragmentTransaction.commit();

      sbCtrlButton.setOnClickListener(new View.OnClickListener()
      @Override
      public void onClick(View v)
      FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
      fragmentTransaction.replace(android.R.id.content, seatbackControlFragment);
      fragmentTransaction.addToBackStack(null);
      fragmentTransaction.commit();

      );




      And this is my fragment:



      import android.app.Activity;
      import android.content.Context;
      import android.app.Fragment;
      import android.support.v4.app.FragmentActivity;
      import android.util.Log;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.CompoundButton;
      import android.widget.ToggleButton;


      public class SeatbackControlFragment extends Fragment

      public ToggleButton readingLight;
      private View view;
      private PacioWebSocketListener pacioWebSocketListener = new PacioWebSocketListener();

      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
      view = inflater.inflate(R.layout.fragment_seatback_control, container, false);
      readingLight = view.findViewById(R.id.reading_light);

      readingLight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
      if(isChecked)
      pacioWebSocketListener.ws.send(""app":"seatServices", "command":"readingLight", "args":"event":"on"");
      else
      pacioWebSocketListener.ws.send(""app":"seatServices", "command":"readingLight", "args":"event":"off"");


      );

      return view;








      android






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 19:03









      S HuynhS Huynh

      32




      32






















          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268514%2fandroid-togglebutton-state-sync%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268514%2fandroid-togglebutton-state-sync%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          Crossroads (UK TV series)

          ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế