All! I am pretty new to Developing Android. I have run into many issues already and solved most of them myself, and some by searching here and on other sites. The problem I currently face now, I can't seem to find a solution for. It is close to what others on here have asked, but I can't find anything for my problem.
I am working on the beginning stages of my first big app, in which a user signs in to the fist page and is then allowed access to the rest of the app and features with. I am using basic examples right now of a simple log in app and it works fine, but when I try to delay the first activity from calling the second one until the results are posted, either it posts and doesn't call the second activity, or it doesn't post the results and moves on anyways. I am also currently trying to bundle the results and display them in the second activity. I Will change that later though, I just need to see if it will work right.
Here is my code:(not sure if I'm doing this right. It's my first time!)
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Login button clicked
ok = (Button)findViewById(R.id.btn_login);
ok.setOnClickListener(this);
result = (TextView)findViewById(R.id.lbl_result);
final Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
#Override
public void run() {
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Bundle b = new Bundle();
EditText txt1 = (EditText)findViewById(R.id.editText1);
EditText txt2 = (EditText)findViewById(R.id.lbl_result);
b.putString("ID", txt1.getText().toString());
b.putString("PW", txt2.getText().toString());
final Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
#Override
public void run() {
final Intent myIntent = new Intent(TempActivity.this, TempActivity2.class);
myIntent.putExtras(b);
startActivity(myIntent);
}
}, 3000);
}
});
}
}, 4000);
}
Am I just going about this the wrong way? Any help is greatly appreciated! I hate being a noob! Let me know if anymore information is needed!
Ohhhh... i dont know.. if it is actually right.. but as far as i understand your code... you are using
handler1.postDelayed(new Runnable() {
and in its runnable
public void run() {
ok.setOnClickListener(new View.OnClickListener() {
here.. you are setting onClickListener.. and it will happen after 4 seconds.. because of this line..
}, 4000);
so if you click before 4 seconds.. i think onclicklistener is not being set... so give some time.. like 4 seconds after the application starts
.. and then try clicking... i think then it should work..
Related
I am writing here because I have a very annoying problem in Android Studio.
My "application" is extremely simple, one page with 13 button; All I want is simple: update the first twelve buttons one by one when I click the 13th button.
I would like to see buttons updating with a little interval between each of them, but I can't understand how to do it.
I tried many tricks inside the "onClick" method but I can not figure out how to solve it; what I obtain is that after some time (the time obtained adding up the various "sleep" I put in the function) all the buttons become colored at the same time.
I put my last attempt, but if you have any other way to do that I am willing to change the way to proceed.
int[] buttonIDs = new int[] {R.id.button1, R.id.button2, R.id.button3, R.id.button4, R.id.button5, R.id.button6, R.id.button7,
R.id.button8, R.id.button9, R.id.button10, R.id.button11, R.id.button12 };
int currentI = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button goButton = (Button) findViewById(R.id.button13);
goButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (currentI < buttonIDs.length) {
Button b = (Button) findViewById(buttonIDs[currentI]);
b.setBackgroundColor(Color.parseColor("#FF22FF"));
currentI++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t = new Thread() {
public void run() {
goButton.performClick();
}
};
t.start();
}
}
});
}
The result of this attempt is that the first button become colored and then I get "Application has stopped" error on the Android Emulator.
Thanks in advance
Use this
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 1000ms
}
}, 1000);
The basic idea here is that you need to create a new thread which signals the UI thread to update each button. In between each update, the new thread needs to wait until the UI has a chance to perform the update. I'm not very experienced with this kind of thing. You can most likely do this with a Handler. There might be more high-level tools you can use instead, but I'm not sure.
You definitely do not want to sleep the UI thread. This will only cause the UI to become unresponsive.
I have the Problem that my Android app does not delay a second (or 10 seconds), if I use the postDelayed method..
Basically I would like my program to wait one second after I clicked the button, then update the text on my textview ("READY"), wait another 2 seconds, then update the textview again ("SET") and then it should start another activity (not yet implemented :-) ).
With my code, the programm starts and after I click the button the textview shows the last text ("SET") immediately.. It just does not wait.
What am i doing wrong?
Here is my code:
public class MyCounterActivity extends Activity {
private long mInternval = 100000;
private Handler mHandler;
private Runnable mStatusChecker = new Runnable() {
#Override
public void run() {
//updateInterval(); //change interval
startRepeatingTask();
}
};
void startRepeatingTask(){
mHandler.postDelayed(mStatusChecker, mInternval);
//mStatusChecker.run();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gym_counter);
final TextView tv1 = (TextView) findViewById(R.id.fullscreen_content);
final Button startButton = (Button) findViewById(R.id.startbutton);
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final long up;
EditText textUp = (EditText) findViewById(R.id.editTextUp);
up = Integer.parseInt(textUp.getText().toString());
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//
}
},1000);
Log.d("after 1 runnable", "whaaat");
tv1.setText("Ready");
handler.postDelayed(new Runnable() {
#Override
public void run() {
//
}
}, 2000);
Log.d("after 2nd runnable", "whaaat 2");
//startRepeatingTask();
tv1.setText("SET");
}
});
}
I also tried to run it with the runOnUiThread() (within the onClick(View v) but with with the same result). I expected it to wait 1 second (startRepeatingTask()) and then runs the loop and waits several seconds...
runOnUiThread(new Runnable() {
#Override
public void run() {
startRepeatingTask();
for (int u = 0; u < up; u++){
startRepeatingTask();
}
}
}
});
Hope my description makes sense :-).
Thank you for your help!
EDIT:
I was now able to find a solution for my first problem. The answer from #mad in this post helpded me: How to start a different activity with some delay after pressing a button in android?
(Thats probably the same thing that #laalto tried to tell me. Thanks for the hint!)
In the onClick()
tv1.setText("READY");
mHandler.postDelayed(mDelay1, 2000);
And then the Runnable
private Runnable mDelay1 = new Runnable() {
#Override
public void run() {
if (tv1.getText()=="READY")
tv1.setText("SET");
}
};
BUT:
If i want to refresh the text on my Textview after every second, how do i do that? I cant just call mHandler.postDelayed() several times.. Any help is appreciated.
When you call postDelayed(), it just places the Runnable in a queue and returns immediately. It does not wait for the runnable to be executed.
If you need something to happen after a delay, put the code in the run() method of the runnable.
Whenever you call something like Thread.start(), handler.postDelayed, view.postDelayed, AsynchTask, TimerTask .. you enter the world of threading or you might call it parallel computing.
So there can be multiple threads ("codes") running at the same time.
When you are inside your Activity it is running in a Thread that is calld UI-thread or main thread. All graphics is handled in that thread and that thread alone.
Do NEVER wait in the UI-thread!
Example: you have a button that switches color from say gray to yellow on pressing it. Now you enter a Thread.sleep(10000); - waiting 10 seconds at the start of your onClick.
You will then see that the button stays yellow (=pressed) for 10 seconds even if you only pressed very shortly. Also: if you overdo it android os will become angry and post the user if he wants to force-close your app.
So what happens on handler.postDelayed?
Android will very quickly open a thread that runs in the background parallel to your UI thread. So in some nanoseconds it has done that and will execute the next command in UI thread (in the example above it is Log.d). In the background it will wait and count the millis until time is up. Then any code that is inside the runnable.run method will again be executed in the ui-thread after the wait.
Note also: postDelayed will not be super precise with the wait time as usually the ui-thread is quite buisy and when the wait time is up it may have something else to do. Your runnable code will be added to a queue and executed when ui-thread is ready again. All this happens without you having anything to do about it.
Also:
Remember to work with try/catch inside the runnable.run as many things can happen while waiting - for example user could press Home button closing your app - so the ui-element you wanted to change after the wait could already been destroyed.
I am trying to show invisible ImageViews on OnCreate method. I have a bunch of ImageViews that creates a tree image (E.g. ivStem, ivRoot... etc). These ImageViews are invisible at first, but after a few second, one will show up, another one again, and again... But the problem is I am having a hard time how to do it. I've tried the code below.
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
ImageView ivLogoText = (ImageView) findViewById(R.id.ivLogoText);
final double secondsDelayed = 5;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(SplashScreen.this, HomeLeftPanel.class));
//What should I put here?
if (secondsDelayed == 2000) {
ivLogoText.setVisibility(1);
} ...//more if statement here
finish();
}
}, (long) (secondsDelayed * 1000));
Overall, I have 5 seconds delayed. What I want to happen is set the visibility of this ImageViews according to that delay. How can I do this? I believed this is just an easy question but I've been searching for an hour and still can't find solutions. Thanks in advanced!
I just want to call a function after every 3secs on click of a button
What is going wrong here-
galleryBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final Handler handler = new Handler();
for(int i = 0;i<3;i++){
handler.postDelayed(new Runnable() {
#Override
public void run() {
// Do something after 5s = 5000ms
viewAnimator.showNext();
}
}, 3000);
}
}
});
You don't actually say what goes wrong but I'll take a wild guess that nothing happens (i.e. no animations) and the reason for that is probably that your Handler is being GC'd long before it gets to handle anything. Try keeping moving 'handlers' scope from local variable to class member.
(Also note that, even when it works, all 3 of your functions will run at more or less the same time. If you want them to run 3 seconds apart you should change the '3000' to 'i*3000'.)
Iam an newbie to android.I don't know whether this question may sound silly but i didn't find any solution.Please bare me. I had created an application which will first loads the app logo. I need to call another activity after this without using any click event.can anybody help me out wit this? and also i need to know in windows we can place panels over another panel. Can we do the same ting android? If yes how can i achieve that? I know that in a layout we have to place views but my questions is can we design view over another view so that i can hide and show views whenever needed?
Thanks in advance
Using Timers or Threads is a horrible way to do this, you are inviting memory leaks into your app. Use Android's Handler instead:
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
// create Intent for next activity and call startActivity with it
}
}, 2000);
If you have a reference to your content view, use contentView.getHandler() instead of creating a new one.
By the way, if this is for a personal project, consider NOT USING SPLASH SCREENS
You do not really provide enough information to give you a proper answer, but this will start a timer and when 5000 milliseconds has elapsed it will switch to another activity:
public class SplashActivity extends Activity {
private Timer t;
public void onCreate(Bundle b) {
super.onCreate(b);
t = new Timer();
t.schedule(new TimerTask() {
#Override
public void run() {
Intent i = new Intent(SplashActivity.this, NextActivity.class);
startActivity(i);
}
}, 5000);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
Thread timer = new Thread() {
public void run() {
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent intent = new Intent();
intent.setClass(WelcomePage.this, HomePage.class);
startActivity(intent);
}
}
};
timer.start();
}
that should do the trick my friend!!