0

how to check switch position in to other fragment

First Fragment

          textView=view.findViewById(R.id.textview_first);

    // check  second fragment switch position and change text 

Second Fragment

    aSwitch=view.findViewById(R.id.switch1);

    aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){

                //Checnge first fragment textview text to checked

            }else {
                //Checnge first fragment textview text to unchecked
            }
        }
    });

2 Answers2

1

What you want to do is this (Communication between Fragments):

https://stackoverflow.com/a/63612656/7968986

With a String (checked/unchecked)

I assume you aren't using ViewModels/LiveData, otherwise you could also solve the problem with those

EDIT:

Just saw that you are using Java, you can find Java examples and the general android guide here:

https://developer.android.com/guide/fragments/communicate#fragment-result

Merthan Erdem
  • 5,598
  • 2
  • 22
  • 29
0

To communicate between two fragments in with java, most times we employ the concept of interface. where we you just use an interface then set the listeners on the fragments class then implement the action listeners on the main class controlling the fragments.

Use this link to see how to set up an interface, attach listeners in the fragments and implement the interface on another class that will be used to pass the information between the two fragments

Andrew Mititi
  • 933
  • 12
  • 11
  • Yes. in the fragment that is listening the interface call. just get the string coming the prior fragment and to `.toUppercase()` to make it into upper case – Andrew Mititi May 15 '21 at 12:32