Can you please help me about this game that I created, I want to reload the same activity, like restart game in quiz game. I hope you guys can help me, this is the code:
package com.example.splash;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.core.widget.ImageViewCompat;
import android.content.Intent;
import android.media.Image;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class stagenumbereasy1 extends AppCompatActivity {
Button congratsback,congratshome,congratsforward,reload;
ImageView congrats,tryagain;
Animation fromsmall,fromnothing;
MediaPlayer click,congrats1,die;
#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_stagenumbereasy1);
click = MediaPlayer.create(this, R.raw.click);
congrats1 = MediaPlayer.create(this, R.raw.congrats);
die = MediaPlayer.create(this, R.raw.die);
final Button butt8=(Button)findViewById(R.id.button8);
final Button butt9=(Button)findViewById(R.id.button9);
final Button butt10=(Button)findViewById(R.id.button10);
final Button butt11=(Button)findViewById(R.id.button11);
final Button back=(Button)findViewById(R.id.button12);
final LinearLayout overbox=(LinearLayout)findViewById(R.id.overbox);
final LinearLayout congratsbox=(LinearLayout)findViewById(R.id.congratsbox);
final LinearLayout tryagainbox = (LinearLayout)findViewById(R.id.tryagainbox);
final LinearLayout trybox=(LinearLayout)findViewById(R.id.trybox);
fromsmall = AnimationUtils.loadAnimation(this, R.anim.fromsmall);
fromnothing = AnimationUtils.loadAnimation(this, R.anim.fromnothing);
congratsbox.setAlpha(0);
overbox.setAlpha(0);
tryagainbox.setAlpha(0);
trybox.setAlpha(0);
congratsback=(Button)findViewById(R.id.congratsback);
congratshome=(Button)findViewById(R.id.congratshome);
congratsforward=(Button)findViewById(R.id.congratsforward);
congrats=(ImageView)findViewById(R.id.congrats);
reload=(Button)findViewById(R.id.reload);
tryagain=(ImageView)findViewById(R.id.try1);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent back=new Intent (stagenumbereasy1.this,stagenumber.class);
click.start();
startActivity(back);
}
});
butt8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
click.start();
congrats1.start();
butt8.setEnabled(false);
butt9.setEnabled(false);
butt10.setEnabled(false);
butt11.setEnabled(false);
back.setEnabled(false);
overbox.setAlpha(1);
overbox.startAnimation(fromnothing);
congratsbox.setAlpha(1);
congratsbox.startAnimation(fromsmall);
}
});
congratshome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent home = new Intent(stagenumbereasy1.this, Home.class);
click.start();
startActivity(home);
}
});
congratsforward.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent easy2 = new Intent(stagenumbereasy1.this, stagenumbereasy2.class);
click.start();
startActivity(easy2);
}
});
butt9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
click.start();
die.start();
butt8.setEnabled(false);
butt9.setEnabled(false);
butt10.setEnabled(false);
butt11.setEnabled(false);
back.setEnabled(false);
trybox.setAlpha(1);
trybox.startAnimation(fromnothing);
tryagainbox.setAlpha(1);
tryagainbox.startAnimation(fromsmall);
reload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent reload = new Intent(stagenumbereasy1.this, stagenumbereasy1reload.class);
click.start();
startActivity(reload);
finish();
}
});
}
});
butt10.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
click.start();
die.start();
butt8.setEnabled(false);
butt9.setEnabled(false);
butt10.setEnabled(false);
butt11.setEnabled(false);
back.setEnabled(false);
trybox.setAlpha(1);
trybox.startAnimation(fromnothing);
tryagainbox.setAlpha(1);
tryagainbox.startAnimation(fromsmall);
reload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent reload = new Intent(stagenumbereasy1.this, stagenumbereasy1reload.class);
click.start();
startActivity(reload);
finish();
}
});
}
});
butt11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
click.start();
die.start();
butt8.setEnabled(false);
butt9.setEnabled(false);
butt10.setEnabled(false);
butt11.setEnabled(false);
back.setEnabled(false);
trybox.setAlpha(1);
trybox.startAnimation(fromnothing);
tryagainbox.setAlpha(1);
tryagainbox.startAnimation(fromsmall);
reload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent refresh = new Intent(stagenumbereasy1.this, stagenumbereasy1reload.class);
click.start();
startActivity(refresh);
finish();
}
});
}
});
}
}
Image of my game
I actually create another activity to make it happens but when I click the reload button it is going back to home. This is my thesis game.
I don't know if exists a recommended answer in Android docs. But, I think you can call startActivity() and finish(), like:
// Java
startActivity(new Intent(MyActivity.class));
finish();
// Kotlin
startActivity(Intent(MyActivity::java.class));
finish();
This will create a new instance of the activity and destroy the current.
For better performance, you should make tests with Fragments instead resetting an Activity.
You could try recreate(); which would recreate the activity
You could also try Intent intent = new Intent(MyActivity.this, MyActivity.class);
startActivity(intent);
Just simply use intent flags one is INTENT_FLAGS_NEW_TASK
it restart activity
Intent.setflags()
In your current activity recall it
Intent(main.this,main.class)
Related
I'm really stuck here. I'm trying to navigate to a second activity using a button but whenever i try to parse in the name of the class to the Intent method, Android Studio throws an error.
In the Intent method toWeightsScreen, it won't let me parse in a class.
Can anyone tell me what I'm messing up please.android studio snapshot
package leith.comstephen.facebook.httpswww.fitnessapp5;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button navToWeightsScreen = (Button)findViewById(R.id.Firstweights);
navToWeightsScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent toWeightsScreen = new Intent(this, cut.class)
startActivity(toWeightsScreen);
}
});
}
}
navToWeightsScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent toWeightsScreen = new Intent(getActivity(), cut.class);
startActivity(toWeightsScreen);
}
});
The this in your intent declaration refers to the button's setOnClickListener.
You should specify the actual activity. See the code below
navToWeightsScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Changed cut.class to Cut.class to follow coding conventions
Intent toWeightsScreen = new Intent(MainActivity.this, Cut.class);
startActivity(toWeightsScreen);
}
});
I'm trying to make a simple math game with two modes, addition and subtraction. I figured out how to create a button that will link the "Addition button" to the addition activity but I can't seem to figure out how to create a second "Subtraction Button" that will link to the subtraction activity. Here's my broken code:
package com.example.kirky_000.madmath;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.content.Intent;
public class MainMenu extends ActionBarActivity {
Button button;
Button button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
});
}
Your code just have some syntax error which is solved as per given code...
package com.example.kirky_000.madmath;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.content.Intent;
public class MainMenu extends ActionBarActivity {
Button button;
Button button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
});
}
}
Your second instruction to add listener is inside the first OnClickListener.
So the listener is never added to the second button. You code should be like this :
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
});
}
You need to have different click listeners for different buttons. Right now, you're putting the click listener for the second button inside the click listener for the first button. Separate them like this..
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
)};
Some points before the solution (those will help you for further coding).
Declare context after buttons declaration : So after Button
button, button2, write :
final Context context;
Always keep in mind : Always initialize objects in onCreate()
method. So in OnCreate() after
setContentView(R.layout.activity_main_menu);, write :
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
//then define context
context = MainMenu.this;
//or context = getApplicationContext();
Now addListenerOnButton() function will be like this (just replace
addListenerOnButton() with following code):
public void addListenerOnButton(){
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
});
}
Now what is the wong in your code ?
Your onClick listener code is wrong...
enjoy the coding :) (and android :))...
you can even do like this
public void mainClickHandler(View v)
{
switch (v.getId()) {
case R.id.button:
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
case R.id.button2:
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
}
I am having a screen which has a WebView showing a UI developed in HTML and PHP. Now, when i migrate to a different screen in Android and want to come back to my parent screen, I want to have a NEW WebView layout. Even though I am passing a String variable back for validating purposes, but still then it does not work out. Does this have to do anything with the savedInstanceState? Any suggestions would be appreciated. Below is the code snippet.
package com.example.notifboard;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class AdminLogin extends Activity {
boolean flag = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
flag = true;
}
protected void onResume() {
super.onResume();
if (flag == true) {
setContentView(R.layout.adminloginmixed);
final Button AdminBtn = (Button)findViewById(R.id.AdminBtn);
final Button EventGenBtn = (Button)findViewById(R.id.EventGenBtn);
final Button BackBtn = (Button)findViewById(R.id.BackBtn);
AdminBtn.setBackgroundColor(Color.WHITE);
AdminBtn.setTextColor(Color.BLACK);
EventGenBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
EventGenBtn.setBackgroundColor(Color.WHITE);
EventGenBtn.setTextColor(Color.BLACK);
AdminBtn.setBackgroundColor(Color.parseColor("#1D326B"));
AdminBtn.setTextColor(Color.WHITE);
BackBtn.setBackgroundColor(Color.parseColor("#1D326B"));
BackBtn.setTextColor(Color.WHITE);
Intent EveGenIntent = new Intent(AdminLogin.this, EventGen.class);
startActivity(EveGenIntent);
finish();
}
});
AdminBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
EventGenBtn.setBackgroundColor(Color.parseColor("#1D326B"));
EventGenBtn.setTextColor(Color.WHITE);
BackBtn.setBackgroundColor(Color.parseColor("#1D326B"));
BackBtn.setTextColor(Color.WHITE);
AdminBtn.setBackgroundColor(Color.WHITE);
AdminBtn.setTextColor(Color.BLACK);
}
});
BackBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
BackBtn.setBackgroundColor(Color.WHITE);
BackBtn.setTextColor(Color.BLACK);
EventGenBtn.setBackgroundColor(Color.parseColor("#1D326B"));
EventGenBtn.setTextColor(Color.WHITE);
AdminBtn.setBackgroundColor(Color.parseColor("#1D326B"));
AdminBtn.setTextColor(Color.WHITE);
Intent BackBtnIntent = new Intent(AdminLogin.this, SelectEntryType.class);
startActivity(BackBtnIntent);
finish();
}
});
WebView browser = (WebView)findViewById(R.id.Adminloginwebview);
browser.setWebViewClient(new WebViewClient());
browser.loadUrl("http://sivasphpmobileapps.site90.net/NotificationBoard/AdminLogin.php");
}
else {
setContentView(R.layout.adminloginmixed);
final Button AdminBtn = (Button)findViewById(R.id.AdminBtn);
final Button EventGenBtn = (Button)findViewById(R.id.EventGenBtn);
final Button BackBtn = (Button)findViewById(R.id.BackBtn);
AdminBtn.setBackgroundColor(Color.WHITE);
AdminBtn.setTextColor(Color.BLACK);
EventGenBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
EventGenBtn.setBackgroundColor(Color.WHITE);
EventGenBtn.setTextColor(Color.BLACK);
AdminBtn.setBackgroundColor(Color.parseColor("#1D326B"));
AdminBtn.setTextColor(Color.WHITE);
BackBtn.setBackgroundColor(Color.parseColor("#1D326B"));
BackBtn.setTextColor(Color.WHITE);
Intent EveGenIntent = new Intent(AdminLogin.this, EventGen.class);
startActivity(EveGenIntent);
}
});
AdminBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
EventGenBtn.setBackgroundColor(Color.parseColor("#1D326B"));
EventGenBtn.setTextColor(Color.WHITE);
BackBtn.setBackgroundColor(Color.parseColor("#1D326B"));
BackBtn.setTextColor(Color.WHITE);
AdminBtn.setBackgroundColor(Color.WHITE);
AdminBtn.setTextColor(Color.BLACK);
}
});
BackBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
BackBtn.setBackgroundColor(Color.WHITE);
BackBtn.setTextColor(Color.BLACK);
EventGenBtn.setBackgroundColor(Color.parseColor("#1D326B"));
EventGenBtn.setTextColor(Color.WHITE);
AdminBtn.setBackgroundColor(Color.parseColor("#1D326B"));
AdminBtn.setTextColor(Color.WHITE);
Intent BackBtnIntent = new Intent(AdminLogin.this, SelectEntryType.class);
startActivity(BackBtnIntent);
}
});
WebView browser = (WebView)findViewById(R.id.Adminloginwebview);
browser.setWebViewClient(new WebViewClient());
browser.loadUrl("http://sivasphpmobileapps.site90.net/NotificationBoard/NewEvents.php");
}
}
}
package com.example.example;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button btn;
#Override
public void onCreate(Bundle savedInstanceState) {
this.setContentView(R.layout.activity_main);
this.btn = (Button)this.findViewById(R.id.button);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, profile.class);
startActivity(intent);
}
});
}
}
I don't get any errors however my program also dont run as I want. I am new in android and I want to change the screen after the button is clicked for that I am using two classes so in one class my program should invoke another one onclick. How can I do this ? My code is as above.
First remove this.setContentView(R.layout.activity_main); because you declared it twice . Then declare
btn = (Button)this.findViewById(R.id.button);
after
setContentView(R.layout.activity_main);
Declare your profile activity in manifest file.Check my code below.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)this.findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, profile.class);
startActivity(intent);
}
});
1) your oncreate method should look like below one.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)this.findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, profile.class);
startActivity(intent);
}
});
2) Add profile Activity into Manifest.
3) add logs in onClick method so you should know if it is getting called on not.
Happy coding!!
I tried to make a signup page in android where I use a reset button that should clear all fields in the page. Please see the code below and correct it as my code is not working.
Button btnreset = (Button) findViewById(R.id.btnreset);
btnreset.setOnClickListener(new View.OnClickListener() {
public void restartActivity(Activity act){
Intent intent=new Intent();
act.finish();
intent.setClass(act, act.getClass());
act.startActivity(intent);
}
}
this is an signup page and fields are first name,last name,user id,password.when user click on reset button it clear all the fields who's filled the user.I'm give a complete source code to you please check this:
package com.boyzcorn.android.fyp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Intent;
public class signup extends Activity{
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.signup);
Button b = (Button) findViewById(R.id.btnClick2);
Button btnreset = (Button) findViewById(R.id.btnreset);
final EditText eText1 = (EditText)findViewById(R.id.firstname);
final EditText eText2 = (EditText)findViewById(R.id.lastname);
final EditText eText3 = (EditText)findViewById(R.id.userid);
final EditText eText4 = (EditText)findViewById(R.id.password);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
{
if(eText1.getText().toString().equals("") ||eText2.getText().toString().equals("") || eText3.getText().toString().equals("") ||eText4.getText().toString().equals(""))
{
Toast.makeText( getApplicationContext(),"Fill Empty Fields",Toast.LENGTH_SHORT ).show();
}
else
{
Intent i = new Intent(signup.this,login.class);
startActivity(i);
}
}
}
});
}
btnreset.setOnClickListener(new View.OnClickListener() {
public void restartActivity(Activity act){
Intent intent=new Intent();
act.finish();
intent.setClass(act, act.getClass());
act.startActivity(intent);
}
}
public void onClick(View arg0) {
}
}
You've only defined the function; you're not calling it. You will need to execute restartActivity(signup.this) from the OnClickListener.
Also, your intent will likely not execute, because its parent has been finished. Perhaps rearranging the lines of code might help, but a better solution would be having the parent activity start it. Try replacing the click listener with:
btnreset.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Activity act = signup.this;
Intent intent = new Intent(act, act.getClass());
act.startActivity(intent);
act.finish();
}
}