MORE INFO
I have doubts about the Android Structure of an App I´m trying to develop, I´m not expert, just an amateur and curious guy.
I GOT CLEAR ANSWERS FROM YOU GUYS AND I´M SO THANKFUL FOR IT. I HAVE BEEN DOING IT RIGHT, JUST NEED SOMEONE WHO TELL ME SO.
structure -
MainActivity - ButtonA - ButtonB - ButtonC - ButtonD - ButtonE - Button F
ActivityA - ButtonA1 - ButtonA2 - ButtonA3 - ButtonA4
ActivityB - ButtonB1 - ButtonB2 - ButtonB3 - ButtonB4
ActivityC - ButtonC1 - ButtonC2 - ButtonC3
Settings - Name - Date - Numbers -
Layouts: GOT IT RIGHT
Function: READY TO GO
THIS IS WHAT I´M TRYING TO DO
-Each button of each activity (above) start a short DrawableAnimation of 30 images tops.
-Each DrawableAnimation repeat itself continuously.
QUESTIONS
The App start without problems, each button start a new DrawableAnimation without problem, but The App don´t clean the memory used after finish one DrawableAnimation, so sometimes I get the Out of Memory Error, How can I fix this? Is there a way to clean memory after one DrawableAnimation and before a new one starts?
When a DrawableAnimation Start on an Activity different that the MainActivity (second, third, etc) return to the MainActivity or quit because the Out of Memory Error. What is happening here?
The App have to record how many times a Button is click, how can I do this?
CODE TO START EACH DRAWABLEANIMATION
private void addListenerOnButton() {
view = (ImageView) findViewById(R.id.image);
button = (Button) findViewById(R.id.startanimation);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
frameAnimation.stop(); frameAnimation = null;
view.setBackgroundResource(R.drawable.animation01);
frameAnimation = (AnimationDrawable) view.getBackground();
frameAnimation.start();
I try to stop or clean memory using frameAnimation.stop(); frameAnimation = null; but it does´t work.
1.Yes, MainActivity is the default activity, but you can change your "launcher" activity in your AndroidManifest file. The line below defines the launcher activity
<category android:name="android.intent.category.LAUNCHER" />
However, it should not be "MainActivity.xml", but "activity_main.xml", because Android's convention is sensitive about letter cases. The java file is "MainActivity.java". If you want a new screen, you should create a new activity(e.g. SecondActivity.java) with its layout(e.g. activity_second.xml). There are also alternative ways to change the screen, for example hiding and showing new views in single layout file but they are more advanced.
2.You can start a new activity from the current activity. For example, if you want to start SecondActivity from MainActivity, first you should declare the intent;
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
Then start this;
startActivity(intent);
You can locate this for example in an setOnClickListener method.
3.Not so sure what you wanted in this one.
If you want to make things in a right way, you should:
1) Learn from the free, avaialble, easy to find sources (in your case it's https://developer.android.com/develop/index.html).
2) Gain some experience, by writing code, writing code and writing more code.
3) If you will meet any difficulties while writing code (unexpected output, exceptions and e.t.c.) you should ask the question here while showing your code.
Good luck.
P.S.: You will not find a private teacher on stackoverflow.
Another rank amateur here ... If you're working in a development environment such as Eclipse, you can "make a new activity" and the layout will be at least started for you, but yes, each new screen will need its own xml file for the layout.
To go to another activity, you will need to use Intent per http://developer.android.com/training/basics/firstapp/starting-activity.html and each Activity will need its own Java file.
Your third bullet point doesn't seem to be a question, so I won't try to answer it.
Good luck!
Related
I am creating my first app. In this app, I have an expandable list with many items. When I select any of these items, I want several paragraphs to be displayed. Do I need to create an Activity for each of these items if text is the only thing I want displayed? I know that there has to be an easier way. I did create it like this at first and it seemed very bulky (30+ activities), so now I have it set up so that when an item is selected, the setContentView opens the corresponding layout with the text that needs to be displayed. This works but there is a catch, whenever I hit the back button, it takes me back to my main activity class and not my expandable list class. I want the user to be able to go back and select something else from the list. Any guidance as to what I need to do would be appreciated.
I would suggest creating string resources for each item you would like to display, then creating one activity with a TextView. Then, instead of creating new intents for each activity, create an intent that goes to the new activity, and add an extra that contains the text for the TextView. For example:
Activity1:
myButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(this, ParagraphView.class);
intent.putExtra("textData", getResources().getString(R.string.myText));
getBaseContext().startActivity(intent);
}
});
In the onCreate of the viewer, add this to get your TextView:
Intent intent = getIntent();
String textData = intent.getStringExtra("text");
Now, we need to write the text into the TextView:
TextView tv = (TextView) findViewById(R.id.myTextView);
tv.setText(textData);
All you have to to is set up your string resources and button click listeners. You may consider this easier than having lots of activities (it's definitely easier to manage entries this way) but does require a bit of setup.
Edit: Thanks to #ianhanniballake for pointing out a much better way (I don't even know what I was thinking at the time...)
Edit2: Wow, I REALLY messed up my code. (Hopefully) Fixed now
This is my first week of Android development and I am having some troubles so please be patient with me.
This is really simple but all other answers weren't clear or detailed enough for me to apply it.
I am trying to switch from my "activity_main.xml" to a second .xml after a button click. I have already connected the button and put in setContentView(R.layout.view) and it works but I want it to animate. I want the view to come from the right and then the opposite when the user press back. I am doing this in eclipse if that helps.
Thanks in advance!
Assuming what you're after is a transition between two activities, here's what you're going to need to do:
Create a new Activity class. For this example, lets name it MySecondActivity.
In this new Activity class, make sure you're inflating the new layout xml.
In the original Activity class, open the new Activity with an Intent, then on the new activity, call the overridePendingTransition with the animation you want:
Code sample:
Intent intent = new Intent(this, MySecondActivity.class);
startActivity(intent);
getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
In this example, I'm using Android pre-defined animations. You can create your own too, but I feel this might be enough for your needs.
Hope this helps.
For example, we have 2 activities: Main - libgdx and Aux - Android normal Activity.
In Main, we have a button to go to Aux( read and follow this instruction ), and a button in Aux to return to Main.
My startActivity code:
#Override
public void startActivity(Class<?> C) {
Intent intent = new Intent(appContext, C);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
appContext.startActivity(intent);
}
When we go from Main to Aux, it's OK, but when we return, it just show a blank WHITE screen( it still interact with player but doesn't show anything):((.
Could you please help me a proper way to solve this?
P/S: Currently I leave onCreate and onResume, etc... blank.
Your problem is OpenGLES context loss.
As of today, libgdx v0.9.9 Textures are managed, this means they will be reloaded automatically in case of context loss. If you created them like this:
texture = new Texture(...);
If not, then you have to reload them manually.
Development Environment: Eclipse 3.7.0
Developing: Android 3.2 application for Market Place
Using: aChartEngine 0.7.0
I'm new to the development scene but have done a bit of coding in the past various languages, I've created the ZopaStats(on Marketplace) app, but I'm now trying to convert a text based stats page to be displayed in a bar graph using achartengine.
I can get the graph to display from an activity via another activity i.e.:
Intent achartIntent = new TemperatureChart().execute(this);
startActivity(achartIntent);
but this gives me an additional activity screen i.e.:
Main Screen -> 1st Activity (Original Text Stats View) -> 2nd Activity (Graph)
Therefore, with I hit back on the graph screen, I get the blank 1st activity screen.
I hope I'm making sense here.
So what I tried to do was launch the activity from the Main Screen (i.e. my Main class) e.g.
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent achartIntent = new TemperatureChart().execute(this);
startActivity(achartIntent);
}
});
But eclipse gives me the following error in the code:
The method execute(Context) in the type TemperatureChart is not applicable for the arguments new (View.OnClickListener(){}}
I've tried letting Eclipse change the method but this then causes other problems, so I think what I'm really looking for (in a round about way) is to find out what the difference is when I can try to start the activity from another Activity class rather than starting it from the main class.
I apologise for the misuse of terms etc, as I say I'm new. I've been looking at this for a few days now but the Intent and Activity documentation doesn't help me much so I just need a few pointers.
Thanks,
In your example, the this reference that you're passing to execute() is your annonymous inner subclass of OnClickListener. This is not a context object, which is what eclipse is complaining about.
Rather, you want to pass in the activity instance. Assuming the code snippet you posted lives in a class named MyExampleActivity, then you can use MyExampleActivity.this from inside the inner class to access the instance of the containing class. You should be able to pass that to TemperatureChart.execute()
There is no difference in starting an activity from the main activity or from any other activity. You just create an intent, and call startActivity on it.
For completeness, the new code is this:
N.B. My original class is called ZopaStats.class
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent achartIntent = new MarketZopaGraph().execute(ZopaStats.this);
startActivity(achartIntent);
}
});
This works great, only a single Activity windows, once again many thanks for the quick response, in record time ;)
when we use achartengine to draw graph in includes it own activity...i.e.org.achartengine.GraphicalActivity..
when we press back it shows own activity which is used to show graph..to hide these activity call finish() method on onPause() method.
I have a problem that I can't seem to find the solution to.
I have an app that loads the main.xml file on startup, of course. In it are several buttons, and I want the buttons to take me to a different XML file. I just used setContentView(R.layout.newlayout.xml) method for that, and it works great.
The problem comes in after that. If I reference any of the buttons or other objects in the new layout, the app won't even finish loading before it errors out and closes on the emulator. However, if I take all references to objects out, the app runs fine.
I can navigate TO the new layouts, but their buttons can't do anything. Do I need to create a separate Java file for each layout? Or am I doing it all wrong? I'm trying to be as specific as I can. I suppose you could say I need to have different "pages" in my app as a website would.
I think what you are trying to do is best solved using multiple java files, each one defining it's own android Activity.
While it is possible to have multiple layouts/views in a single activity, this will generally make the code more complex and harder to read/debug in the future. By having each 'screen' in its own file, it will be a bit easier to manage all the different views you need to juggle.
The buttons and views only can refer to those mentioned in the current SetContentView() file..
u can test this by creating a button and initialising to an R.id... without setting the content view.. U will get a force close..
so if u change the XML file u shud initialise stuff again....
Ok, for anyone out there with the same problem and haven't figured out how to do it, as I said in my comment on ylebre, my Coworker and I have finally discovered how to do it. First off, we added
implements OnClickListener
to the class, after
extends Activity
then, we created a new java file, and at the beginning of the file it called
setContentView(R.layout.newlayout);
instead of main. Then, we made a button as follows:
Button button1 = (Button) findViewById(R.id.button01;
button1.setOnClickListener(this);
then later in the code:
public void onClick(View v) {
switch(v.getId()) {
case R.id.button01:
startActivity(new Intent(this, NEWJAVAFILE.class));
break;
}
}
And that's it! We just copied and pasted that code into NEWJAVAFILE, changed the names and such, and we were able to navigate freely back and forth. As ylebre said, all of the code for the new activity is in the NEWJAVAFILE.java. OH and don't forget to add the name of the java file to the manifest inside the tags:
<activity android:name=".NEWJAVAFILE">
</activity>
it all seems so simple now!