I am trying to write a simple code to switch activity when unlock the device (if app is open) for that I have to switch another activity I have tried this code.But even that code it shows the current screen for a fraction of second. what should I do.
#Override
protected void onResume() {
// This is the second main Layout after main layout (which has
// background image)
mainLayout.setVisibility(View.INVISIBLE);
super.onResume();
checkActivity();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
SimpleChatMainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
//Visible after 1 second
mainLayout.setVisibility(View.VISIBLE);
}
});
}
}, 1000);
// Some code
}
Related
Goal:
When you start the android app, the button should not be displayed after 5 seconds.
Problem:
The code doesn't work and what part am I missing?
Info:
*Im new in android
*The code is inspired from this page Android - Hide button during an onClick action
Thank you!
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button2 = (Button) findViewById(R.id.btn_test);
button2.setVisibility(GONE);
new Thread(new Runnable() {
#Override
public void run() {
try
{
//dummy delay for 5 second
Thread.sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
runOnUiThread(new Runnable() { //resetting the visibility of the button
#Override
public void run() {
//manipulating UI components from outside of the UI Thread require a call to runOnUiThread
button2.setVisibility(VISIBLE);
}
});
}
}).start();
}
}
This can be achieved in a simpler way. If your needed sequence is:
Start the app -> Display a button -> Wait 5 seconds -> Hide the button
final Button button2 = (Button) findViewById(R.id.btn_test);
button2.postDelayed(new Runnable() {
#Override
public void run() {
if (!isDestroyed() && !isFinishing()) {
button2.setVisibility(View.GONE);
}
}
},5000);
Otherwise, if you should display the button after 5 seconds after app launch, then just set button's visibility to GONE in your layout and change button2.setVisibility(View.GONE) to button2.setVisibility(View.VISIBLE) inside post delayed action
You need to set a listener to start your command, onCreate is the creation.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Button button2 = (Button) findViewById(R.id.btn_test);
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
button2.setVisibility(GONE);
new Thread(new Runnable() {
#Override
public void run() {
try
{
//dummy delay for 5 second
Thread.sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
runOnUiThread(new Runnable() { //resetting the visibility of the button
#Override
public void run() {
//manipulating UI components from outside of the UI Thread require a call to runOnUiThread
button2.setVisibility(VISIBLE);
}
});
}
}).start();
}
this code will hide the button AFTER onClick, start the thread, and after 5 seconds it will appear again.
i'm working with bunch of activities, i need to implement auto time out when my app is in inactivity state. my scenario is when i login my timer need to start if i didn't do anything after i logged in.
my timer need to start with last interaction with application,
my timer need to start when my app goes to background and screen off cases, and also
my timer need to stop when i log out from application.
i tried with below code from Here only:
public class MyBaseActivity extends Activity {
public static final long DISCONNECT_TIMEOUT = 300000; // 5 min = 5 * 60 * 1000 ms
private Handler disconnectHandler = new Handler(){
public void handleMessage(Message msg) {
}
};
private Runnable disconnectCallback = new Runnable() {
#Override
public void run() {
// Perform any required operation on disconnect
}
};
public void resetDisconnectTimer(){
disconnectHandler.removeCallbacks(disconnectCallback);
disconnectHandler.postDelayed(disconnectCallback, DISCONNECT_TIMEOUT);
}
public void stopDisconnectTimer(){
disconnectHandler.removeCallbacks(disconnectCallback);
}
#Override
public void onUserInteraction(){
resetDisconnectTimer();
}
#Override
public void onResume() {
super.onResume();
resetDisconnectTimer();
}
#Override
public void onStop() {
super.onStop();
stopDisconnectTimer();
}
}
But this code doesn't starts my timer when my app goes background or app screen offs, in those cases timer stopped.
Please give me ur valuable suggestions and help me for saving my time on things.Thanks all.
Need to detect whether the app is in background state or not
#Override
public void onStop() {
if (Constant.isAppIsInBackground(this)) {
stopDisconnectTimer();
resetDisconnectTimer();
}else {
stopDisconnectTimer();
}
super.onStop();
//stopDisconnectTimer();
}
Here isAppisBackground() is the method for detecting background state
I am displaying one activity in this one image view is there and only image is display to the user. user will not allow to do any task such as like or comment.
the screen will on 5 seconds hold.
after that controls on that screen will be enabled and workable
even user does not allow to go back from the screen till the 5 seconds will not be completed.
how do i implement this functionality
I have written the code
public class AdvertActivity extends Activity implements OnClickListener {
ImageView ivEAdvertUserImage,ivEAdvertImage;
TextView tvEAdvertDislike,tvEAdvertLike,tvEAdvertHeading;
EditText etEAdvertComments;
Button btnEAdvertSubmit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_advert);
InitUI();
tvEAdvertLike.setOnClickListener(this);
tvEAdvertDislike.setOnClickListener(this);
btnEAdvertSubmit.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.tvEAdvertLike:
tvEAdvertLike.setTextColor(color.Blue);
break;
case R.id.tvEAdvertDislike:
tvEAdvertDislike.setTextColor(color.Blue);
etEAdvertComments.setEnabled(true);
btnEAdvertSubmit.setEnabled(true);
break;
case R.id.btnEAdvertSubmit:
Intent in =new Intent(AdvertActivity.this,EAdFragment.class);
startActivity(in);
finish();
break;
default:
break;
}
}
public void InitUI(){
ivEAdvertUserImage=(ImageView)findViewById(R.id.ivEAdvertUserImage);
ivEAdvertImage=(ImageView)findViewById(R.id.ivEAdvertImage);
etEAdvertComments=(EditText)findViewById(R.id.etEAdvertComments);
tvEAdvertDislike=(TextView)findViewById(R.id.tvEAdvertDislike);
tvEAdvertLike=(TextView)findViewById(R.id.tvEAdvertLike);
tvEAdvertHeading=(TextView)findViewById(R.id.tvEAdvertHeading);
btnEAdvertSubmit=(Button)findViewById(R.id.btnEAdvertSubmit);
}
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_advert);
InitUI();
final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
// Will be called after 5000ms
tvEAdvertLike.setOnClickListener(this);
tvEAdvertDislike.setOnClickListener(this);
btnEAdvertSubmit.setOnClickListener(this);
}
}, 5000);
}
This way your Listeners will be set after 5 seconds. Before that, the buttons etc. will have no effect if someone clicks them.
(Can not test my code right now, but it should work)
So I have an activity which runs a simple snakes and ladders game, and I want to allow the player to click a button and move, which would subsequently be followed by the computer moving. My problem is, that once the player moves, the computer immediately makes its move.
Instead I want the activity to wait before the computer makes its move. I've looked around a lot and found that waiting involves using a thread, but I have failed to implement it without my app crashing
My attempt at declaring a thread:
final Runnable runnable = new Runnable() {
#Override
public void run()
{
while (true)
{
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
};
Thread thread= new Thread(runnable);
My onClick() method for the button:
Button rollButton = (Button) (findViewById(R.id.rollButton));
rollButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//executes only if current player is a human
if(GAME_BOARD.getCurrentPlayer().isHuman()) {
GAME_BOARD.rollDie(); // rolls the die
showDie(GAME_BOARD); // displays die on screen
GAME_BOARD.move(); // moves the player and sets next player as current player
playerTurn.setText(GAME_BOARD.getCurrentPlayer().getName() +"'s turn");// sets TextView to display who's player's turn it is
thread.start();
if(!GAME_BOARD.getCurrentPlayer().isHuman()) {
GAME_BOARD.rollDie();
showDie(GAME_BOARD);
GAME_BOARD.move();
playerTurn.setText(GAME_BOARD.getCurrentPlayer().getName() +"'s turn");
}
}
}
});
So again, my question is, how do I make it so that before the second if statement executes, the activity waits, say 4 seconds?
you can use an Handler.postDeleayed. You have to provide a Runnable to execut and the delay period in milliseconds. The Runnable will be executed on the UI Thread
You can use Handle to post your task defined in runnable after some delay
use this
Inside Activity define
Handler hand = new Handler();
and define your task in runnable like this
Runnable run = new Runnable() {
#Override
public void run() {
**your Task here.......**
}
};
In on click event
btnStartShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
hand.postDelayed(run,3000); // For delay three seconds
}
});
also remove all pending messages by calling following sentence at appropriate place
hand.removeCallbacksAndMessages(null);
Use a CountDownTimer:
CountDownTimer timer = new CountDownTimer(4000, 1000) {
#Override
public void onFinish() {
*function containing the second statement*
}
#Override
public void onTick(long millisLeft) {
// not ticking
}
};
and in the onClick method in the click listener use:
timer.start();
Try the following,
rollButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Runnable runnable = new Runnable() {
#Override
public void run() {
// whatever you would like to implement when or after clicking rollButton
}
};
rollButton.postDelayed(runnable, 5000); //Delay for 5 seconds to show the result
}
I would like the user to press a button.
When the onClickListener for the button is called, I would like to display a spinner until the webpage is fetched.
I don't want to use a AsyncTask because it can only be run once and more than likely the user will click the button more than once after the first url results are retreived.
So how would I go about doing this with this onClickListener?
findIT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
item = game.getText().toString();
getUserPreference();
itemLookup.loadUrl(url);
}
});
You can still use AsyncTask<>, you just have to create a new one each time.
public void onClick(View v) {
new DoWhateverTask().execute();
}
Otherwise you can do it yourself using Handlers. Assuming you already have Handlers defined for the UI and a worker thread:
public void onClick(View v) {
showProgress();
workerThreadHandler.Post(new Runnable() {
public void run() {
doNetworkStuff(); // Which will post progress to the UI thread if needed.
mainThreadHandler.Post(new Runnable() {
public void run() {
hideProgress();
}
});
}
});
}
Personally, I think AsyncTask is a nicer solution.