lord_sneed
lord_sneed

Reputation: 824

Android: How to Show TextView When Button is Clicked?

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

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?

Upvotes: 2

Views: 13754

Answers (2)

fgeorgiew
fgeorgiew

Reputation: 1204

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

Upvotes: 6

AndroidPenguin
AndroidPenguin

Reputation: 3453

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

Upvotes: 2

Related Questions