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
Related
The splash screen should move towards up and down, then go to the next screen. however, it just change the screen normally. the problem i think maybe between the handler and the animate splashscreen. this is my splash.java. hoping anyone can help thank you.
package com.example.dashboard;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.widget.HeaderViewListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.airbnb.lottie.LottieAnimationView;
public class Splash extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 3000;
ImageView logo,splashImg;
TextView appName;
LottieAnimationView lottieAnimationView;
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);
logo = findViewById(R.id.logo);
appName = findViewById(R.id.appname);
splashImg = findViewById(R.id.img);
lottieAnimationView = findViewById(R.id.lottie);
splashImg.animate().translationY(-1600).setDuration(1000).setStartDelay(3000);
logo.animate().translationY(1400).setDuration(1000).setStartDelay(3000);
appName.animate().translationY(1400).setDuration(1000).setStartDelay(3000);
lottieAnimationView.animate().translationY(1400).setDuration(1000).setStartDelay(3000);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(Splash.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
It looks like you start another Activity at the same time you play your animation. Notice the .setStartDelay(3000); at the end of each of your animation calls.
Just removing this will work. However, there are better ways to handle this.
One of the ways would be to override the onAnimationEnd method of your longest animation so that it either changes the Activity immediately or the change is delayed starting from that point in time. This will look something like this:
splashImg.animate().translationY(-1600).setDuration(1000).setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
Intent i = new Intent(Splash.this, MainActivity.class);
startActivity(i);
}
});
I'm trying to make a quiz app and everything was working fine until I put in my code for my second button, now when I click start nothing happens and clicking study brings up a black screen. Start is supposed to take the user to a different activity, and study is supposed to take them to a website. Can someone check what's wrong with my code?
package com.example.rupin.whosthatpokemon;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class questionactivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questionactivity);
Button start = findViewById(R.id.start);
start.setOnClickListener(
new Button.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent (getApplicationContext(), one.class);
startActivity(intent);
}
});
start = findViewById(R.id.study);
start.setOnClickListener(
new Button.OnClickListener() {
#Override
public void onClick(View view) {
Intent i;
i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.pokemon.com/us/pokedex/"));
startActivity(i);
}
});
}
public void goToActivity2 (View view){
Intent intent = new Intent (this, one.class);
startActivity(intent);
}
}
As Loris Securo said in the comments, "
you have btn.setOnClickListener twice instead of start.setOnClickListener". That means you never set the onClickListener of your start button.
Also, in the second onClickListener, you have:
i = new Intent(view.getContext(), one.class);
You should instead do:
i = new Intent(questionactivity.this, one.class);
Although view.getContext() should technically work, I always seen this used as the first parameter in the Intent constructor which is a Context object. Since this (an instance of an Activity) can be cast to a Context, it is better to get the context of the outer class , and this would explain why you get a black screen when trying to go to the other activity.
Side note: Your class names should start with a capital letter and be CamelCased such as ClassOne or QuestionActivity.
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 am making a simple scare your friend app. You have to press a button and then set a minute timer that will then bring up classic exorsist icon and scream on screen. I tried putting android:persistent="true", but it didn't work...
Here's my activity:
package com.odysseus.myapp;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
MediaPlayer scareMusic;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startTimer = (Button) findViewById(R.id.btimerStart);
scareMusic = MediaPlayer.create(MainActivity.this, R.raw.monster_scream);
startTimer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Thread scareTimer = new Thread(){
public void run(){
try{
sleep(5000);
Intent activityIntent = new Intent("com.odysseus.myapp.SCARER");
startActivity(activityIntent);
}catch(InterruptedException e){
e.printStackTrace();
}
}
};
scareTimer.start();
}
});
}
}
I am really new to android so don't just say use a service or something because I don't know what that is. Other answers I found were too advanced for me so please explain as much as you can!
There's no way to truly make your app immune to shutdown. The attribute "android:persistent" gets ignored for all apps that are not System apps.
That being said, to make sure that the application fires the intent after the given time, you'll probably have to place the launching code in a serivce (if even possible then).
Instead of using Activity you can use a Service that always run in the background. See this answer for how to create a app that just has an activity. Android app with Service only. As an work around you can create an Activity no content view or transparent layout, then in this activity start the service and then quickly close the activity using finish().
Now in the Service you can use the exact code that you are trying to use in an Activity. But remember to stop the Service after showing com.odysseus.myapp.SCARER.
Update :-
In your com.odysseus.myapp.SCARER activity after showing the code you can use the following command to stop the Service.
stopService(new Intent(this, service.class));
To use services is not really hard. Just create a new class and add extends Service. When you're done doing that you should add this method:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Your code here
return START_STICKY;
}
Inside this method you can launch your media player. To stop the service you just put stopSelf() in the onDestroy(). Good luck!
I am writing a code for going to the next page after clicking a button so I have written the code that I have mentioned below i just want to confirn that it is correct as i cant check it now, I know this is silly but I need help
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class DetailsActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);
btnNextScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent nextScreen = new Intent(getApplicationContext(), JewelInfo.class);
// TODO Auto-generated method stub
startActivity(nextScreen);
}
});
}
}
Assuming that your JewelInfo extends from an Activty it looks fine.
I think you should use Intent nextScreen = new Intent(DetailsActivity.this, JewelInfo.class); instead of getApplicationContext()
The correctness of code is never checked just by looking at a bit of code. Syntactically, it seems to not contain an error. Semantically, how should we know? You need to define what you want to do, how you want it to look, etc. Basically, what are the requirements?
We can then validate the code against the requirements but not assess the correctness as that is only discerned by executing the code and evaluating the result of the execution on the device it is executed against the requirements.
use DetailsActivity.this instead of getApplicationContext(), and declare your JewelInfo activity on the manifest file : <activity android:name=".JewelInfo" />.Check this tutorial about how to switch between activities and pass data between them