, I have just started learning java for android, I have set the TextVisibility of a textfield to GONE, I need the TextVisibilty to change to VISIBLE after 7 seconds from Button click, I had searched the internet and could not find anything useful or I could'nt understand. So, Please help me.
Asked
Active
Viewed 46 times
2 Answers
0
Try
final TextView yourText = (TextView) findViewById(R.id.yourTextId);
yourText.setVisibility(View.GONE);
yourText.postDelayed(new Runnable() {
@Override
public void run() {
yourText.setVisibility(View.VISIBLE);
}
}, 7 * 1000);
in your button click listener. For example, in OnCreate() of your activity:
final TextView yourText = (TextView) findViewById(R.id.yourTextId);
Button yourButton = (Button) findViewById(R.id.yourButtonId);
yourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
yourText.setVisibility(View.GONE);
yourText.postDelayed(new Runnable() {
@Override
public void run() {
yourText.setVisibility(View.VISIBLE);
}
}, 7 * 1000);
}
});

Handy
- 13
- 4
-
Another help dude, can I play a audio track after a period of time, If yes, Can you please help me ? – Kdvinmk Oct 04 '14 at 11:15
0
On android, when you want something to be executed after some period of time and in other thread you generally use a Handler like so:
final TextView yourText = (TextView) findViewById(R.id.yourTextId);
Handler handler = new Handler();
handler.postDelayed(new Runnable()) {
@Override
public void run() {
yourText.setVisibility(View.VISIBLE);
}
}, 7000);
If you want, you could implement the Runnable class in your Activity, so you don't have to encapsulate anything:
public class MyActivity implements Runnable
And then you pass this to de postDelayed method first argument,
handler.postDelayed(this, 7000);
and implement the run method inside your activity:
public void onCreate() {
super.onCreate();
setContentView(R.id.your_layout);
/* some code */
}
@Override
public void run() {
yourText.setVisibility(View.VISIBLE);
}
This will execute the code inside the run method after the period of time you set in the second argument of postDelayed, and inside another thread, so you could continue to make things inside your MainActivity like clicking buttons or something else.
EDIT with sample code for comments:
Handler handler1 = new Handler();
Handler handler2 = new Handler();
handler1.postDelayed(new Runnable() {
tvt1.setVisibility(View.VISIBLE);
}, 7000);
handler2.postDelayed(new Runnable() {
tvt2.setVisibility(View.VISIBLE);
}, 12000);

tibuurcio
- 766
- 8
- 11
-
Another help dude, can I play a audio track after a period of time, If yes, Can you please help me ? – Kdvinmk Oct 04 '14 at 11:19
-
@Kdvinmk The logic to play after a period of time is just the same, just put what you want to do inside the run method. I don't have the knowledge on how to handling audio since I'm kind of new to android, but you should get it from developers site, there's a section of training for [Managing Audio Playback](http://developer.android.com/training/managing-audio/index.html). – tibuurcio Oct 04 '14 at 11:24
-
Thank you!! If iI want to add another text after 12 seconds, its just follow this again right ? – Kdvinmk Oct 04 '14 at 11:32
-
Yes, just change 7000 to whatever time you want in milliseconds, in that case, 12000. – tibuurcio Oct 04 '14 at 11:36
-
I tried that but its not working. My code: http://gyazo.com/bce4d6da418ba9873da8edb1de8c8da1 – Kdvinmk Oct 04 '14 at 11:38
-
tvt2 is another textView? In this case you're just changing it's visibility and not "adding" another textView. If you want to add it programmatically you could try [this](http://stackoverflow.com/questions/2395769/how-to-programmatically-add-views-to-views). – tibuurcio Oct 04 '14 at 11:44
-
Please can you explain me about this ? sorry fr the trouble, the first textview is changing its visibility after 7 seconds, like that i want another textview to channge its visibility after 12 seconds – Kdvinmk Oct 04 '14 at 11:50
-
Assuming you've already the textViews referecens in your code, try to use the code a posted on the answer to create a handler and use the postDelayed method on it instead of doing it directly from the view like tvt.postDelayed. Also, create 2 handlers, one for each task. It should work. I'll edit my answer post with some code. – tibuurcio Oct 04 '14 at 11:57