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.
Related
In my application, I want to transit from Fragment to Activity.
For this purpose I am using following code for Animation,
Bundle bndlanimation =ActivityOptions.makeCustomAnimation(getActivity(), R.anim.slideinleft,R.anim.slideinright).toBundle();
startActivity(intentonboard,bndlanimation);
This works fine.
But What I want to do is that, I am moving from Fragment To Activity.
So I just want to apply the exit animation for Fragment only. Next Activity should be added behind the scene without animation.
So What Should I write in place of enter animation ?
I tried with 0 instead of R.anim.slideinright. But It effects on Exit animation.
Thanks
if you in fragment Layout and wanna to get intent to same activity you must use this...
Intent intent = new Intent(getActivity(),MainActivty.class);
startActivity(intent);
getActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
getActivity().overridePendingTransition(R.animator.slide_in_from_left, R.animator.slide_in_from_right);
Use getActivity() After startActivity(intent);
May this help you.
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!
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
I have 4tab on every tab I have listview, on selecting row(any row) I want to generate activity with 6tab,.
#parkinson, Ok, so create an activity that has tabs. OnClick of a list item spawn a new intent and startActivity(new intent). BTW punctuation will help us understand what you are trying to say.
http://developer.android.com/guide/topics/fundamentals.html
Also read this, It will help with spawning new activities.
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!