Change action of a button according to text displayed - java

I would like to know how to change the action of a button according to what text is display in a TextView. I am using Android Studio.
I have tried to use an 'if statement' but that doesn't seem to work. So, I want a different sound to play according yo what text is displayed in the TextView.
package com.msp.exampleapplication;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class PrimaryClass extends AppCompatActivity {
TextView placeholder;
Button playsound_button;
MediaPlayer mySound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.primary_layout);
placeholder = (TextView) findViewById(R.id.placeholder);
playsound_button = (Button) findViewById(R.id.playsound_button);
placeholder.setText(getIntent().getStringExtra("message"));
}
public void playSound(View view) {
if (placeholder.equals("BMW M4")) {
mySound = MediaPlayer.create(this, R.raw.sound);
mySound.start();
}
else if (placeholder.equals("BMW M5")) {
mySound = MediaPlayer.create(this, R.raw.sound2);
mySound.start();
}
}
}
So, if the text of the TextView (placeholder) is "BMW M4", then when the button is clicked, it must play R.raw.sound. And if "BMW M5" is displayed in the TextView (placeholder), then R.id.sound2 must play.
But as I said, I've attempted to use the if statement and when I click the button, no sound plays at all.

Yes, as #Akshay Bhat 'AB' said, you must do this:
if (placeholder.getText().toString().equals("BMW M4")) {
mySound = MediaPlayer.create(this, R.raw.sound);
mySound.start();
}
else if (placeholder.getText().toString().equals("BMW M5")) {
mySound = MediaPlayer.create(this, R.raw.sound2);
mySound.start();
}
Refer this post: how to get text from textview
There's one error in the comment tho, its toString() and not toString

You need to check the text in the TextView and not the reference of the TextView itself.
placeholder.getText().toString().equals("BMW M4")

Related

stop playing sound when another sound is playing in android studio

hello I use MediaPlayer for play sound in my project in android studio
when I click in a button and play sound , and when I click in another button that button play another sound , I want the first sound or another sound be stoped and just the last button that I clicked be played
I try this code but it not worked
package azad.broooska.fartfuns;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MusicFartActivity extends AppCompatActivity implements View.OnClickListener {
private CardView cardView1, cardView2, cardView3, cardView4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music_fart);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
cardView1 = findViewById(R.id.card_music1);
cardView2 = findViewById(R.id.card_music2);
cardView3 = findViewById(R.id.card_music3);
cardView4 = findViewById(R.id.card_music4);
cardView1.setOnClickListener(this);
cardView2.setOnClickListener(this);
cardView3.setOnClickListener(this);
cardView4.setOnClickListener(this);
}
#Override
public void onClick(View view) {
int id = view.getId();
final MediaPlayer s1 = MediaPlayer.create(this, R.raw.bohemian_fartsody);
final MediaPlayer s2 = MediaPlayer.create(this, R.raw.fart_ballade);
final MediaPlayer s3 = MediaPlayer.create(this, R.raw.fart_uverture);
final MediaPlayer s4 = MediaPlayer.create(this, R.raw.farting_xmas);
if (id == R.id.card_music1) {
s1.start();
s2.stop();
s3.stop();
s4.stop();
} else if (id == R.id.card_music2) {
s2.start();
s1.stop();
s3.stop();
s4.stop();
} else if (id == R.id.card_music3) {
s3.start();
s2.stop();
s1.stop();
s4.stop();
} else {
Toast.makeText(this, "noThing", Toast.LENGTH_SHORT).show();
}
}
}
you are creating new instances of MediaPlayer each time something is clicked and assigning the variables to new objects; make s1,s2... class property;
MediaPlayer s1 ,s2,s3,s4;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music_fart);
//some code
s1 = MediaPlayer.create(this, R.raw.bohemian_fartsody);
//assign the rest;
}
public void onClick(View view) {
int id = view.getId();
if (id == R.id.card_music1) {
s1.start();
s2.stop();
s3.stop();
s4.stop();
}
//rest of the code
}
Well, its seems like you don`t want to play very long sounds because you have names like
"fart_ballade" and "fart_uverture" in your code.
If your sounds are not very long it is better to work with the SoundPool class.
The MediaPlayer class sometimes can cause problems if you switch fast between sounds.
So if your sounds are not that long, you can try the SoundPool class.
Here you can learn more about the SoundPool class:
How to use SoundPool
I won`t write any code because SoundPool needs more than 5-6 lines of code to work properly.

Java- hot to have just 1 button to toggle back and forth instead of two buttons?

I have 1 button for changing an image and a text.
I wanted to make that same button so that if I click AGAIN, it would change back to the original image and the text. However, 'TextView' and 'ImageView' in Java code would tell me, I have already defined. Therefore, I guess I can't re-define them within 1 button.
I ended up creating 2 buttons: 1 to change and 2nd one to return back. How can I just have one button to change and return images and text? HELP!
package com.example.android.cookies;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* Called when the cookie should be eaten.
*/
public void eatCookie(View view) {
// TODO: Find a reference to the ImageView in the layout. Change the image.
ImageView imageView = (ImageView)
findViewById(R.id.android_cookie_image_view);
imageView.setImageResource(R.drawable.after_cookie);
// TODO: Find a reference to the TextView in the layout. Change the text.
TextView textView = (TextView) findViewById(R.id.status_text_view);
textView.setText("Im so full");
}
public void returnCookie(View view) {
ImageView imageView = (ImageView)
findViewById(R.id.android_cookie_image_view);
imageView.setImageResource(R.drawable.before_cookie);
TextView textView = (TextView) findViewById(R.id.status_text_view);
textView.setText("I'm so hungry");
}
}
]2
I have written a well maintained code for you. You can save current state.
I don't recommend boolean. Because if you take int you can save more states in future, whereas in boolean you can save only two states- true or false.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
ImageView imageView;
TextView textView;
Button button;
final int STATE_HUNGRY = 1;
final int STATE_FULL = 2;
int currentState = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.android_cookie_image_view);
textView = (TextView) findViewById(R.id.status_text_view);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (currentState) {
case STATE_FULL:
returnCookie();
break;
case STATE_HUNGRY:
eatCookie();
break;
default: // used when there is no state available
eatCookie();
}
}
});
}
public void eatCookie() {
currentState = STATE_FULL;
imageView.setImageResource(R.drawable.after_cookie);
textView.setText("Im so full");
}
public void returnCookie() {
currentState = STATE_HUNGRY;
imageView.setImageResource(R.drawable.before_cookie);
textView.setText("I'm so hungry");
}
}
Have you tried using a static variable to keep track of the currently displayed image? Static means it will maintain its state between function calls. Then toggle it each time the function is called. The initial declaration will only be called once.
static Boolean eaten = false;

My app won't load when I bring in a MediaPlayer

I'm working on a simple number guessing game. At the moment I have it set that when you guess the correct number it takes you to WinActivity.java. All I want it to do on that screen is say "You won" and to play a little trumpet victory sound (and also display a button that'll bring them to the GameActivity if they want to play again).
The trouble is, when I try bringing in a MediaPlayer the app doesn't even start.
Here is my code for my WinActivity that contains the MediaPlayer.
package com.example.jeremy.numberguessinggame;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class WinActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_win);
MediaPlayer trumpetsound = MediaPlayer.create(this,R.raw.trumpet);
trumpetsound.start();
Button btnPlayAgain = (Button) findViewById(R.id.button2);
TextView youWon = (TextView) findViewById(R.id.textViewYouWon);
btnPlayAgain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent playAgainIntent = new Intent(WinActivity.this,GameActivity.class);
startActivity(playAgainIntent);
}
});
}
}
When I run the app the Messages Gradle Build window will pop up and says "error: cannot find symbol variable raw".
I added the raw folder to the res location, and was able to drag the sounds into the folder no problem.
I just don't see why I can't make this MediaPlayer work.
I would love some help. If you need more information I'll be glad to give it to you.

java android handler calling

I am new in the android programming. I see many ways to do the event handling, but when I try to do it by calling the handler class it give error on handling class name:
package com.example.test;
import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//attach an instance of HandleClick to the Button
findViewById(R.id.button1).setOnClickListener(new HandleClick());
}
private class HandleClick implements OnClickListener{
public void onClick(View arg0) {
Button btn = (Button)arg0; //cast view to a button
// get a reference to the TextView
TextView tv = (TextView) findViewById(R.id.textview1);
// update the TextView text
tv.setText("You pressed " + btn.getText());
}
}
}
"HandleClick" error come on this it say class should be abstract type?
I do not understand why it is giving this error can any one help me?
That's the wrong OnClickListener class. You have
import android.content.DialogInterface.OnClickListener;
You need:
import android.view.View.OnClickListener;
For future reference, the error you get is "The type must implement the inherited abstract method...". This is because you need to implement the DialogInterface's onClick, which should have led you to notice that it was the wrong import (since you have onClick(View))
you imported the wrong OnClickListener, you should import the one from android.view.View
Make it simple & use this,
b1 = (Button) findViewById(R.id.button1);
TextView tv = (TextView) findViewById(R.id.textview1);
b1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast msg = Toast.makeText(getBaseContext(),
"You have clicked Button 1", Toast.LENGTH_LONG);
msg.show();
tv.setText("You pressed " + btn.getText());
}
});

Android button slow action

I have a button that leads to another page. And whenever I click it I get this info code in logcat :
12-07 16:09:45.073: I/ActivityManager(273): Displayed com.example.prva/.button: +1s764ms
Seconds and ms vary of course each time between 1-3 seconds. The problem is that I noticed that it takes a while for that button to open that page. It has some kind of pause or whatever and this is the only relevant thing I have found in the logcat that could be connected to it. How can I fix this, why is this button acting "slow"?
This is where the button code is :
package com.example.prva;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Meni_Splash extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnv = (Button) findViewById(R.id.buttonv);
btnv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Meni_Splash.this, button.class));
}
});
}
}
And this is the class that opens :
package com.example.prva;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
public class button extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.button);
//Button click sound
final MediaPlayer MPRadio1 = MediaPlayer.create(this, R.raw.radio1);
final MediaPlayer MPRadio2 = MediaPlayer.create(this, R.raw.radio2);
final MediaPlayer MPRadio3 = MediaPlayer.create(this, R.raw.radio3);
final RadioButton rb1, rb2, rb3;
rb1 = (RadioButton) findViewById(R.id.radio1);
rb2 = (RadioButton) findViewById(R.id.radio2);
rb3 = (RadioButton) findViewById(R.id.radio3);
Button btn = (Button) findViewById(R.id.buttonplay);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(rb1.isChecked())
{
MPRadio1.start();
}
else
{
if(rb2.isChecked())
{
MPRadio2.start();
}
else
{
if(rb3.isChecked())
{
MPRadio3.start();
}
}
}
}
}
);}}
I don't know what thing could make it so slow from these activities?
Your code looks pretty decent to be honest. Not sure entirely what could be causing it to intialise slowly.
But there are two areas to look at.
The first, most likely, is your layout loading:
setContentView(R.layout.button);
I dont imagine your layout to be complex though. But if it is, aka, lots of nested views (linear layouts within other linear layouts), or lots of views (textviews etc) in general on the page, then it could be taking a while to "inflate" the Layout.
Alternatively and less likely, is that MediaPlayer.create takes a fair while to load. The reason I suggest this, is I have no idea how it works, as I've not used it before.
//Button click sound
final MediaPlayer MPRadio1 = MediaPlayer.create(this, R.raw.radio1);
final MediaPlayer MPRadio2 = MediaPlayer.create(this, R.raw.radio2);
final MediaPlayer MPRadio3 = MediaPlayer.create(this, R.raw.radio3);
The best thing to do, would be to profile it with the DDMS profiler. Or put a timer around it, and print the results to logcat.
Also, on a quick note, is it just 2-3 seconds loading? And is it really that bad for what its trying to do?

Categories