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