Android Studio, making a text based game - java

My first android app I'm creating, a text based game. Everything is looking great so far with functionality. I'm just wondering 2 things, one, is there a way to control how the new activity opens in the screen (new activities open from the center of the screen on my app, takes about a half second), and second, its it better to have each button pressed choice open a new activity or is there a better way to clear the screen and use the if and else commands in java.

I think as for the first part, you may want to use animation on transition between activities/fragments.
You do want to read more about fragments as i think it will solve your second question.
Check this small tutorial for the animation part.
Check docs for fragments.
Good luck.

Related

How to control the screen slide by code in the android?

I'm trying to writing an app that can help me to control my screen.
For example,I can press a button,and my android phone can help me to slide from left to right by itself.
Please refer to the picture as below.
I saw some of the Transfer beads Game Cheat MAYBE use this method to control the screen slide automatically, but actually I don't know how it does.
For another example, refer form the following video. In this video,he made the phone slide the screen by itself. What I want to do is as same as his work. =)
https://youtu.be/a5quGThvjT4
Thank you all in advance!
As it figured out the OP needs a drag/drop on the same screen for his problem and not swiping between activities or fragments:
The keyword is "android drag and drop": https://developer.android.com/guide/topics/ui/drag-drop.html

New to android fragments - looking for a few answers

I'm fairly new to android, and have been working on an app for a few weeks. I want the app to run on handhelds and tablets. I decided to implement fragments after running things on my tablet and am having trouble getting my head around it to get the ball rolling. My app is like most: The first page you see is a menu list of various activities you can navigate to, some of which call other activities. I've read countless tutorials, but still have a few questions, I'm hoping someone can help with. I haven't had any luck finding the answers on the internet.
1) Should the main menu be in a fragment or just remain an activity that calls fragments?
(I'm thinking it should be a fragment as well)
2) I've read that the activities are just a portal to the fragment, so in the main menu all of my logic should go in the fragment?
3) Why does eclipse throw a dummy class into the wizard setup? Shouldn't this package just be deleted and the array adapter or whatever be put into the logic of the main menu or do I require another class for a good reason?
4) I also read somewhere that the Main activity must be called Main.java is this true? I don't see why it would be.
I realize these are all probably pretty simple questions to someone that has a grasp on fragments, but I don't!
Thanks for any answers
Ken
1) Yes, if you are handling logic and ui, and you are attempting to do your best and want to have a structure that you can easily support multiple screen sizes, your menu should be a fragment.
I think the activity is the mechanism to handle what fragments to display.
So on a phone the first activity just displays the menu fragment and launches a new activity to handle the selection.
While on a tablet you might have the menu fragment on the left side of the screen and do a fragment transition for the right side of the screen upon user selection
2) Your activity still has work to do to coordinate those fragments as my example in 1.
3) You don't need that class
4) You can name anything at all, anything at all. Pay attention to what's specified in your manifest. Your app can even have multiple starting points / icons in the launcher if you specify it as such in the manifest.
1) If you want different layouts depending on device and/or orientation then yes. Maybe you want the main menu to be located in a pane on the left on a tablet while it's a full screen list on a phone.
2) This is a bit more tricky to answer. It all depends on how you design your application, and there really is not right or wrong here. You could opt for an MVC like approach where you put the actual logic in a controller. You could let your Activity deal with what happens when a menu item is selected. You could use an eventbus (Otto or Guava) to communicate between fragments. It's up to you.
3) Not needed
4) No, you don't have to name it Main.java. I think you may be confusing this with the public static void main(String[] args) entry point of a standard J2SE program?

View different images depending on what item in my list view I click

I'm fairly new to Android programming and I've got this project I need to finish and I'm currently stuck.
I've got a standard listview in a Menu class with an array containing around 20 different elements. So what I want to do is load images in an imageview depending on which item in the listview I click, and since I'm a beginner the only idea I had was to make a new activity for each imageview which seems like a pretty bad way to do it since I'd need about 20-30 new activities..
To sum things up what I want is:
Code for making ONE activity that will display a different image depending on which item in the listview I click, probably pretty basic coding I want as simple solution as possible.
If possible I'm also looking for a solution that includes an SQLite database that stores the URL of an image and then display it in a single activity, also depending on which item I press in my current listview.
(I hope you understand my needs, and if you need I can also post my current code for the Menu class if it helps you help me) Or you can just show me a different way to make this work, I appreciate every answer! Thanks in advance!
NOTE
And please keep in mind, I'm a noob at Java and Android so keep it rather simple or at least explain what you do.
When you click on a list item, display the image in another view in the same layout, unless you want the image to take up the entire screen real estate. If you want it in the entire screen, go to a new Activity by sending the activity an Intent.
Activities are the "controller" of your application. They interact with the visible UI and the input from the user. You don't need a separate activity for each image, just an activity that's associated with a "place" in the UI (an ImageView) where you'll display the image.
I'd start by adding the images as resources under res/drawable before going on to databases.
You are going to have to do most of this yourself. There really isn't any substitute for taking the time to learn Java and Android. There are several tutorials and Android University classes under the Resources tab in the Developers Guide; I suggest you do all of them.

Good way to make a launcher screen for sub-apps within your app?

I'm making an app for a company, and they want a main screen when you first enter the app with a couple sub-apps (right now they're separate activities) that you can go into (glossary, faq, calculator, etc).
Any ideas what the best way to do this is? I'm new to all this.
Thanks
Use a Gallery or a Grid, depending on the number of sub-apps we are talking about.
Example of Gallery: https://ssl.gstatic.com/android/market/com.ihaapp/ss-480-0-0
The grid view will look like the launcher app on your android phone.
Sounds like a case for the Dashboard imho.
To implement this: Just lay out a few ImageViews or Buttons in a grid, and start the activitys inside the various onClick() events.

When to use new layouts and when to use new activities?

I'm making a game in Android and I'm trying to add a set of menu screens. Each screen takes up the whole display and has various transitions available to other screens. As a rough summary, the menu screens are:
Start screen
Difficult select screen
Game screen.
Pause screen.
Game over screen.
And there are several different ways you can transition between screen:
1 -> 2
2 -> 3
3 -> 4 (pause game)
4 -> 1 (exit game)
4 -> 3 (resume game)
3 -> 5 (game ends)
Obviously, I need some stored state when moving between screens, such as the difficulty level select when starting a game and what the player's score is when the game over screen is shown.
Can anyone give me some advice for the easiest way to implement the above screens and transitions in Android? All the create/destroy/pause/resume methods make me nervous about writing brittle code if I'm not careful.
I'm not fond of using an Activity for each screen. It seems too heavy weight, having to pass data around using intents seems like a real pain and each screen isn't a useful module by itself. As the "back" button doesn't always go back to the previous screen either, my menu layout doesn't seem to fit the activity model well.
At the moment, I'm representing each screen as an XML layout file and
I have one activity. I set the different buttons on each layout to call setContentView to update the screen the main activity is showing (e.g. the pause button changes the layout to the pause screen). The activity holds onto all the state needed (e.g. the current difficulty level and the game high score), which makes it easy to share data between screens. This seems roughly similar to the LunarLander sample, except I'm using multiple screens.
Does what I have at the moment sound OK or am I not doing things the typical Android way? Is there a class I can use (e.g. something like ViewFlipper) that could make my life easier?
By the way, my game screen is implemented as a SurfaceView that stores the game state. I need the state in this view to persist between calls to setContentView (e.g. to resume from paused). Is the right idea to create the game view when the activity starts, keep a reference to it and then use this reference with setContentView whenever I want the game screen to appear?
This question has been asked a lot. Did you read these other posts?
Android: What is better - multiple activities or switching views manually?. This link in particular talks about the Android Design Guidelines which "don't mention switching Views at all; it's centered around an Activity-as-View design."
Android - Should I use multiple activities or multiple content views
Android app with multiple activities
How to pass the values from one activity to previous activity
I'm not sure what you mean by the back button not always going back to the right screen correctly. I've got a game with a similar structure to yours, and the back button always takes the user correctly up the chain of activities.
Furthermore, using the onResume, onPause etc is somewhat necessary. What happens to your application if the phone rings? (Yes I know, some people still do strange things like using their phone to receive calls! :P) The OS tries to call the onPause method in your activity, but if that isn't implemented then your application won't act as expected. Another useful thing with onResume is it lets you update your tables as soon as the user returns to the view. For example, your player has just completed a level and is then brought back to the select difficulty screen. If you simply recover the previous screen from memory, it might not have been updated to take into account that a the level was just completed. However, if you put some code in onResume to handle that, then that will always be executed before the player sees the screen.
Lastly, you say transferring data around activities is a pain with intents - yes, that's probably true. But I usually find transferring any kind of complex data a pain, no matter how you do it. Are intents really that much worse, or is it just that things aren't as easy as you'd hoped? I don't mean any offense with that; I also often find things which intuitively seem easy to be rather frustrating to implement in code.

Categories