Solution 1 :
you are doing it wrong you cant use setchecked() on switch
Problem :
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.messaging.FirebaseMessaging;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.content.ClipboardManager;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
public class MainActivity extends AppCompatActivity {
Button button;
String token;
Button on,on2;
Button off,off2;
TextView distance;
DatabaseReference dref;
String status;
//FirebaseFirestore firestore;
//Button btn3 ,btn4;
TextView sens1,sens2,sens3,sens4;
TextView tx2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText tx=(EditText) findViewById(R.id.txt);
Switch control=findViewById(R.id.control1);
CheckBox ch1=(CheckBox)findViewById(R.id.chk1);
tx2=(TextView) findViewById(R.id.txt2);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button);
sens1=(TextView) findViewById(R.id.rsens1);
sens2=(TextView) findViewById(R.id.rsens2);
sens3=(TextView) findViewById(R.id.rsens3);
sens4=(TextView) findViewById(R.id.rsens4);
System.out.println("SWITCH IS"+control);
String app_server_url="url";
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("LED_STATUS");
dref= FirebaseDatabase.getInstance().getReference();
dref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
status=dataSnapshot.child("sensor1").getValue().toString();
sens1.setText(status);
status=dataSnapshot.child("sensor2").getValue().toString();
sens2.setText(status);
status=dataSnapshot.child("sensor3").getValue().toString();
sens3.setText(status);
status=dataSnapshot.child("sensor4").getValue().toString();
sens4.setText(status);
status=dataSnapshot.child("control1").getValue().toString();
if(Integer.parseInt(status)==1){
control.setChecked(true);
}
else{
control.setChecked(false);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
control.setChecked(true);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseMessaging.getInstance ().getToken ().addOnCompleteListener (task -> {
Log.e("spalsh",task.getResult());
token=task.getResult();
System.out.println("tokenis"+token);
tx.setText("t");
setClipboard(getApplicationContext(),token);
});
StringRequest stringRequest=new StringRequest(Request.Method.POST, app_server_url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
})
{
@Nullable
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params=new HashMap<String,String>();
params.put("fcm_token",token);
return params;
}
};
MySingleton.getmInstance(MainActivity.this).addToRequest(stringRequest);
}
});
}
this is my code i want to set the switch to true but it doesnt work, i want to read value from firebase and based on that i want to set switch to true or false
i also tried to used setchecked and setselected on checkbox and switch and radiobutton but nothing work , what im doing wrong ?
<androidx.constraintlayout.widget.ConstraintLayout xmlns_android="http://schemas.android.com/apk/res/android"
xmlns_app="http://schemas.android.com/apk/res-auto"
xmlns_tools="http://schemas.android.com/tools"
android_layout_width="match_parent"
android_layout_height="wrap_content"
tools_context=".MainActivity">
<LinearLayout
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_orientation="vertical">
<Button
android_id="@+id/button"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="Button" />
<EditText
android_id="@+id/txt"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_ems="10" />
<TextView
android_id="@+id/txt2"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="TextView" />
<LinearLayout
android_layout_width="wrap_content"
android_layout_height="match_parent"
android_orientation="horizontal">
<TextView
android_id="@+id/textView"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_width="500px"
android_text="sensor1" />
<TextView
android_id="@+id/rsens1"
android_layout_width="match_parent"
android_layout_height="match_parent"
android_width="500px"
android_text="rsens1" />
</LinearLayout>
<LinearLayout
android_layout_width="wrap_content"
android_layout_height="match_parent"
android_orientation="horizontal">
<TextView
android_id="@+id/textView6"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_weight="1"
android_width="500px"
android_text="sensor2" />
<TextView
android_id="@+id/rsens2"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_weight="1"
android_text="rsens2" />
</LinearLayout>
<LinearLayout
android_layout_width="match_parent"
android_layout_height="match_parent"
android_orientation="horizontal">
<TextView
android_id="@+id/textView3"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_weight="1"
android_text="sensor3" />
<TextView
android_id="@+id/rsens3"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_weight="1"
android_text="rsens3" />
</LinearLayout>
<LinearLayout
android_layout_width="match_parent"
android_layout_height="match_parent"
android_orientation="horizontal">
<TextView
android_id="@+id/textView8"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_weight="1"
android_text="sensor4" />
<TextView
android_id="@+id/rsens4"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_weight="1"
android_text="rsens4" />
<CheckBox
android_id="@+id/chk1"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_weight="1"
android_text="CheckBox" />
</LinearLayout>
<Switch
android_id="@+id/control1"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="control1"
tools_checked="false" />
<Switch
android_id="@+id/control4"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="control4" />
<Switch
android_id="@+id/control3"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="control3" />
<Switch
android_id="@+id/control2"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="control2" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
this is my activity_main.xml
Comments
Comment posted by Kozmotronik
Are you getting any warnings in compile time or any exceptions in runtime?
Comment posted by MOVIES FX
no im not getting any exceptions
Comment posted by Mike M.
Remove the second
Comment posted by edit
Your answer could be improved with additional supporting information. Please