How can I move the Splash screen with touch - java

My splash screen diseapear after 2 seconds but I want it to diseapear also after a screen touch
Like if I press screen I moved next page without waiting until the end of the timer.
Is it possible??
public class SplashActivity extends Activity {
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashfile);
handler=new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
}
},2000);
}
}

Tes it is possible, you can add a touch listener to your layout that do the same as what will happen after two seconds , some thing like that
findViewById(R.id.myLayout).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
return true;
}
});
EDIT :
or you can instead add the next code outside onCeate method if the layout don't receive the touch event:
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
return super.dispatchTouchEvent(ev);
}

Related

Hold the button for 2 seconds and then show an AlertDialog

I have a button and when I hold the button for 2 seconds, I want to show an AlertDialog.
This is what I have tried:
btn.setOnTouchListener(new OnSwipeTouchListener(getContext()) {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Helper.setTimeout(() -> {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Message");
builder.setMessage(text);
builder.show();
System.out.println("I am text");
}
}, 2000);
return super.onTouch(view, motionEvent);
}
}
My method setTimeout():
public static void setTimeout(Runnable runnable, int delay){
new Handler(Looper.getMainLooper()).postDelayed(runnable, delay);
}
My problem is that sometimes the AlertDialog shows up multiple times and I always get this warning:
W/InputEventReceiver: Attempted to finish an input event but the input
event receiver has already been disposed.
What am I doing wrong?
I don't know if there is a better solution. I have also tried It with
btn.setOnLongClickListener(v -> {
System.out.println("hold");
return true;
});
but that doesn't output anything.
You can try this
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Message");
builder.setMessage("hello");
builder.show();
}
}, 2000);
return false;
}
});
}
}

android button animation. How to get animation first, on click before activity

I made a button for Android that rotates on click, but when I set a button and new activity, when I click it's just set me to new activity.
I need just this: when I click on that button, first to do animation e.g. rotate, then to execute a new activity. Here is my code:
ImageButton pandaButton2 = (ImageButton) findViewById(R.id.pandaButton2);
pandaButton2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
v.startAnimation(pandarotate);
startActivity(new Intent("com.example.threepandas.MENU"));
}
});
You can set animation listener, when finish animation start your activity.
Please refer this link
Example code (must update based on your purpose):
ImageButton pandaButton2 = (ImageButton) findViewById(R.id.pandaButton2);
pandaButton2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
v.startAnimation(pandarotate);
}
});
pandarotate.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
startActivity(new Intent("com.example.threepandas.MENU"));
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
insert a delay for the length of the animation after the animation starts and before the activity starts
try {
Thread.sleep(1000); //1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
Here is the solution I use for this problem (got it from the Google I/O App Source Code on Github)
private static final int DELAY = 250;
private Handler mHandler;
#Override
public void onClick(final View view) {
switch (view.getId()) {
case R.id.button:
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
}
}, DELAY);
break;
}
}

delay between menu and game activity

I am making my own game for practising.
Once my game became bigger and bigger i noticed some delay once the main activity was about load.
So after reading, some threads i come up with another activity ( main ) showing a splash screen until the real main (menu) activity is loaded.
That worked pretty well, but since my game is on another activity once i change activity to play the game and then press the back button, or even when i am calling again the real main (menu) activity i face again this shitty lag/delay.
So to summurize, i have 3 activities so far.
1) The main activity which loads the splash screen and then calls the MenuActivity
2) The MenuActivity which basicly provides settings and buttons to start
3) The GameActivity which is the game
I could easily remove the MenuActivity and mix up the game with the menu activity handling the menu with the visibility of the images/buttons but i am wondering why is this happening for future knowleque also.
Here's the MenuActivity that makes the delay
public class MainActivity extends AppCompatActivity {
RelativeLayout _layer = null;
SharedPreferences _res;
Boolean _onFirstLoad=true,_bgMusic,_doubleBackToExitPressedOnce = false;
ImageView _musicBtnStop, _musicBtnStart, _about, _infoBtn;
SharedPreferences.Editor editor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(_onFirstLoad)
{
loadAllContent();
_onFirstLoad = false;
}
if (_bgMusic) {
_musicBtnStart.setVisibility(View.INVISIBLE);
BackGroundMusic.playMusic(); //starting
} else {
_musicBtnStart.setVisibility(View.VISIBLE);
_musicBtnStop.setVisibility(View.INVISIBLE);
}
//Start Game Button
_layer.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
callGameStart();
return false;
}
return false;
}
});
_musicBtnStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
callMusicStop();
}
});
_musicBtnStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
callMusicStart();
}
});
_about.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
callGameStart();
}
});
}
#Override
public void onBackPressed() {
if (_doubleBackToExitPressedOnce) {
super.onBackPressed();
BackGroundMusic.quit();
return;
}
_doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
_doubleBackToExitPressedOnce=false;
}
}, 2000);
}
private void callGameStart() {
Intent game = new Intent(this, GameInterface.class);
startActivity(game);
}
private void callMusicStart() {
BackGroundMusic.setMuted(false);
_musicBtnStop.setVisibility(View.VISIBLE);
_musicBtnStart.setVisibility(View.INVISIBLE);
editor = _res.edit();
editor.putBoolean("bgMusic", true);
editor.apply();
}
private void callMusicStop() {
BackGroundMusic.setMuted(true);
_musicBtnStop.setVisibility(View.INVISIBLE);
_musicBtnStart.setVisibility(View.VISIBLE);
editor = _res.edit();
editor.putBoolean("bgMusic", false);
editor.apply();
}
private void loadAllContent() {
BackGroundMusic.setParams(MainActivity.this, R.raw.background_sound); //setting the music file
_layer = (RelativeLayout) findViewById((R.id.main));
_res = getApplicationContext().getSharedPreferences("com.nvm.tapper.prefs.xml", 0); // load our xml database
_bgMusic = _res.getBoolean("bgMusic", true);
_musicBtnStop = (ImageView) findViewById(R.id.stopMusic);
_musicBtnStart = (ImageView) findViewById(R.id.startMusic);
_about = (ImageView) findViewById(R.id.about);
}

android back buttons, only one piece of code

I want to write some code that performs the action to go back to the previous activity. Of course I can write a code for each back button that I have, but I would prefer to have only a piece of code working for all the back buttons (and then, for example, to assign the function to the "onclick" event). How can I manage that?
Please use onclick method and that method redirect to previous activity.
#Override
public void onBackPressed()
{
// your code.
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction()==KeyEvent.ACTION_DOWN)
{
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}
Use inheritance
YourActivity.java
public class YouActivity extends BaseActivity {
Button back_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
back_button = (Button) findViewById(R.id.back_button);
back_button.setOnClickListener(base_listener);
}
}
BaseActivity.java
public class BaseActivity extends Activity {
View.OnClickListener base_listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
//you back action here;
}
};
#Override
public void onBackPressed() {
super.onBackPressed();
}
}

how to resume my audio in Android?

I have to implemented application for creating audio with the status of pause and resume and when my app as an when start the audio is start and when I press the back button on the emulator the audio music is on pause state but When my activity comes back to the foreground from the stopped state my audio music is not resumed. Here is my code.
public class Audio_Activity extends Activity
{
private MediaPlayer mp;
Button btnStartStop ;
Button btnChapter ;
Button btnOne;
Button btnTwo;
Button btnThree;
Button btnFour;
Button btnFive;
int length;
ImageView imgVw;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.audio);
init();
mp=MediaPlayer.create(Audio_Activity.this,R.raw.ennamo_yadho);
Log.e("Song is playing","in Mediya Player ");
if(mp!=null)
{
mp.setLooping(false);
mp.start();
btnChapter.setEnabled(false);
System.out.println("B4 button Click!!!!");
}
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
mp.stop();
mp.release();
btnChapter.setEnabled(true);
System.out.println("Music is over and Button is enable !!!!!!");
}
});
btnStartStop.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
if(mp.isPlaying())
{
if(mp!=null)
{
mp.pause();
}
}
else
{
// Resume song
if(mp!=null)
{
mp.start();
}
}
}
});
btnOne.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
imgVw.setImageResource(R.raw.chocklate);
}
}
);
btnTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
imgVw.setImageResource(R.raw.creame);
}
});
btnThree.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
imgVw.setImageResource(R.raw.schocklate);
}
});
btnFour.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
imgVw.setImageResource(R.raw.pinapple);
}
});
btnFive.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
imgVw.setImageResource(R.raw.strobery);
}
});
}
#Override
protected void onResume()
{
super.onResume();
System.out.println("Activity is Resume !!!");
}
#Override
protected void onStart()
{
super.onStart();
System.out.println("Activity is Started !!!");
}
#Override
protected void onRestart() {
super.onRestart();
System.out.println("Activity is Re-Started !!!");
if(mp.isPlaying())
{
if(mp!=null)
{
length=mp.getCurrentPosition();
mp.seekTo(length);
mp.start();
}
}
}
#Override
protected void onPause() {
super.onPause();
System.out.println("Activity is Pause!!!");
}
#Override
protected void onStop() {
super.onStop();
System.out.println("Activity is Stop !!!");
}
#Override
protected void onDestroy() {
super.onDestroy();
System.out.println("Activity is Destroyed !!!");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{ //Back key pressed
//Things to Do
if(mp!= null)
{
if(mp.isPlaying())
{
mp.pause();
//mp=null;
}
}
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
You should use android MediaPlaybackService for background play,Pause, stop or just open the activity by clicking the Notification. when click on back button it will bind a PendingIntent to with the button click event or buttons on the notification bar for controlling audio playbacks.
Use this Gist Code for AudioPlayer, try o use MediaPlaybackService class or try to reverse engineer this owncloud code.
Your code is not correct. You should not bother backbutton etc, but activity life cycle. You should pause your audio in onPause() and resume in onResume(). So move code to onPause() and get rid of onKeyDown() and move code from your onRestart() to onResume(). And remove all methods you do not need, line onDestroy(), onStart() etc

Categories

Resources