MyFirstApp tutorial Android Studio -- sendMessage Issue - java

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.

Related

How do I get back to the start page with a button?

I'm trying to develop a game app. At the gameover screen, I want to have a button that goes back to the start when you click it. But the problem is, it doesn't work. I really don't know why it doesn't work, I have tried everything but can't find the problem. Can someone help me?
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class GameOver extends AppCompatActivity {
MediaPlayer gameoversound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameover);
Button weiter_button = (Button) findViewById(R.id.weiter);
weiter_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { goToMainActivity(); }
});
}
private void goToMainActivity() {
Intent back = new Intent( this, MainActivity.class);
startActivity(back);
}
}
Change this to GameOver.this
You don't have to cast widgets explicitly anymore, except for some special cases. It has been this way for a while.
If the game is over, you'd best insert a finish(); after launching the main Activity. You don't want users to be able to go back to the gameover screen by pressing the back button.
Set up FLAGS for your Intents. This comes in handy because if you didn't finish(); some Activity it remains in the stack, so it will be launching that one, but then you might want a new one. This will cause issues in navigation.
Add/ check your onBackPressed() methods for the 2 Activities.
Furthermore, specify that you are overriding the method
#Override
public void onClick(View view)
{
}
In the XML, <Button> tag, add
android:clickable=true
android:focusable=true

Opening New Activity in Second Activity [duplicate]

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.

Multiple activities make changes in a single layout

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.

How do I make my button play a sound?

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;
}
}
}

multiple onCreate(Bundle under one activity for webview and button

I built a web app, and I am making a wrapper to put it on the app store. I have a main page, or start page for the app, and the submit button successfully loads my webview page, and the app works well.
I wanted to add a small webview above the submit button where I could display updated news before users enter the app, such as new terms of use.
I followed examples to get the webview to load on the main page, and it works - the content is displayed, but when I add the webview, the submit button doesn't work any longer.
Eclipse throws an error "Duplicate Method OnCreate(Bundle) in type TOS, so I tried changing OnCreate to OnStart or OnCreateView for the button. It renders from the layout xml, but there is no functionality. I'm certain it is a novice syntax error, so I've just posted the Tos.java code:
package com.packagename.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Tos extends Activity {
private Button button;
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tos);
button = (Button) findViewById(R.id.buttonUrl);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Mapp.class);
startActivity(intent);
}
});
}
private WebView TermswebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tos);
WebView TermswebView = (WebView) findViewById(R.id.webView);
TermswebView.loadUrl(someURL");
};
};
So you need only one onCreate method for each Activity not object, which is where you're thinking(I assume) that you need multiple onCreate methods.
So.. just merge the two you have...
public class Tos extends Activity {
//UI ELEMENTS
private Button button;
private WebView TermswebView;
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tos);
TermswebView = (WebView) findViewById(R.id.webView);
TermswebView.loadUrl("http://www.barglance.com/assets/tos/tos.php");
button = (Button) findViewById(R.id.buttonUrl);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Mapp.class);
startActivity(intent);
}
});
}
I hope this helps. For more information about activities and what onCreate actually does and when it's called, refer to Android Docs - Activity

Categories