media player sound stop() doesn't work - android

I have these two options in the menu of an activity
option one starts a music track and option two should stop it, but it isn't.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
MediaPlayer mpSoundTrack = MediaPlayer.create(this, R.raw.app_score);
switch (item.getItemId()) {
case R.id.icon: Toast.makeText(this, "Music On!", Toast.LENGTH_LONG).show();
mpSoundTrack.start();
break;
case R.id.icontext: Toast.makeText(this, "Music Off!", Toast.LENGTH_LONG).show();
mpSoundTrack.stop();
break;
}
return true;
}

Each time you create a new mediaPlayer, so you stop a new one, not the old one. You should keep a reference to it:
private MediaPlayer mpSoundTrack = null;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.icon:
Toast.makeText(this, "Music On!", Toast.LENGTH_LONG).show();
mpSoundTrack = MediaPlayer.create(this, R.raw.app_score);
mpSoundTrack.start();
break;
case R.id.icontext:
Toast.makeText(this, "Music Off!", Toast.LENGTH_LONG).show();
if(mpSoundTrack != null)
mpSoundTrack.stop();
break;
}
return true;
}

Related

How can i change background on item click in overflow?

So in my MainActivity, I can change the background of the layout by clicking on the overflow in my actionbar and selecting a background.
But how can I change the layout across all other activities?
This is the code i have now
private int selectedBackgroundId = R.id.defaultTheme;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.defaultTheme:
mainLayout.setBackgroundResource(R.drawable.defaultbackground);
if (selectedBackgroundId == R.id.defaultTheme){
Toast.makeText(getApplicationContext(), "Background already set", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Background set", Toast.LENGTH_SHORT).show();
}
selectedBackgroundId = R.id.defaultTheme;
return true;
case R.id.background1:
mainLayout.setBackgroundResource(R.drawable.redpinkgradientbackground);
if (selectedBackgroundId == R.id.background1){
Toast.makeText(getApplicationContext(), "Background already set", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Background set", Toast.LENGTH_SHORT).show();
}
selectedBackgroundId = R.id.background1;
return true;
Second question i have
How can I save the layout that has been set when app is destroyed?
So when I reopen the app, it's still the layout I clicked in the overflow menu.

Hiding activitiy / options menu on different fragments

The idea is to temporarily hide an option menu for a specific fragment. If the user is signed in, the system should hide the menu.
In all other fragments the menu should be implemented and enabled.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_main_home:
if (MainActivity.isSignedIn() == true){
(signedIn == false)
forwardToWelcomeFragment();
} else {
Toast.makeText(this, "You are not logged in.",
Toast.LENGTH_SHORT).show();
forwardToLoginFragment();
}
return true;
case R.id.menu_main_settings:
Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_main_info:
Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_main_signout:
Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
setSignedIn(false);
forwardToLoginFragment();
return true;
default:
return super.onOptionsItemSelected(item);
}

How to start different activity using switch case in the menu item?

public void onPopup(View view)
{
final PopupMenu menu=new PopupMenu(this,view);
menu.getMenuInflater().inflate(R.menu.menu1,menu.getMenu());
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem item)
{
Toast toast=Toast.makeText(MainActivity.this,
item.getTitle()+"Selected",Toast.LENGTH_SHORT);
//Intent intent2 = new Intent(MainActivity.this, YourSpotActivity.class);
//startActivity(intent2);
//startActivity(new Intent(MainActivity.this,YourSpotActivity.class));
toast.show();
return true;
}
});
menu.show();
}
When i click any one of the list item then it will start another activity.
How can i do that by modify an above code. explain me please.
I have use four car model in the menu. when i choose any one of that car then it will go to particular activity.
You need to use switch as below
switch (item.getItemId()) {
case R.id.menuitem1:
Toast.makeText(getApplicationContext(), "StartActiviy 1", Toast.LENGTH_SHORT).show();
// start activity 1
return true;
case R.id.menuitem2:
Toast.makeText(getApplicationContext(), "StartActiviy 2", Toast.LENGTH_SHORT).show();
// start activity 2
return true;
default:
//default intent
return true;
}
http://developer.android.com/reference/android/widget/PopupMenu.html
You can use switch statement as below inside onMenuItemClick:
switch (item.getItemId()) {
case R.id.menuitem1:
//calling intent ( activity1 )
case R.id.menuitem2:
//calling intent ( activity 2)
default:
//default intent
}

Get listview clicks from sidenavigation

I have implemented the Sliding Menu in my App. Source: https://github.com/johnkil/SideNavigation
It works like it should but when I click on any item in my menu, the click wont work some reason. I added onClick listener and all that.
Code snippet:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
sideNavigationView.toggleMenu();
Toast.makeText(getApplicationContext(),( R.string.title1),
Toast.LENGTH_LONG).show();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
#Override
public void onSideNavigationItemClick(int itemId) {
switch (itemId) {
case R.id.side_navigation_menu_item1:
Toast.makeText(getApplicationContext(),( R.string.title1),
Toast.LENGTH_LONG).show();
break;
case R.id.side_navigation_menu_item2:
Intent intent = new Intent(this, DiffAdapter.class);
this.startActivity(intent);
break;
case R.id.side_navigation_menu_item3:
invokeActivity(getString(R.string.title3), R.drawable.ic_action_storage);
break;
case R.id.side_navigation_menu_item4:
invokeActivity(getString(R.string.title4), R.drawable.ic_action_settings);
break;
case R.id.side_navigation_menu_item5:
invokeActivity(getString(R.string.title5), R.drawable.ic_launcher);
break;
default:
return;
}
finish();
}
Any help would be nice. It just wont do anything when I click on a item.
Thanks
I forgot to implement some strings, which caused bugs. Got this solved by adding:
icon = (ImageView) findViewById(android.R.id.icon);
sideNavigationView = (SideNavigationView) findViewById(R.id.side_navigation_view);
sideNavigationView.setMenuItems(R.menu.ribbon_menu);
sideNavigationView.setMenuClickCallback(this);
if (getIntent().hasExtra(EXTRA_TITLE)) {
String title = getIntent().getStringExtra(EXTRA_TITLE);
int resId = getIntent().getIntExtra(EXTRA_RESOURCE_ID, 0);
setTitle(title);
icon.setImageResource(resId);
}
to my code: onCreate ()

onOptionsItemSelected issue

i have created an options menu for my database class. Upon launching the options menu, I would like to the desired activity at the click of the specified button.
But the issue is that if i click on any option , i get directed to the MainMenu.class . Any ideas why this is happening ?
code:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
new MenuInflater(this).inflate(R.menu.optionmenu, menu);
return(super.onCreateOptionsMenu(menu));
}
public boolean onOptionsItemSelected ( MenuItem item){
switch (item.getItemId())
{
case R.id.item1:
{ Intent r=new Intent(Database.this,MainMenu.class);
startActivity(r);
}
case R.id.takesurvey:
{
Toast toast=Toast.makeText(this, "check", 2000);
toast.show();
Intent r1=new Intent(Database.this,SurveyActivity.class);
startActivity(r1);
}
case R.id.viewstats:
{ Intent r2=new Intent(Database.this,Stats.class);
startActivity(r2);
}
case R.id.changesort:
{ Intent r3=new Intent(Database.this,MainMenu.class);
startActivity(r3);
}
case R.id.menuexit:
{ Intent r4=new Intent(Database.this,MainMenu.class);
startActivity(r4);
}
}
return true;
}
It looks like you are missing a break statement in every case.
public boolean onOptionsItemSelected ( MenuItem item){
switch (item.getItemId())
{
case R.id.item1:
startActivity(new Intent(Database.this,MainMenu.class));
break;
case R.id.takesurvey:
Toast.makeText(this, "check", 2000).show();
startActivity(new Intent(Database.this,SurveyActivity.class));
break;
case R.id.viewstats:
startActivity(new Intent(Database.this,Stats.class));
break;
case R.id.changesort:
startActivity(new Intent(Database.this,MainMenu.class));
break;
case R.id.menuexit:
startActivity(new Intent(Database.this,MainMenu.class));
break;
return true;
}
For each of your conditions in the Switch statement in onOptionsItemSelected() you must return true. If you handle the case then you must return true, if you don't then you should call the super class implementation of it.
case R.id.item1:
{ Intent r=new Intent(Database.this,MainMenu.class);
startActivity(r);
return true;
}
Go through this for more details
http://developer.android.com/guide/topics/ui/menus.html#options-menu

Categories

Resources