Search code examples
androidswitch-statementfragmentvisible

Enabling Visibility via switch


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());
              }


          }
      });

Solution

  • 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())