2

I am trying to disable EditText if Switch is off I have made If-else for it. But my CompoundButton buttonView cannot be resolved. What can be the issue? Here's my code:

public class MainActivity extends AppCompatActivity {

        public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
        EditText editText = (EditText) findViewById(R.id.editText);
        /** Called when the user taps the Send button */
        public void sendMessage(View view) {
            // Do something in response to button
            Intent newxml = new Intent(this, DisplayMessageActivity.class);
            String message = editText.getText().toString();
            newxml.putExtra(EXTRA_MESSAGE, message);
            startActivity(newxml);
            TextView displayText = (TextView) findViewById(R.id.displayText);
            displayText.setText(message);
            String someText = "Your Message";
            TextView yourMessage = (TextView) findViewById(R.id.yourMessage);
            yourMessage.setText(someText);
        }
        final Switch switch1 = (Switch) findViewById(R.id.switch1);
        switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // true if the switch is in the On position
                if(isChecked){
                    editText.setFocusableInTouchMode(true);
                    editText.setFocusable(true);
                }else{
                    editText.setFocusableInTouchMode(false);
                    editText.setFocusable(false);
                }
            }
        });
    }

Error image Code Image

Yash
  • 369
  • 5
  • 18
  • Is it unresolved in editor or while building/running it ? Please also share the error appearing of 'unresolved'. – SHS Dec 01 '18 at 05:16
  • Its is appearing in both editor and compiler – Yash Dec 01 '18 at 05:21
  • Is it possible by you to post this error on stackoverflow. written or screenshot. it will help us to find the cause of it. ( while building the error appears in 'Build' console. While running it appears in 'Debug' or 'Logcat' console ) – SHS Dec 01 '18 at 05:24
  • Ok I am adding it as image.. – Yash Dec 01 '18 at 05:25
  • You may alspo go through these error links if the error is same https://stackoverflow.com/a/44413351/2641380 or https://stackoverflow.com/q/29844990/2641380 – SHS Dec 01 '18 at 05:29
  • Do you think my `switch` is in `oncreate` method. – Yash Dec 01 '18 at 05:32
  • I have added images of my code and error. – Yash Dec 01 '18 at 05:40
  • good, you may have found the error. shift code of --> final Switch switch1 ... and it's onChackedChanged listener code inside onCreate(). If you are not using switch1 variable at any other places, this could work. – SHS Dec 01 '18 at 05:41
  • Can you please make a code in "Answer" column, I did'nt understood what you told. I request... – Yash Dec 01 '18 at 05:45
  • It is stoping again and again in android! – Yash Dec 01 '18 at 05:51
  • use SwitchCompat instead of Switch. – karan Dec 01 '18 at 06:15

1 Answers1

1

I have rewritten the portion of code below. You may try it at your end.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Switch switch1 = (Switch) findViewById(R.id.switch1);
        switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // true if the switch is in the On position
                if(isChecked){
                    editText.setFocusableInTouchMode(true);
                    editText.setFocusable(true);
                }else{
                    editText.setFocusableInTouchMode(false);
                    editText.setFocusable(false);
                }
            }
        });

    }

..... then other code.

SHS
  • 1,414
  • 4
  • 26
  • 43