Android OnClick play sound and change button image. - java

I want to create a simple application that has a button with an image (ben1), and when you click the button it plays an audio clip (audiofile) and the image changes to a second image (ben2).
Once the button is no longer clicked the image changes back to the original.
I currently have the below code which plays an audio file when the button is clicked
And below that is code I had from an other app that change the button image when it was clicked.
Can someone assist me with exactly how I would merge theses.
Play audio File
public class MainActivity extends Activity{
private SoundPool soundPool;
private int soundID;
boolean loaded = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer benSoundMP = MediaPlayer.create(this,R.raw.audiofile);
Button playbenSound = (Button) this.findViewById(R.id.button1);
playbenSound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
benSoundMP.start();
}
});
}
}
Change Button Image
public class MainActivity extends Activity {
ImageButton button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (ImageButton)findViewById(R.id.button);
button.setOnClickListener(imgButtonHandler);
}
View.OnClickListener imgButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
button.setBackgroundResource(R.drawable.icon_red);
}
};
}

The right way to do this, is to set as resource of the button a selector xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="#drawable/button_standby"/>
<item android:state_pressed="true" android:drawable="#drawable/button_pressed"/>
</selector>
And you don't need to change or add anything to the button listener, on your button declaration on layout, you add this file as the background.

Remove the imgButtonHandler and add this:
button.setOnClickListener(new OnclickListener(){
public void onClick(View v) {
button.setBackgroundResource(R.drawable.icon_red);
}
});

Related

Button animation in Android studio

image
How to get this kind of animation in Android studio.
I am creating a quiz app. I want user to click a button and let the code check for answer and then change button animation as in the video.
Create a file in res folder with name alpha_animation
<?xml version="1.0" encoding="utf-8"?>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromAlpha="1"
android:toAlpha="0.05"
android:repeatCount="5"
android:repeatMode="reverse"
/>
now write this in you activity
private Button mButton;
private Animation mAlphaAnimation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the application context
mContext = getApplicationContext();
mActivity = MainActivity.this;
mButton = (Button) findViewById(R.id.btn);
mAlphaAnimation = AnimationUtils.loadAnimation(mContext,R.anim.alpha_animation);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Start the blink animation (fade in and fade out animation)
mButton.startAnimation(mAlphaAnimation);
}
});
}

How to add key click event in on view?

How can I do key event. I do it, but on button event. I have to change it to key.
I have my keyboard from How can you make a custom keyboard in Android?
My buttons events:
public class MainActivity extends AppCompatActivity {
private static final int pic_id = 123;
EditText editText;
Button button1;
Button button2;
Button button3;
Button button5;
Button button6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
button1 = (Button) findViewById(R.id.n1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText("I am custom Keyboard!");
}
});
final MediaPlayer sawSound = MediaPlayer.create(this, R.raw.playagame);
button2 = (Button) this.findViewById(R.id.playMusic);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sawSound.start();
}
});
}
}
I don't know whether I got you right: do you mean whether reacting to key events of a physical keyboard? If so, you should be able to overwrite dispatchKeyEvent of your keyboard activity.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_2:
case KeyEvent.KEYCODE_NUMPAD_2:
handleInput("2");
return true;
}
}
return super.dispatchKeyEvent(event);
}
Your code seems to be working fine.....it plays the music
check your xml file and cross-check if you are reffering the correct id to each button
and check your device volume sound incase its muted
and also post your aactivity.xml file if still didnt fix

How to combine two onClick for change text of Button and play/stop sound

How to combine two functions in one button?
How to make when button clicked - the button text change and sound will play
Now I can only play the sound or just change the button text
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button one;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button one = (Button) this.findViewById(R.id.button);
one.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
String ButtonText=one.getText().toString();
if(ButtonText.equals("Stop")){
one.setText("Play");
}else {
one.setText("Stop");
}
}
});
It seems that you're already changing the button text in your onClick event handler. If you're using some method call to play the desired sound, you can just put that in your onClick handler method as well. Eg:
public void onClick(View v) {
String ButtonText=one.getText().toString();
// method call that plays the sound
if(ButtonText.equals("Stop")){
one.setText("Play");
} else {
one.setText("Stop");
}
}

"setOnClicklistener" not working in Android Studio 3.5

I am new to Android Programming and I am making a simple browser in which I want to open my web activity by clicking the button but I am amazed to see that setOnClicklistener is not available in Android Studio 3.5 as I have just updated
You need to put the setOnClickListener in one of the activity callbacks. In your onCreate() method, move the button there and then setOnClickListener().
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filters);
Button button = findViewById(R.id.google);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TODO()
}
}
}
Access/init Your button inside onCreate() method.
private Button btn;
#Override
public void onCreate(#Nullable Bundle savedInstanceState, #Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.home_search_layout);
btn = findViewById(R.id.someId);
setClickListener();
}
private void setClickListener() {
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
All the functional elements' code is to be initialized in the activity's onCreate() method. Since you want to make a button clickable, you need to add the setOnClickListener() in the onCreate() method, like so:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selectionactivity);
Button button = findViewById(R.id.google); //this id should be of a button not a view
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TODO do something
}
}
}
I highlighted some text because from the image, you can see that the 9th that you get from the code completion menu is the setOnClickListener but the parent is from the group android.view.View but not from android.widget.Button. Make sure, that R.id.google is a Button and also put the code inside onCreate()

onClick not working in android studio

I'm trying to learn android development with Android Studio but I can't seem to figure out why my clicks are not registering. Is there something I'm missing here?
I don't like the way Stack Overflow FORCES me to format things they way THEY want. Here is pastebin! :_
http://pastebin.com/hdzZpsud
call this changeText() method inside onCreate() method of your activity
your activity should look like this:
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
changeText();
}
public void changeText(){
final Button button = (Button) findViewById(R.id.button1);
final TextView text = (TextView)findViewById(R.id.largetext);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
button.setText("Button has been pressed");
text.setText("The large text has been changed");
}
});
}
}
In fact, you didn't set the listener. So you should add changeText() after setContentView(R.layout.activity_my);.

Categories