2

I currently have it set up so there are a couple TextViews:

  • One of them is updated when one button is clicked, the other button does not alter it when clicked.

  • The other TextViews display a number and I would like them to be altered when either one of the buttons is clicked, but currently all of the TextViews are visible and the numbers are not altered.

I would like the other TextViews (num1-num3 below) to be invisible initially, then when the user clicks either one of the buttons, the TextViews become visible and their values are updated by a method that I have written.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_name);
    num1 = randNum();
    num1 = alterNum(num1);      
    num1View = (TextView) findViewById(R.id.number1); 
    num1View.setText("Num1 Number: " + String.valueOf(num1));

    num2 = randNum();
    num2 = alterNum(num2);
    num2View = (TextView) findViewById(R.id.number2); 
    num2View.setText("Num2 Number: " + String.valueOf(num2));

    num3 = randNum();
    num3 = alterNum(num3);
    num3View = (TextView) findViewById(R.id.number3); 
    num3View.setText("Num3 Number: " + String.valueOf(num3));

    // This one is always visible, the ones above should be invisible
    // and appear onClick
    currentNum = randNum();
    myTextView = (TextView) findViewById(R.id.current_number); 
    myTextView.setText("Current Number: " + String.valueOf(currentNum));
    okButton = (Button) findViewById(R.id.ok_button);
    okButton.setOnClickListener(this);
    changeButton = (Button) findViewById(R.id.change_button);
    changeButton.setOnClickListener(this);
}

My onClick:

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.num_confirmation:
        //do nothing
        // do not let user hit buttons more than once (either case)
        changeButton.setEnabled(false);
        okButton.setEnabled(false);
        break;
    case R.id.swap_button:
        currentNum = alterNum();
        myTextView.setText("Current Number: " + String.valueOf(currentNum));
        // do not let user hit buttons more than once (either case)
        swapButton.setEnabled(false);
        okButton.setEnabled(false);
        break;
    default:
        break;
    }
}

How is this done?

lord_sneed
  • 702
  • 3
  • 12
  • 24

2 Answers2

5

set the initial value of "visible" to "invisible" in your xml layout file for textViews that should be invisible and in your onClick method alter its text valie and change visibility:

 myTextView.setVisibility(View.VISIBLE);

here is similar question that should help: How to change visibility of layout programaticly

Community
  • 1
  • 1
fgeorgiew
  • 1,182
  • 1
  • 8
  • 25
  • Excellent. Thanks! Small side question with regards to altering the numbers for num1-num3: Are those statements correct in the onCreate? Or do I need to move them to onClick or elsewhere? (i.e. An initial number should be calculated, then when either button is clicked, the number should be altered and the altered number should be displayed onClick.) – lord_sneed Dec 09 '12 at 19:33
  • yop, you can do it in onCreate, but it in onClick will calculate values only when they will be needed. – fgeorgiew Dec 09 '12 at 19:39
2
yourTextView.setVisibility(View.VISIBLE);

This makes your textview visible.

yourTextView.setVisibility(View.INVISIBLE);

This makes your textview invisible but keeps the layout.

yourTextView.setVisibility(View.GONE);

This removes it so other Views can rearrange. You can still call View.VISIBLE to make it appear again.

So for example you could put this after you define the TextView (findviewbyid)

 num1View.setVisibility(View.INVISIBLE);

Then where you want it to come back:

num1View.setVisibility(View.VISIBLE); 
AndroidPenguin
  • 3,435
  • 2
  • 18
  • 40