user2938439
user2938439

Reputation: 3

Setting a TextView to visible from invisible based on an if statement

I have several strings of text on a screen that are set to invisible when the application starts. When a button is clicked on another screen, I want a specific string to become visible. Ultimately I want to have a few strings, of the several, become visible as a result of clicking this button.

    public void buttona0Click(View view){
    setContentView(R.layout.report_screen);
    buttonClicked2 = 1;
    if(buttonClicked1==1){
        setVisibility(R.id.textView2.VISIBLE);
        }
    }

I am primarily looking for guidance on this line

    setVisibility(R.id.textView2.VISIBLE);

I am new to programming in general, so I don't know if what I've said makes sense to most of you. Is .setText an alternative?

Upvotes: 0

Views: 1180

Answers (4)

QuokMoon
QuokMoon

Reputation: 4425

Write your own algo:

  1. Use one boolean
  2. view VISIBLE and GONE based own above boolean variable

Upvotes: 1

codeMagic
codeMagic

Reputation: 44571

You need to instantiate your TextView first. So the easiest way to begin is to declare them before onCreate() and outside of any other method so they are member variables

public class MyActivity extends Activity
{
    TextView tv1, tv2, etc...;

    public void onCreate(...)
    {
        super.onCreate(...);
        setContentView(R.layout.my_layout);
        tv1 = (TextView) findViewById(R.id.textView1);
        ...
     }

Then in your onClick() change the Visibility which takes an int value

 public void buttona0Click(View view){
     buttonClicked2 = 1;
     if(buttonClicked1==1){
         tv1.setVisibility(View.VISIBLE);
    }
}

Note: please don't include the "..." above in your code. That is just omitted code that I assume you know how to handle already. Also, I took out setContentView() from the onClick() method because typically this should only be done once in onCreate().

I'm not sure about the logic inside there because I don't know what the buttonClicked1 variables are for but that is how to do the Visibility.

setVisibility() Docs

Is .setText an alternative?

This will simply set the text so if you have it already set in your xml then you don't need to...you can just change the Visibility as you are trying to do. If you haven't set it then you will need to with something like

tv1.setText("Hello World");  // input your own String or String resource

Upvotes: 0

Manishika
Manishika

Reputation: 5564

First findViewById of the TextView and in onClick function of your button set visibility of that textView as VISIBLE

TextView textView = (TextView) findViewById(R.id.textView1);


 public void buttona0Click(View view){
     buttonClicked2 = 1;
    if(buttonClicked1==1){
        textView.setVisibility(view.VISIBLE);
        }
}

Upvotes: 0

Aakash
Aakash

Reputation: 1900

Make sure you call setVisibility(View.GONE) for all your TextViews in onCreate() after setContentView(). Then in your if clause set the visibility of selected textview to View.VISIBLE - textview.setVisibility(View.VISIBLE)

Upvotes: 0

Related Questions