android


Android studio 1 button 2 event


I have an ImageView as a button. I want it to if I click it, play a sound and switch to an another activity. In alone it works perfectly, but I cant mix them, it can only play sound, or switch activity.
Activity switch code:
public void imagebutton (View v) {
Intent startNewActivity = new Intent(this, NewActivity.class);
startActivity(startNewActivity);
}
And the sound code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer sound = MediaPlayer.create(this, R.raw.clicksound);
ImageView click = (ImageView) findViewById(R.id.imagebutton);
click.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick (View v){
sound.start();
}
}
);
Thanks.
Try like this,
click.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick (View v){
sound.start();
Intent startNewActivity = new Intent(MainActivity.this, NewActivity.class);
startActivity(startNewActivity);
}
}
);
also check .NewActivity in your manifest.xml
Put your code to launch the activity under sound.start();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer sound = MediaPlayer.create(this, R.raw.clicksound);
ImageView click = (ImageView) findViewById(R.id.imagebutton);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v){
sound.start();
Intent startNewActivity = new Intent(this, NewActivity.class);
startActivity(startNewActivity);
}
}
);
You can implement a AsyncTask to reproduce your sound before start your new Activity.
public class BackgroundSound extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
MediaPlayer player = MediaPlayer.create(this, R.raw.clicksound);
player.setLooping(true); // Set looping if you want
player.setVolume(100,100); // Set Volume if you want
player.start();
return null;
}
}
And use like this: BackgroundSound.execute(); before start new activity.
you can use setOnCompletionListener to make sure sound is over and then start the new activity:
final MediaPlayer sound = MediaPlayer.create(this, R.raw.clicksound);
sound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
Intent startNewActivity = new Intent(this, NewActivity.class);
startActivity(startNewActivity);
}
});
ImageView click = (ImageView) findViewById(R.id.imagebutton);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v){
sound.start();
}
}

Related Links

push notification with cordova and firebase Cloud messaging:registrationID keep changing
Cordova Android app deploying to Android N device
Android app links clear openning
Real World Use case of RxJava Subject
Communication between multipe fragments
Android - Implementing search filter to a RecyclerView
Cocos2d-x jni How do I connect a Bluetooth keyboard
Stream audio (only) from ffmpeg to Android application
How to always show title in BottomNavigationView [duplicate]
Unable to save image from the directory using FileProvider
Sqlite 3.8 with pre-Lollipop APIs
IconTabBar loses content only on device - cordova android
Get PDF stream from (Xamarin)Android.Print.PrintDocumentAdapter
how to compare my contact with the contacts uploaded in firebase database
How to change independently the distance between ActionBar icons in Android?
MPAndroidChart x-axis date/time label formatting

Categories

HOME
webdriver
twig
swt
rendering
yandex
vimdiff
datastage
favicon
mongodb-csharp
symbol
gruntfile
google-content-api
pyramid
digital-signature
windows-xp
postscript
beta
syntax-highlighting
wcag
spotipy
rpgle
dst
kodi
modalviewcontroller
sql-delete
python-pptx
cd
modernizr
connection-pool
urlsession
apache-stanbol
azure-arm
doc
postgresql-8.4
movilizer
plr
outputcache
opencmis
spring-integration-sftp
trojan
magic-draw
semantic-logging
java-websocket
objectscript
bootstrap-tags-input
donations
ziparchive
windows-azure-queues
expectations
clipboard.js
shopping
http-status-code-401
qiime
cda
yajsw
halcon
pypiserver
dock
sha512
skspritenode
nutiteq
snackbar
kubuntu
osc
gamekit
mruby
viewer
crowd
psr-4
named-parameters
verold
live555
jjaql
ios8-extension
terminate
file-not-found
dsym
scringo
facebook-timeline
jquery-1.9
pagedown
insert-id
uimanageddocument
xcode3.2
listitem
database-create
screen-size
mtu
delimited-text
feasibility
visualj#
wordprocessingml
wysiwym
soappy

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App