I know that this has been asked many, many times before, but how do I make my button play a sound when it's pressed?
this is my button's code:
<Button
android:id="#+id/c1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
android:background="#drawable/button_selector" />
and here is my MainActivity.java:
package com.example.appname;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
what do I have to add to make my button play a sound? If it helps I'm using Eclipse.
EDIT
I don't really know what I'm doing when it comes to this, so if you could please show me what to add to my code that would be great.
You could use an OnClickListener. Here is the developer section detailing how to do it.
You need to implement the onClick method once you assign the OnClickListener to your button. Here is the section about buttons, the code here will get you started.
For playing the sound, I recommend a MediaPlayer You can set your MediaPlayer to start (playing) in the onClick method.
first of all you have to understand events generated by views in android ,in your case button click event, you have to use OnClickListener
and youre code look like
public class MainActivity extends Activity implements
OnClickListener{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnPlaySound = (Button) findViewById(R.id.c1);
btnPlaySound.setOnclickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.view_id:
break;
default:
break;
}
}
}
Related
So I just started to learn Android Studio for android development and started going through the myFirstApp tutorial on their website. I am trying to add a method to the button but can't get it to work. I have the sendMessage method in MainActivity.java and when i go to select it from the "on click" drop down list, it doesn't appear. I have the correct imports as well. Does anyone know why this may be? Thanks.
Here is what my code looks like:
package com.example.tyler.myfirstapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
}
Add android:onClick="sendMessage" attribute to your button tag in activity_main.xml.
From the tutorial you are following:
Now return to the activity_main.xml file to call this method from the button:
Click to select the button in the Layout Editor.
In the Properties window, locate the onClick property and select sendMessage [MainActivity] from the drop-down list.
You must've skipped this step.
I got stuck with this one as well, so i skipped down the page a bit and grabbed the complete code for MainActivity.java (apart from the first line that i kept).
Once i'd replaced this i got the sendMessage[MainActivity] appear on the onClick Button attribute.
package com.example.your....
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
The answer for this is that you do not have the function INSIDE of the last curly brace at the bottom. Just undo the function you pasted and make sure to repaste just before the last curly brace and then it will all be fixed.
This question already has answers here:
How to start new activity on button click
(28 answers)
Closed 6 years ago.
So far from all the tutorials I've looked at, most only get to the point of "Button Was Clicked" I need my second activity button to open a new activity.
I named this class, fifth_layout.xml
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Amazon"
android:drawableLeft="#drawable/amazon"
android:drawableStart="#drawable/amazon"
android:layout_weight="0.07"
tools:ignore="HardcodedText"
android:id="#+id/button10"
android:textSize="35sp" />
After that in my FifthActivity.java I have
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FifthActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button = (Button) findViewById(R.id.button10);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
}
});
}
}
I just need the button to be able to open a new blank activity. But when i click the button nothing happens? I just need a new activity. i feel like the code is correct i just need help on what i might be doing wrong.
You have to use intent to open a new Activity. Assuming you want to open an activity called SixthActivity from your FifthActivity.
You should use this:
public class FifthActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button = (Button) findViewById(R.id.button10);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this,SixthActivity.java);
FifthActivity.this.startActivity(intent);
}
});
}
}
Hope this helps,
Regards.
Your onClickListener does nothing, of course nothing happens.
Create a new Activity (let's say you name it NewActivity, add it to the AndroidManifest.xml and add the following code you your existing activity:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
final Intent intent = new Intent(FifthActivity.this, NewActivity.class);
startActivity(intent);
}
});
I have a very strong feeling you're kind of lost in Android Development. I strongly suggest you follow Udacity's Android Development course.
Alright, so you have the single activity with its layout, right?
What your asking is "how do I launch another activity with another layout?"
To do this, we'll use an "intent" (think of an intent as how the activities talk to eachother, they get passed back and forth)
To create the intent and start, you'll need these couple lines:
Intent intent = new Intent(this, Target.class);
startActivity(intent);
Which should work within your onClick.
If you created the activity within Android Studio with File>New>Activity, this should have put the activity in your AndroidManifest.xml already, otherwise you'll need to add it yourself.
I'm a newcomer to Java and Android Studio so am still learning, so sorry if this question is simplistic. I have recently discovered that as an alternative to coding an onClickListener in the activity.java file to respond to a button click, it can be done more simply with an android:onClick="method name" in the corresponding layout.xml file. I have already looked at this site's questions relating to the pros and cons of each method, but that is not my problem. My problem is, where and how can I declare a text field that is used by two methods without having to declare it in each method?
The code that follows is as minimal as possible. There are two buttons and one text view. If I attempt to put "TextView themessage = (TextView) findViewById(R.id.message);" anywhere other than in both methods, I get the dreaded "Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference".
Is there anywhere and any way to make a single declaration that can be used by both methods without generating this exception?
Extract from the .xml file
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:id="#+id/topbutton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="88dp"
android:onClick="doit"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="***"
android:id="#+id/message"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Again"
android:id="#+id/againbutton"
android:layout_below="#+id/message"
android:layout_centerHorizontal="true"
android:layout_marginTop="63dp"
android:onClick="starit"/>
Top end extract from the .java file
package com.example.owner.clickme;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void doit (View v)
{
TextView themessage = (TextView) findViewById(R.id.message);
themessage.setText("Well clicked, Sir!");
}
public void starit (View v)
{
TextView themessage = (TextView) findViewById(R.id.message);
themessage.setText("****");
}
.
. etc.
It is common practice in Android to instantiate any Views within the onCreate, or where you inflate the main content view.
So, make a field inside of MainActivity like so
private TextView themessage;
Within onCreate get the TextView
theMessage = (TextView) findViewById(R.id.message);
Then you are free to use theMessage across both methods.
All in all, it looks like this
public class MainActivity extends AppCompatActivity
{
private TextView themessage;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
themessage = (TextView) findViewById(R.id.message);
}
public void doit (View v)
{
themessage.setText("Well clicked, Sir!");
}
public void starit (View v)
{
themessage.setText("****");
}
}
I think what you want to do is have the TextField as member of your activity class.
Something like that probably
public class MainActivity extends AppCompatActivity {
TextView theMessage;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
theMessage = (TextView) findViewById(R.id.message);
}
public void doit () {
theMessage.setText("Well clicked, Sir!");
}
public void starit() {
themessage.setText("****");
}
}
I'm just started to make my first android app and I'm trying to get more familiar with the basic principles of android developing. So, in no time my MainActivity exploded with lines of code. To make my code more maintainable, i'm trying to put pieces of code in different activities. Also according to the design principles of android: Don't Overload a Single Activity Screen
Now I'm struggling to use different activities with a single XML layout. I found some similar cases here like: this one But i'm also reading here that I should use fragments. I can't see how to work this out properly.
The specific problem I encounter with my code is that the second activity should change the background of the imageview to normal with the setImageResource, but it doesn't.
My code:
package com.test.scores;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class MainActivity extends Activity implements View.OnClickListener {
private ImageButton btn1, btn2;
int varMinusScore;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(this);
btn2 = (ImageButton) findViewById(R.id.btn2);
btn2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
varMinusScore = 1;
startActivity(new Intent(getApplicationContext(), ResetImageResources.class));
btn1.setImageResource(R.drawable.btn01p);
}
switch (v.getId()) {
case R.id.btn2:
varMinusScore = 2;
startActivity(new Intent(getApplicationContext(), ResetImageResources.class));
btn2.setImageResource(R.drawable.btn02p);
}
}
}
And the second activity:
package com.test.scores;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
public class ResetImageResources extends Activity {
private ImageButton btn1, btn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (ImageButton) findViewById(R.id.btn1);
btn2 = (ImageButton) findViewById(R.id.btn2);
btn1.setImageResource(R.drawable.btn01);
btn2.setImageResource(R.drawable.btn02);
finish();
}
}
Activities are absolutely isolated from each others. The same XML file which you are setting as a content of each activity doesn't mean that it same/shared instance of layout. You should think not in terms of layouts, but in terms of activities.
In your case you just start second Activity, change background of buttons here, then go back and see first Activity. Any changes in second Activity would not be mirrored somewhere else. That's it.
Try this:
Insert a button to finish in second activity. Use finish() under the button interface button.setOnClickListener(new OnClickListener(){} ); then you'll clearly notice the difference between the backgrounds. Only if you click you can go back to main activity.
i am fairly new to android programming and usually find my answers to my problems by searching, but this one i just cant and its very confusing.
The code itself doesn't show any signs of problems, well i do get 2 java exception breakpoints but i dont know how to fix those as they are "unknown"but when i run it on the emulator it says the application has stopped unexpectedly force close. I try to debug it but i dont know how to do it that well. any way here are the codes btw the app is just a test all it to do is have buttons that take me to other "pages" and back. I would appreciate any help.
Main java file
package com.simbestia.original;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class original extends Activity implements View.OnClickListener {
Button button1, button2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.pagetwo);
button2 = (Button) findViewById(R.id.main);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.pagetwo:
setContentView(R.layout.pagetwo);
break;
case R.id.main:
setContentView(R.layout.main);
break;
}
}
}
Main xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<Button android:text="pagetwo" android:id="#+id/pagetwo" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
Well here is what i change the code to this one is just one button but it works with multiple and i made a class for every page...
package com.simbestia.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mainmenu = (Button) findViewById(R.id.mainmenu);
mainmenu.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), mainmenu.class);
startActivityForResult(myIntent, 0);
}
});
}
}
Works how i wanted to so its all good i guess ty again
In the command line/terminal, use ./adb logcat to see, in real time, warnings, erros and such from your device, while you run your app. That should help you a lot.
Note: Don't forget to be in the right folder... <android-sdk-version>/platform-tools, that's where the ADB is.
You should learn to debug your own application. Starting with a break point right in the first line of your onCreate() method.
You can also take a look here: http://www.droidnova.com/debugging-in-android-using-eclipse,541.html
Another possibility is to add a log call in the first line of your onCreate() so you can see where the log of your app starts...
edit:
The way you want to switch the layout is wrong. Try layout switcher or start a new activity for your new layout. calling setContentView more than once is basically just wrong...
Some things to check: make sure your code source folder is the same as your package name (com.simbestia.original) ; make sure it builds (without errors) before you try and run it and make sure that your manifest file has its package attribute set to your package name (com.simbestia.original).