I have an onClick method set in the xml layout file that triggers the vibration of the phone for 100ms at this point I have the ImageView Visibility set to visible so it can be seen. I want the ImageView to be set back to gone again when the vibration has stopped. How do I go about this?
You can start this method at the same time:
public void timerDelayRemoveView(float time, final ImageView v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
v.setVisibility(View.GONE);
}
}, time);
}
Related
I am creating like a Whack a Molee type of game, and I am creating imageviews at random positions on the screen.
The thing is, I am adding a postdelayed to a var I am creating every two seconds ( the var is the imageview) but it only acts to the last imageview created. It happens because every two seconds that var is replaced with a new instance of imageview.
How can I add an independent postdelayed or something that detects when 2 seconds has passed and removing that specific imageview.
Thanks!
mv = new ImageView(this);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
mv.setVisibility(View.GONE);
}
}, 2000);
I tried this, but only removes the very last imageview.
public void createImage() {
final ImageView mv = new ImageView(this);
// Do all your positioning and sizing (layout params) setting stuff here
new Handler(getMainLooper()).postDelayed(new Runnable() {
#Override
public void run() {
mv.setVisibility(View.GONE);
}
}, 2000);
}
Simply don't override the value every time, by using a local varaible inside a method.
This way the mv instance will be the same in the runnable which is executed by the handler.
(And also don't create a handler every time)
(You could also draw the entire thing on a canvas)
I'm trying to have two animations occur in one activity with the first happening first and only once it's done, does the second start. The ObjectAnimator moves the imageView up the screen using the .ofFloat() method and then I want a loading animation which loops through several image files to create a loading-like effect.
This is my code:
//Animations
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
//Change cloud from sleeping to awake/happy
g2ndSleepingClouds.setBackgroundResource(R.drawable.clouds_happy_formatted);
//Move image view 250 pixels up the screen vertically
ObjectAnimator animation1 = ObjectAnimator.ofFloat(g2ndSleepingClouds, "translationY", -250f);
animation1.setDuration(1000);
animation1.start();
//Loading animation
loadingImageView.setBackgroundResource(R.drawable.loading_1);
loadingAnimation = (AnimationDrawable) loadingImageView.getBackground();
loadingAnimation.stop();
loadingAnimation.start();
}
}, 1000);
The error I get in Logcat is:
"android.graphics.drawable.BitmapDrawable cannot be cast to android.graphics.drawable.AnimationDrawable"
Any help?
This is my first question so sorry if I don't provide all the needed info. Just comment and I'll add it.
Onto the problem itself. I'm trying to make a splash screeen of sorts which consists of a short video. I want the app to start the following activity after the video ends, but after I start the app it just starts the second activity right away (Menu.class in the code). Once I'm in the Menu activity I can go back but all i see is a black VideoView. Also, the VideoView worked before I added the Handler. Not sure if it even could have been the problem (new to android studio) but doesn't work even after removing #Override (probably a stupidity, right?). Or can it be caused by the Menu.java activity? (It's just the standard pre-generated preset, I didn't make any changes to it)
public class splash_screen extends AppCompatActivity {
static long duration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_splash_screen);
VideoView videoView = this.findViewById(R.id.videoView);
String path = "android.resource://" + getPackageName() + "/" + R.raw.splash;
videoView.setVideoURI(Uri.parse(path));
videoView.start();
duration = videoView.getDuration();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(splash_screen.this, Menu.class));
}
}, duration);
}
}
Thanks for any and every advice, and like I said let me know if you need more info on the problem.
VideoView need some time to warm up, so here:
videoView.start();
duration = videoView.getDuration();
duration could be 0, that is why handler fires immediately. To switch to the second activity you should remove handler part and use:
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
startActivity(new Intent(splash_screen.this, Menu.class));
finish();//close activity
}
});
You Can Also Use Thread.sleep(millisecond); If you are adding animation to your splash screen
The Full C O D E:
Thread t=new Thread(){
#Override
public void run() {
try{
sleep(3000);
}
catch (InterruptedException e){
}
finally {
startActivity(i);
}
}
};
t.start();
It Gives a delay of 3 seconds,you can do animation part within it
I have an app with a title screen. When the app first starts, I have an onCreate method that contains the following code:
setContentView(R.layout.title_screen);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
setContentView(R.layout.main_screen);
}
}, 2000);
When I run my app and press the back button while on the main_screen layout, it closes the app (as it should). However, when I reopen the app, it displays the title_screen layout for two seconds again even though the app is already running. How can I prevent this?
This will prevent the delay appearing again when resumed:
private static boolean flag = false;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(!flag){
setContentView(R.layout.title_screen);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
setContentView(R.layout.main_screen);
}
}, 2000);
flag = true;
} else {
setContentView(R.layout.main_screen);
}
}
Btw, if your app was on background and it is calling onCreate again while being resumed, it means that it is killed by the OS. Therefore it is normal to have the initial delay again.
What I would do is to implement two different activities first one showing title_screen and the second which is started after 2s should show your main screen.
After looking at your code I can see that you ALWAYS start with title_screen then after 2s, you change to main_screen. Therefore, when you press back, that means you finish your activity. When you re-open your app, onCreated is called again, and it run every line of code as the previous opening.Of course, there's no difference in 2 times you open your app. To overcome it, I recommend to use SharedPreference to store the flag to check main_screen or title_screen.
I need to perform an action after onClick method of OnClickListener has run.
Here is my code for onClickListener:
View.OnClickListener imgButton0Handler0 = new View.OnClickListener() {
int identifier=0;
public void onClick(View v) {
//check if tile is found and return if it is
if(isFound[identifier]==true) return;
//set tile as open
checkField[identifier]=1;
//set background on predetermined
button0.setBackgroundResource(tiles[identifier]);
}
};
After this has run, and the background is set I would like to call a method checker(int identifier) which will check for other open tiles and change backgrounds accordingly.
This method needs to be run separately because the background is only displayed after onClick finishes, and I need predetermined background shown for a short time before checker method changes it to something else.
How can I accomplish this?
Have You Tried Post Delayed see this,
View.OnClickListener imgButton0Handler0 = new View.OnClickListener() {
int identifier=0;
public void onClick(View v) {
//check if tile is found and return if it is
if(isFound[identifier]==true) return;
//set tile as open
checkField[identifier]=1;
//set background on predetermined
button0.setBackgroundResource(tiles[identifier]);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
checker(identifier) // your method call
}
}, 3000); // 3 second
}
};