Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check TextView Visibility using IF

Tags:

android

I have an onCheckChangedListener to show a textView depending on which radio button is selected. I have 1 question and 1 problem that I was wondering if anyone could help me with.

Question: Can you set the radio groups default check value to NO radio button so that none are checked to start with?

Problem: How can I use an IF statement to determine whether a text view is already "visible" and If it is then set it to "gone", I will include my current code.

code:

    @Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
    switch(arg1){
    case R.id.rfolk1:
        Folk1.start();
        TvFolk1.setVisibility(View.VISIBLE);

        TvFolk2.setVisibility(View.GONE);

        Play.setVisibility(View.VISIBLE);
        Pause.setVisibility(View.VISIBLE);
        Stop.setVisibility(View.VISIBLE);

        Play2.setVisibility(View.GONE);
        Pause2.setVisibility(View.GONE);
        Stop2.setVisibility(View.GONE);
        break;
    case R.id.rfolk2:
        Folk2.start();
        TvFolk2.setVisibility(View.VISIBLE);
        TvFolk1.setVisibility(View.GONE);

        Play2.setVisibility(View.VISIBLE);
        Pause2.setVisibility(View.VISIBLE);
        Stop2.setVisibility(View.VISIBLE);

        Play.setVisibility(View.GONE);
        Pause.setVisibility(View.GONE);
        Stop.setVisibility(View.GONE);
        break;
    }
like image 595
Thomas Avatar asked Mar 12 '13 14:03

Thomas


2 Answers

The View class includes a getVisibility() method. Compare that:

Eg:

if (TvFolk1.getVisibility() == View.VISIBLE)
  TvFolk2.setVisibility(View.GONE);

To shorten down code, you can also make a method:

public static void goneIfVisible (View v)
{
  if (v.getVisibility() == View.VISIBLE)
      v.setVisibility(View.GONE);
}

And keep in mind in Java, variables are lowercased, only use uppercase for class names.

like image 198
A--C Avatar answered Oct 21 '22 00:10

A--C


// If TextView is already showing and you want to hide it.

if (TvFolk1.isShown()) {
    TvFolk2.setVisibility(View.INVISIBLE);
}

// For uncheck all radio button from radio button groups

RadioGroup rgButton = (RadioGroup)findViewById(R.id.radiobuttongroup);
rgButton.clearCheck();
like image 28
Daud Arfin Avatar answered Oct 21 '22 00:10

Daud Arfin