-1

Inside one of my fragments I have edit texts, switches, seek bars ect. I would like one of the switch's 'on' position to enable visibility of an additional edit-text - ect. I have tried a couple different variations like shown below. (Does it matter where I make this rule)? Thanks.

Do I need on start and nonstop like I have for my seek bar or use another argument to determine visibility?

I have tried many variations, so I don't know what code to post, but basically I have tried things like this. XML is pretty standard let me know if you need it..

View view = inflater.inflate(R.layout.life_lay1, container, false);
    seekBar = (SeekBar) view.findViewById(R.id.seekBar1);
    textView1 = (TextView) view.findViewById(R.id.textView1);
    EditText01 = (EditText) view.findViewById(R.id.EditText01);
    //ib1 = (ImageButton) view.findViewById(R.id.ib1);
//  ib1.setOnClickListener(this);
    ib2 = (ImageButton) view.findViewById(R.id.ib2);
    ib2.setOnClickListener(this);
    switch3 = (Switch) view.findViewById(R.id.switch3);

    switch3.setVisibility(View.INVISIBLE);

    if(switch3.getText() =="Yes" ) // have tried string resource as well.
    {
        switch3.setVisibility(View.VISIBLE);
    }
    seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 

      {
          int  progress = 18;
            @Override
          public void onProgressChanged(SeekBar seekBar,int progresValue, boolean fromUser) 
          {
            progress = progresValue;
            if(progresValue < 18)
            {
                 textView1.setText(":  " + 18);
            }
          }

          @Override
          public void onStartTrackingTouch(SeekBar seekBar) 
          {
                textView1.setText(":  " + seekBar.getProgress());
          }

          @Override
          public void onStopTrackingTouch(SeekBar seekBar)
          {
              if(seekBar.getProgress() < 18)
              {
                 textView1.setText(":  "+ 18);
              }
              else
              {
              textView1.setText(":  " + seekBar.getProgress());
              }


          }
      });
droidShot
  • 137
  • 2
  • 12
  • well that != was me just testing, but started out with ==. I have also tried comparing to the string resource as well. my setTextOn is a string res "Yes" and setTextOff is "No" – droidShot Jul 01 '14 at 17:05
  • Have a look here - on how to manage a toggle button or a switch: http://developer.android.com/guide/topics/ui/controls/togglebutton.html – Phantômaxx Jul 01 '14 at 17:12

1 Answers1

1

Switch is not a EditText or TextView so it don't have a getText method.

You should check if it's checked using isChecked which returns true if it's checked.

So it will be

if(switch3.isChecked() ) // have tried string resource as well.

To make it dynamic you should implement a onCheckedChanged listener, so everytime the user changes the state of it will update the visibility.

switch3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            switch3.setVisibility(isChecked ? View.VISIBLE : View.INVISIBLE);
        }
});

But wait, stop! Why you change the visibility of switch3 ? It will make it unable to change it again. Maybe you want to change switch3 with something else?

(p.s remember to check strings use .equals())

Marco Acierno
  • 14,682
  • 8
  • 43
  • 53
  • Thanks for the help & wow just realized I was changing viability of that switch not my edit text -.- (not enough rep to up vote but thanks for the quick post). – droidShot Jul 01 '14 at 17:15
  • If it fixes your problem, click the "V" (under the votes) to accept the answer. – Marco Acierno Jul 01 '14 at 17:16