Android togglebutton state sync
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
add a comment |
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
add a comment |
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
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
android
asked Nov 12 '18 at 19:03
S HuynhS Huynh
32
32
add a comment |
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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