Button that Plays Sound in Android Studio not Playing - java

I am trying to make a simple button that plays a sound, but I am getting an error on (this, R.raw.shotgun). I have the raw folder and the sound file. I think the problem is with the this but I don't know why. Thanks.
public class MainActivity extends AppCompatActivity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(this, R.raw.shotgun);
mp.start();
}
});
}
}

The first parameter of MediaPlayer.create is a Context. The usage of MediaPlayer.create(this, R.raw.shotgun) works when you are working in your Activitys scope, because Activity extends Context, therefore this in this case ends up being a Context as well.
However, you're working within View.OnClickListener scope, and this assumes this class' value, and not Context as required. To fix that, just set the Context variable to this while still in your Activity scope.
public class MainActivity extends AppCompatActivity {
private Button button;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(context, R.raw.shotgun);
mp.start();
}
});

Related

Java Android: the button does not work after returning to the first layout

when I press two buttons the picture changes but when I move with the 3 button to the 2nd layout and I go back to the first picture it doesn't change anymore
public class MainActivity extends AppCompatActivity {
private Button button1;
private Button button2;
private Button changeLayout;
private Button exit;
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
imageView = findViewById(R.id.imageView);
changeLayout = findViewById(R.id.changeLayoutBt);
exit = findViewById(R.id.exitBtn);
}
public void changeImage(View view) {
switch (view.getId()){
case R.id.button1:
imageView.setImageResource(R.drawable.orange);
break;
case R.id.button2:
imageView.setImageResource(R.drawable.java);
break;
}
}
public void changeLayout(View view) {
setContentView(R.layout.second_layout);
}
public void exit(View view) {
setContentView(R.layout.activity_main);
}
}
`
In order to create new layout got to File>New>Activity>Empty Activity
Change your changeLayout code to this one
public void changeLayout() {
startActivity(new Intent(MainActivity.this, Main2Activity.class);
}
Main2Activity.class is your newly created java class. It would be Main2Activity.java or what ever you named it.
Inside your onCreate() method. Just below
changeLayout = findViewById(R.id.changeLayoutBt);
Paste this code
changeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
changeLayout();
}
});

Android Studio / How to apply SharedPreferenced to my code

I'm doing Quiz in Android Studio. The game is divided into two categories. After passing the first category, the second button (opening the second category) changes the status from setEnable "false" to "true". How to use the SharedPreferenced method in my code that the changes related to the second button (.setEnable) will be saved after closing the application.
Last level of first category
public class win extends AppCompatActivity implements View.OnClickListener{
Button win1;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_winflagi);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
win1 = (Button) findViewById(R.id.winflagi);
win1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v==win1)
{
Intent myIntent = new Intent(win.this, Activity2.class);
myIntent.putExtra("isEnabled", "enabled");
startActivity(myIntent);
}
}
}
Class containing a buttons for two categories ...
button3 opened the first category
entrycity opens the second category
public class Activity2 extends AppCompatActivity implements View.OnClickListener{
Button button3;
Button entrycity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_2);
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(this);
entrycity = (Button) findViewById(R.id.entrycity);
entrycity.setOnClickListener(this);
}
#Override
public void onClick(final View v) {
final MediaPlayer mp = MediaPlayer.create(this, R.raw.menu);
if (v == button3) {
startActivity(new Intent(Activity2.this, flagi1.class));
Bungee.zoom(this);
mp.start();
}
if (v== entrycity){
Intent intent=getIntent();
String isEnabled = intent.getStringExtra("isEnabled");
if(isEnabled==null||isEnabled.equals("disabled")){
entrycity.setEnabled(false);
}
else{
entrycity.setEnabled(true);
startActivity(new Intent(this, cities1.class));
}
}
}
}
Set a boolean preference when you are enabling the Button.
else{
entrycity.setEnabled(true);
getSharedPreferences("MY_PREF", MODE_PRIVATE).edit().putBoolean("isEnabled",true).apply();
startActivity(new Intent(this, cities1.class));
}
and in your onCreate set the enabled status of the button based on the preference :
entrycity.setEnabled(getSharedPreferences("MY_PREF", MODE_PRIVATE).getBoolean("isEnabled",false));

How to pause/stop music when home or back button is pressed?

Here is my code. I'm very new to Java and I know that this question is already been posted but still I didn't get the expected outpost so I had to post.
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer policeSound = MediaPlayer.create(this, R.raw.police);
Button policeSounds = (Button) this.findViewById(R.id.police);
policeSounds.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (policeSound.isPlaying()){
policeSound.stop();
}
else {
policeSound.start();
}
}
});
}
}
I tried adding onBackPressed() code to this but it couldn't detect the 'policeSound' as it was detected in the previous method!
And someone please even teach me how to use #Override annotations!
To detect the 'policeSound' in other methods you need to make it be a field of a class:
private MediaPlayer policeSound;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
policeSound = MediaPlayer.create(this, R.raw.police);
Button policeSounds = (Button) this.findViewById(R.id.police);
policeSounds.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (policeSound.isPlaying()) {
policeSound.stop();
} else {
policeSound.start();
}
}
}
);
}
In your codes policeSound is a local variable which is be seen only in Oncreate() method,as Oleh Sokolov said, you should declare policeSound as a field of class.
About #Override , you could see this good explanation
and in android studio , when you press ctrl + o in java class file, you can override superclass method, and IDE will add #Override automatically for you.

Passing an object of MainActivity through View.OnClickListener()

Below is an example of My app's MainActivity.java:
/*imports and other stuffs*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (Button) findViewById(R.id.first_button);
// myButton.setOnClickListener(this); by using it, app works as desired.
myButton.setOnClickListener(new MainActivity()); //What's wrong with this piece of codes?
}
#Override
public void onClick(View v) {
if(v.getId()==R.id.first_button){
Toast.makeText(this, "Made it", Toast.LENGTH_SHORT).show();
}
}
As setOnClickListener() method requires an object of a class that implements View.setOnClickListener, it can be handled with a custom class implements View.setOnClickListener, but my question, as a beginner, is what's going on actually, while passing an object of MainActivity?
Edit:
But if I pass an object of another class and pass that one, the code works perfectly, doesn't it? and what about those codes:
public class MainActivity extends AppCompatActivity {
Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (Button) findViewById(R.id.first_button);
myButton.setOnClickListener(new MyClass());
}
class MyClass implements View.OnClickListener{
#Override
public void onClick(View v) {
Log.d("buttontest","Working Perfectly");
}
}
}
myButton.setOnClickListener(new MainActivity());
With that piece of code you are setting on OnClickListener for your myButton. However, you are not creating the correct listener object. You are creating a new MainActivity object that is not the correct type.
myButton.setOnClickListener(this);
This is correct, because the class implements View.OnClickListener and has the implementation with the void onClick(View v) method in the class.
You can also do this if you like too:
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// add code here
}
});
This will create an new OnClickListener object with the onClick method implemented as well. This is passing an anonymous class to the setOnClickListener.

Multiple buttons in MainActivity Android studio

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText textmsg;
static final int READ_BLOCK_SIZE = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textmsg = (EditText) findViewById(R.id.editText1);
}
#Override
public void onClick(View v) {
Button noteBtn = (Button) findViewById(R.id.noteBtn);
Button resuBtn = (Button) findViewById(R.id.resuBtn);
Button agenBtn = (Button) findViewById(R.id.agenBtn);
noteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Notes.class);
}
});
resuBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Results.class);
}
});
agenBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Agenda.class);
}
});
When I run the application the buttons don't work. If I set the buttons as so..
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button agenBtn = (Button) findViewById(R.id.agenBtn);
Button resuBtn= (Button) findViewById(R.id.resuBtn);
Button noteBtn = (Button) findViewById(R.id.noteBtn);
agenBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Agenda.class);
startActivity(intent);
}
}); etc...
If I use this code above, the code works fine and the buttons work correctly. But other functionality with different classes/activities won't run. Could someone please show me a solution or explain how to solve this issue.
You should place:
this.noteBtn = (Button) findViewById(R.id.noteBtn);
this.notBtn.setOnClickListener(this);
this.resuBtn = (Button) findViewById(R.id.resuBtn);
this.resuBtn.setOnClickListener(this);
this.timeBtn = (Button) findViewById(R.id.timeBtn);
this.timeBtn.setOnClickListener(this);
in onCreate() instead of onClick(). Make the buttons belong to the class, so you can reference them in onClick(). You should not be setting new onClickListeners in onClick(), but should rather have a switch statement based on the clicked view's id to determine which button was pressed.
this code
#Override
public void onClick(View v) {
Button noteBtn = (Button) findViewById(R.id.noteBtn);
Button resuBtn = (Button) findViewById(R.id.resuBtn);
Button timeBtn = (Button) findViewById(R.id.timeBtn);
...
}
is not working because it's never going to get call, since none of your buttons is implementing it, worse yet they have not even been initialized because the onClick has not and will never be called.
the correct way to do it is like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button but1 = (Button) findViewById(R.id.but1);
Button resBtn = (Button) findViewById(R.id.resBtn);
Button noteBtn = (Button) findViewById(R.id.noteBtn);
agendaBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View viewTimeTable) {
Intent intent = new Intent(MainActivity.this, Agenda.class);
startActivity(intent);
}
}); etc...
because you are first getting a reference to your buttons and assigning them the onClickListener to each one of it.
I will suggest reading more about the Android Activity life cycle you can find it
here

Categories