My activity_main_layout has 2 buttons:
//Start game on click
<Button android:id="#+id/btnStart"/>
//If not sign in Google Game play Service
//Sign in then show leader board on click
<Button android:id="#+id/btnLeader board" />
Click on btnStart to begin playing game:
btnStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent intent = new Intent(MainActivity.this, game_play.class);
startActivity(intent);
finish();
}
});
As you can see, class game_play is the place to play and update height score.
I'm wondering where to extends BaseGameActivity? Inside class MainActivity or game_play or both of them?
I try many times but it's not successfull.
I'm really an amateur, I expect that you give me some ideas.
Have you gone through the QuickStart Guide? This is a good place to start if you are new to developing games for Android. To answer you question, you need to access the Play Game Services from the activity that is going to handle signing in and calling game services APIs.
From your description it sounds like both your activities will need to make calls (the main activity is showing the leaderboard, and the game activity is most likely posting scores).
Extending your activity from BaseGameActivity really is not needed any longer (for an entertaining explanation watch: Death of BasegameActivity. What you do need to do is implement the two interfaces that handle initializing the GoogleAPIClient:
public class MainActivity extends Activity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
}
To implement these, refer to the samples and the doc: https://developers.google.com/games/services/android/init
You can do the same in your game activity. The application will maintain the login state between activities, so the player will not have to login twice.
Related
I'm currently developing a game app for my 2nd year project at uni, but i'm a little stuck on how i can move from a non-activity class to an activity. So my none activity class is a quit screen within the game (yes i know you shouldn't use quit in a game but its one of my requirements), when i click on a button i want it to take me to the activity. (which basically thanks the user for playing and then exits the app) using System.exit(0) is just going back to the last activity visited.
The screen extends my Game Screen Class which is a parent class i created, this is the code for what happens when the user touches the 'yes' button in the quit screen:
#Override
public void update(ElapsedTime elapsedTime) {
Input input = game.getInput();
List<GameTouchEvent> touchEvents = input.getTouchEvents();
if(touchEvents.size() > 0){
GameTouchEvent touchEvent = touchEvents.get(0);
if(yesButtonBound.contains((int) touchEvent.x, (int) touchEvent.y)){
System.exit(0);
}
I'm not looking for specific code answers as this is a project but any advice or hints would be very appreciated thanks!!
so I'm new to Android Studio and currently am coding an Othello board game app for my phone and in my first game class I open with:
public class gameClass extends AppCompatActivity implements View.OnClickListener{
private board b = new board();
And this works fine and runs the game but then I wanted to run the same code on another activity with a few alterations, so I copy and pasted the entire code from the 1st class onto the second class and then made a few modifications (including me changing the class name where appropriate) but for some reason the modifications come up on both activities when I run the program.
My question is how can I change 1 class so that it doesn't affect the other without making one of them a private class since I bring in subs from other classes and send data from class to class.
Edit: As more information is needed apparently, I'll show all the parts of code where there is the issue.
public class game2 extends AppCompatActivity implements View.OnClickListener{
private board b = new board();
....
....
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.play:
mysound.start();
b.playgame();
setboard();
current.setImageResource(R.drawable.blackothello3);
counterforWhites.setText("WHITE : " + b.count(Colour.White));
counterforBlacks.setText("BLACK : " + b.count(Colour.Black));
showPossibleMove(); // The main issue is that this subroutine works in the other activity where this is NOT called for.
break;
The code in the other class for the other activity is almost the exact same, the only exception is that the
showPossibleMove() command is not there yet when I run that activity it shows the runs this sub and it shows the possible moves in that screen when I dont want it to.
your question is not clear . If you are willing to get true answer please put your codes here to see what you have done .
Generally , if you extend one class from AppCompatActivity , you are creating an activity that must be declared in AndroidManifest.xml.However , for passing any data from one activity to another your first option is Intent.
If you asked me , I would create a new activity from wizard not copying and past.
Hope these suggestions would help you.
In android Studio, I want when i click on button , next activity/fragment should come from right side and present activity sholud gone left.I implimented its working on Activity but not on adapters is showing error.
holder.questions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(DoctorsProfile.this,Questions.class);
i.putExtra("DOCTOR_ID",doctor_id);
startActivity(i);
overridePendingTransition( R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}
});
overridePendingTransition is working on Activity but not working on Adapters of Recyclerview and Listview, Please tell any other option. I want when i click on recyclerview item next Activity should navigate or come from right side by using overridePendingTransition.
Fragment fragment = Fragment.newInstance();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.fragment_slide_left_enter,
R.anim.fragment_slide_left_exit, R.anim.fragment_slide_right_enter,
R.anim.fragment_slide_right_exit);
Utils.addFragmentToActivity(fragmentTransaction, Fragment, R.id
.content_frame);
This tip features how to change Android’s default animation when switching between Activities.
The code to change the animation between two Activities is very simple: just call the overridePendingTransition() from the current Activity, after starting a new Intent. This method is available from Android version 2.0 (API level 5), and it takes two parameters, that are used to define the enter and exit animations of your current Activity.
Here’s an example:
//Calls a new Activity
startActivity(new Intent(this, NewActivity.class));
//Set the transition -> method available from Android 2.0 and beyond
overridePendingTransition(R.anim.slide_in_right_up, R.anim.slide_out_right_up);
These two parameters are resource IDs for animations defined with XML files (one for each animation). These files have to be placed inside the app’s res/anim folder. Examples of these files can be found at the Android API demo, inside the anim folder.
for example code visit http://www.christianpeeters.com/android-tutorials/tutorial-activity-slide-animation/#more-483
Change like this code you must be passing activity as context in adapter
holder.questions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(DoctorsProfile.this,Questions.class);
i.putExtra("DOCTOR_ID",doctor_id);
Activity activity = (Activity) context;
activity.startActivity(i);
activity.overridePendingTransition(R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}
});
Note : Context is the Base Object of Activity
Update :
I have checked the accepted answer but hope you understand that it will be called everytime when your activity get launched and thats not supposed to be best practice. I am suggesting better approach if you want to follow the accepted answer .
Alternative :
Pass one parameter in bundle to new activity to make sure that transition coming from that specific adapter so double transation should not happen when you are coming rom any other activity also.
There is a easy way to do this. just put overridePendingTransition on your OnCreate method of next Activity/Fragment.So that when next Activity will come it will come according to your choice.Need not add overridePendingTransition on adapters.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ask_question);
overridePendingTransition( R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}
I searched a lot but i did not find my answer. I have developed an android application where on the very first lunch user will be shown a welcome screen made of viewpager. The problem is i don't know which place is the best to put the welcome activity code in my application.
The simplest way it could be that in the main activity at the very fist line even before super.onCreate(), inside onCreate method where i try to get the shared preference value and then evaluate whether it is fist lunch. If it is, then i start the welcome activity as shown below
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean welcome = sharedPreferences.getBoolean(getString(R.string.key_welcome), true);
if (welcome) {
// go and start welcoming activity
Intent intent = new Intent(this, WelcomeSlideActivity.class);
startActivity(intent);
}
super.onCreate();
}
}
But i found another approach to deal with it. It is Application class. Since Application class is the first one, which runs even before any other codes in my application. So i thought, i would be nice to do it there as shown below
public class App extends Application {
#Override
public void onCreate() {
super.onCreate();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean welcome = sharedPreferences.getBoolean(getString(R.string.key_welcome), true);
if (welcome) {
// go and start welcoming activity
Intent intent = new Intent(this, WelcomeSlideActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
}
So i am in dilemma, which one would be the best option to choose. And i am even not sure if i am doing it in the right way since there is no such documentation in android developer website or anywhere.
Have a look at how to create splash screens the correct way. https://www.bignerdranch.com/blog/splash-screens-the-right-way/
As for using the Application class - this is primarily used for Application-wide configuration for maintaining a global application state. Hence starting an activity from here does not make much sense as it's purpose has changed into becoming an entry point into the application rather than providing state for the app as a whole.
Furthermore, why not make the WelcomeSlideActivity the first 'launcher' activity? Then in there, you can create the logic of whether to launch the next activity without history or whether to show the current view.
Ideally, you should create a splash screen activity, which determines whether to show the WelcomeSlideActivity Or the MainActivity. The advantage of this is that while he app determines which Activity to launch, the user is presented with a splash screen that informs the user that the app has started
Hi i'd like to develop a Dating Sim App for Android with eclipse.
The Game consists of Scenes.
Each Scene consists of one Background Picture (City), one Charakter Picture and one Balloon Image with text in it (Text which the Character says).
When i tap the Balloon, the next Scene will appear, the spoken text will change, maybe one of the pictures will change too.
It's a very simple game it just consists of .png Pictures and Text Strings.
This is my problem:
I assumed that every scene has to be an activity (each of my scenes has it's own layout.xml)
So i created now like 200 layouts, and just started to do the java classes for each activity (each class states, when a button is pressed, please go to the next layout file.)
It all worked pretty well until activity 25 or so appeared. When i tap it, the game crashes.
I tried to fix it with "finish" (please see code below) but it didn't help. Maybe i used it the wrong way?
I tried to bypass the activity that crashes (switched from Activity 25 to Activity 30 instead from 25 to 26) But it also crashes.
Questions 1:
Do i have to many Activites loaded? How can i fix it?
I really would like to continue with "Each Scene is one Activity" if possible, because it's the most easiest way to do it for now.
Question 2:
How would a professional Programmer do this?
Would he create a flash Animation for this and insert it as a view in ONE Activity?
Or just plain Java code?
Thank you very much in common.
package com.irbstudios.zargoslovestories;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class game_p1_prestory_00001 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_p1_prestory_00001);
TextView tx = (TextView)findViewById(R.id.textView1);
Typeface custom_font = Typeface.createFromAsset(getAssets(),
"fonts/rosemary.ttf");
tx.setTypeface(custom_font);
Intent svc=new Intent(this, BackgroundSoundService.class);
startService(svc); //OR stopService(svc);
}
protected void onPause() {
super.onPause();
overridePendingTransition(0, 0);
}
public void onButtonClick(View view) { //Relevant Part for StackOverflow Question:
if(view.getId() == R.id.btn_prestory_00000) { //when the balloon is touched
startActivity(new Intent(this, game_p1_prestory_00002.class)); //got to the next activity
finish(); //delete this activity out memory.
}
}
}
I'd suggest the following:
Create an activity with a full screen ViewPager
Create a Fragment that will be a "scene", but this fragment would be able to load any of the layouts. As a starting point, just convert one of your activities to a fragment, it's pretty straightforward.
Create a FragmentStatePagerAdapter to display your fragments inside the ViewPager
This is a very compact structure, and it allows you to define very cool transitions (cube/3d/fadings/scrolls...) between scenes with very little effort (see PageTransformers when you have a working prototype)
After this is done, to change scenes you can do:
mViewPager.setCurrentItem(1); // change to scene 1
Some Docs:
http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html
http://developer.android.com/training/animation/screen-slide.html