Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – Switch and checkbox setchecked function not working in android studio

Posted on November 11, 2022 By

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

READ  [FIXED] How to detect swipe between views to make path across them in android java?
Powered by Inline Related Posts

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

Android Tags:android-studio, java

Post navigation

Previous Post: [FIXED] React-native android build fails due to missing files in the gradle cache?
Next Post: Can I use Google storage instead of iCloud?

Related Posts

[FIXED] android – get JSON array data Android
[FIXED] android – How to hide/disable TabLayouts when items on navigation drawer is clicked? Android
[FIXED] reactjs – android upload image not working in facebook messenger SDK webview Android
[FIXED] java – Issue when GroupBy an ArrayList object in Kotlin? Android
[FIXED] java – Didn’t find class AdView Android
[FIXED] android – Dagger/MissingBinding when Injecting Dependencies into a ViewModel Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Quel est le fromage qui porte le nom d’une commune de Côte d’Or et qui est produit en Haute-marne ?
  • Welches ist das beste Senioren Smartphone?
  • What is ISO benefit?
  • What do red bottom shoes represent?
  • Are ultrasonic cleaners safe for electronics?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme