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

Cordova 1.7.0 + childbrowser (Android)
MapView not being removed from ViewPager?
Android ScrollView detect end of/finished scroll animation
generate random locations nearby my location
When should I use databases?
Not able disable Home button on specific android devices
view selected items from listview to next tab android
Android alphabets section Listview like contacts app listview
how to get one class transparent variable value into another class in android
Android onResume Show Previous state of Activity
Android: Display custom dialog in center of the container
Why applyDimension method returns wrong values?
Killing an *unresponsive* thread in Android/Java
Android App to be a default application for completion of sending an outgoing SMS
Call Phone Numbers with Commas/Pauses on Samsung Android Phones
Android - NDK/ARM: float parameters size

Categories

HOME
android
algorithm
gridview
localization
cheerio
developer-tools
wakanda
rendering
carousel
dojo
iis-6
special-characters
tput
mvc-mini-profiler
firefox-addon
dimensional-modeling
dreamweaver
red5
jersey-1.0
xmonad
soci
mapstruct
production
connector
sencha-touch
postscript
google-api-client
nstimer
genexus-sd
winexe
quantlib
sql-server-2000
fstream
sharding
searchkit
hivemq
sim-toolkit
google-knowledge-graph
sessionstorage
connection-pool
password-protection
recurrent-neural-network
demo
cin
jvmti
drupal-theming
reportservice2010
bluez
headless
cup
trigger.io
mathcad
vibrate
smartfoxserver
rocks
autosar
webjars
file-uri
resourcemanager
android-dateutils
jclouds
swizzling
ikiwiki
yajsw
flock
opserver
binomial-theorem
dock
branch-and-bound
teamcity-9.1
bufferedinputstream
j-security-check
phpldapadmin
at-job
deform
formview
rcaller
high-resolution
ora-06550
gd-graph
windows-phone-8-sdk
hidapi
android-icons
traceability
insert-id
mysql-error-2013
swfobject
android-loadermanager
patricia-trie
expression-blend-4
u2netdk
listactivity
uipagecontrol
web-garden
tabpanel
visualj#
cldc
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