In my great expectations of Google Android coming to Canada on June 2nd, I’ve started experimenting with developing some apps for the Android platform. My first app is called “The Taxman” and will calculate the amount of tax you owe per year in your province/state – well only Canada for now.
I had trouble adjusting to what an “Activity” was and how to handle it. Here is a quick and dirty way to create an Activity, and to switch to another Activity (think of it as another screen) on the click of a button.
1. Create a new Android project – or you might already have one created.
2. Add a new Class that extends android.app.Activity. You need a total of two classes that extend Activity. You will switch from one Activity to another.
3. Now, we’ll create two XML files to store the layout of each Activity. Under the res/layouts directory create a copy of main.xml
4. Each XML file will contain 1 button. On the click of the button, the Activities will switch.
main.xml will contain:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#000000" android:text="This is Activity 1" /> <Button android:text="Next" android:id="@+id/Button01" android:layout_width="250px" android:textSize="18px" android:layout_height="55px"> </Button> </LinearLayout>
main2.xml will contain:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#000000" android:text="This is Activity 2" /> <Button android:text="Previous" android:id="@+id/Button02" android:layout_width="250px" android:textSize="18px" android:layout_height="55px"> </Button> </LinearLayout>
So each Activity will have a text that says “This is Activity x” and a button to switch the Activity.
5. Add the second Activity to the main manifest file. Open AndroidManifest.xml and add:
<activity android:name=".Activity2"></activity>
The final result will look similar to this:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.warriorpoint.taxman2" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Activity1" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Activity2"></activity> </application> <uses-sdk android:minSdkVersion="3" /> </manifest>
If you forget to do this, then the you will get a Null Pointer exception because “Activity2” will not be found at runtime. It took me some time to find out how to find what Exception was getting thrown as well. I will include how to debug and look at Exceptions in another future post.
5. Open Activity1.java and enter the following code:
package com.warriorpoint.taxman2; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Activity1 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button next = (Button) findViewById(R.id.Button01); next.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent myIntent = new Intent(view.getContext(), Activity2.class); startActivityForResult(myIntent, 0); } }); } }
Here’s a quick explanation of what this does:
- setContentView(R.layout.main) makes sure that main.xml is used as the layout for this Activity.
- Gets a reference to the button with ID Button01 on the layout using (Button) findViewById(R.id.Button01).
- Create san OnClick listener for the button – a quick and dirty way.
- And the most important part, creates an “Intent” to start another Activity. The intent needs two parameters: a context and the name of the Activity that we want to start (Activity2.class)
- Finally, the Activity is started with a code of “0”. The “0” is your own code for whatever you want it to mean. Activity2 will get a chance to read this code and use it. startActivityForResult means that Activity1 can expect info back from Activity2. The result from Activity2 will be gathered in a separate method which I will not include here.
6. Open Activity2.java and enter the code below:
package com.warriorpoint.taxman2; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Activity2 extends Activity { /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main2); Button next = (Button) findViewById(R.id.Button02); next.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent intent = new Intent(); setResult(RESULT_OK, intent); finish(); } }); }
This code does the following:
- Sets main2 as the layout for this Activity
- Gets a reference to Button02 and creates an OnClick listener
- In the OnClick listener, the Activity finishes with finish(). setResult() returns information back to Activity 1. In this example, it returns no information; and Activity1 doesn’t even have the listener to receive this information anyway.
That’s it! Run it!
The app will load in Activity 1:
When you click the button you will see Activity 2. There are no animations, no tweens, etc, so the screen will just “change”. I’ll talk about animations in future posts.
And clicking on the button “Previous” here will go back to Activity1.
Still to come:
1. How to create animations when switching screens.
2. How to switch using a dragging motion of your finger.
3. How to see a log of the exceptions that your app throws.

June 11, 2009 at 07:52
A simple question…
What makes the emulator start with Activity1 rather than Activity2? Is it the addition of
?
Thanks.
Tim
June 16, 2009 at 11:22
Hey Tim,
If I’m not mistaken it’s the following lines in the Manifest that make Activity1 the default activity:
June 16, 2009 at 11:22
The opening and closing tags got cut out.
intent-filter
action android:name=”android.intent.action.MAIN” /
category android:name=”android.intent.category.LAUNCHER” /
/intent-filter
July 6, 2009 at 06:36
If your screens are simple it is better to use Layouts instead of definining it as activity by switching Layouts
July 6, 2009 at 22:08
Cool. Thanks
August 12, 2009 at 00:54
how can i find out need tip solve those problem?
1. How to create animations when switching screens.
2. How to switch using a dragging motion of your finger.
3. How to see a log of the exceptions that your app throws.
thanks
August 22, 2009 at 01:19
I thought this was a great tutorial–straightforward, lots of detail, and it picked up right where the sample “Hello, Android” app left off.
I couldn’t understand why my version of your application crashed whenever I pressed the Next button. Finally I figured it out–thanks to your subsequent post about reading the logs:
E/AndroidRuntime( 1001): android.content.ActivityNotFoundException: Unable to find explicit activity class {tld.domain.helloandroid/tld.domain.helloandroid.Activity2}; have you declared this activity in your AndroidManifest.xml?
Pretty clear message! Since I had been building off the “Hello, Android” sample, I just added a new class file for Activity2, which wasn’t properly registered in the AndroidManifest.xml file. Just thought I’d mention that here in case anyone else makes the same mistake.
August 23, 2009 at 12:17
Awesome, thank you very much Chris for posting that; and thanks for the kind words as well.
September 29, 2009 at 01:55
Hi!
do you have an idea on passing an object Bundle back to the first activity and how can you retrieve the data?
Nice Tutorial. Thanks for the info…
October 16, 2009 at 08:41
And if I want to switch activity trought MenuItem, how can i do?
Thanks
November 26, 2009 at 13:28
Hi there. I’d like to learn to program on Android but I’m completely clueless about how the activities work. I wanted to ask, how do you pass parameters to activities? I want to be able to change colour settings for a “game” activity based on a selection made in a “Settings” activity. Could you tell me how to pass variable values across activities?
Thanks
December 3, 2009 at 18:45
hey..
that really helped. thanks:)
December 7, 2009 at 07:17
this is really great!!!
thanks a lot.
December 11, 2009 at 11:47
Many thanks for this! So many of the demos out there just demo API fnality, all just in Java, all in onCreate, with zero clues as to how to manage this kind of simple application flow. What are we supposed to do, fill onCreate with dozens of lines of Java?!
Thanks again 
December 15, 2009 at 21:53
Just what I’m looking for!
December 27, 2009 at 17:18
Thanks for writing.
December 27, 2009 at 17:18
…this.
December 28, 2009 at 18:39
Hi, thanks for this. Best tutorial i found.
January 5, 2010 at 11:52
i have to agree with everyone. this is an excellent tutorial. thank you so much for putting it together.
January 7, 2010 at 11:34
Thanks! I’ve been looking at how to do this for a couple of hours!
January 15, 2010 at 04:20
me also stuck up for two days……..
thank you very much its working fine…..
February 3, 2010 at 23:28
Great post, took about 10 minutes to get the example working! Thanks a bunch!
February 5, 2010 at 19:48
An excellent introduction for one who is learning to develop with Android. Thanks for taking the time to write it up!
February 9, 2010 at 08:55
Hi,
In am developing a android application. The framework is such that I dont allow Activities to laucnh each other.
Eg: ActivityA can only talk to datahandlerA
ActivityB can only talk to datahandlerB
datahandlers can talk to any other datahandlers.
Some operation happens on ActA. It informs DHA. DHA does some network operation. It informs DHB. DHB does some process than launches ActB.
Please let me know how this can be done.
Thanks
Sam
March 8, 2010 at 00:59
thanks……..got it
March 18, 2010 at 14:11
Thanks!
I found this useful, after several hours of NOT finding this information on the developers site. I modified it for my app, and it worked.
I tried adding onClick method to button and adding a click listner to my code and both of those solutions did not work. This did the trick!
March 18, 2010 at 14:24
Also, I just noticed this also lets the BACK button (hardware) work as well, so you can go back to the last activity. Smooth!
April 25, 2010 at 19:08
hey,
Thanks for the useful post.
I’d like to know if there is a way by which i can know the name of the activity that started my current activity.
my problem is that i react differently according to which activity started me.
Thank you
April 29, 2010 at 04:56
Hey nice tutorial man but i have a query
I am using a listview to create a list of items
and then when the user clicks on or touches on one item he is shown the activity of that particular item. The problem i am having is that :-
1.How can i set an onclicklistener for list view
2.Do i have to validate each item separately because i have like 50 items and so and then writting the code somewhat like:-
“IF this item is clicked then do this and bla bla” will take a hell lot of time. M sure there will be some way in which we can find out the item clicked by user and then store it’s position in some variable and then thru dat variable call the respective activity this shudnt take more than 4 lines of code but i m stuck and cudnt get anythng in my head.
Any suggestions will be highly appreciated
abhishek@delvelogic.com
May 19, 2010 at 10:49
I got a question. What could be a reason of misworking back button action. I mean - when I enter another activity, i press back button (on emulator) and instead of going back it shows up android desktop… I am overriding the
public void onBackPressed() {
Log.e(”myTag”, “out…”);
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
but it only throw log, still not working properly..
June 14, 2010 at 07:24
I love you!!
I’ve been trying to find some place that will explain how to start an activity in a simple way, but I’ve just found shit that was loads of code and I could not managed to get it to work. When I just stumbled across this post and I realize it’s as simple as pie if someone just explain in the great way you did in this post!
I hope you have a great day, I know mine just got a little bit better, Thanks again!
June 15, 2010 at 06:53
HI Abhishek,
I think this gonna help u,
package com.example.helloandroid;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class ListExample extends ListActivity {
/** Called when the activity is first created. */
static final String[] COUNTRIES = new String[] {
“Afghanistan”, “Albania”, “Algeria”, “American Samoa”, “Andorra”,
“Angola”, “Anguilla”, “Antarctica”, “Antigua and Barbuda”, “Argentina”,
“Armenia”, “Aruba”, “Australia”, “Austria”, “Azerbaijan”,
“Bahrain”, “Bangladesh”, “Barbados”, “Belarus”, “Belgium”};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] countries = getResources().getStringArray(R.array.countries_array);
setListAdapter(new ArrayAdapter(this, R.layout.list_item, countries));
// setListAdapter(new ArrayAdapter(this, R.layout.list_item, COUNTRIES));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
}
June 29, 2010 at 23:51
simple, gets the job done. thanks!
July 8, 2010 at 15:57
Thanks so much !!! You’ve saved me hours and hours of searching.
July 19, 2010 at 08:08
i am calling activities inside an activity in songle tab host, but when i press back button from any of sub activity (on emulator) and instead of going back it shows up android desktop. Please guide me. Thanks.
July 23, 2010 at 11:42
Awesome.
I was flipping back & forth in the pages of my Android Wireless Development Book for 2 days trying to get this to work. The example you gave was the best way to describe it. Simple layouts & clear examples of code. I was able to copy/paste 6 lines of code off your site and everything is working now.
Thanks for taking the time to share the knowledge!
July 23, 2010 at 20:25
Boyan - Great tutorial! I got Activity 1 to slide (R to L) to Activity 2. How do I slide back (L to R) from Activity 2 to Activity 1? Thanks.
July 23, 2010 at 20:29
Boyan - I found my own personal error…your code is perfect!
July 31, 2010 at 15:42
Keep getting error when pressing the Next button saying…
07-31 19:38:40.570: ERROR/AndroidRuntime(2478): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.deano/com.deano.SecondScreenView}; have you declared this activity in your AndroidManifest.xml?
my first class has the following intent definition in it
package com.deano;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainScreenView extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), SecondScreenView.class);
startActivityForResult(myIntent, 0);
}
});
}
}
August 11, 2010 at 15:52
Thanks for the great tutorial, made my day!
I’ve been messing around with different layouts but couldn’t figure out how to address them properly. Works like a charm now.
August 19, 2010 at 15:22
Seconded. Best bit of guidance on this nightmare I’ve seen so far. Cheers.
September 16, 2010 at 03:44
awesome….. u made this task so easy….. cheers dude…
September 16, 2010 at 03:53
Great tutorial, helped me a lot. Thanks!
September 18, 2010 at 15:59
Thank You so much. You know how to explain things. Google should hire you to re-write ALL of their documentation.
September 19, 2010 at 05:18
Whenever I click on Next, I get the error “Application stopped unexpectedly” and I have to force close it. Why is that? Please help me. I have tried other examples of switching between the activites and the same thing happened. Whenever i click on next it gives me runtime error.
Anyone has any idea why is it so?
September 28, 2010 at 12:23
@rosebeat and Dean-O:
Make sure you have defined your activities in AndroidManifest.xml, that one got me on several occasions, easy to forget.
/J
October 1, 2010 at 07:51
how to switch between two activities through timer??
October 28, 2010 at 04:50
Thanks for the great tutorial.I was puzzling from last two days in activity and intents.i have successfully implemented your exmple and learn a lot.
can you please tell me how to open an image in new screen wheni click the button next.
i have just modify your code a little in main2.xml and the button becose i want to open an image.
activity1.java is such as while activity2.java is as:
package com.example.Activity;
import android.app.Activity;
import android.os.Bundle;
public class Activity2 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
//how to code here to see an image in new screen
//if i have to use tag(just guessing) in my
//main2.xml
}
}
Please help i m puzzling from couple of days in this issue.
Thanks for the great great tutorial………
November 3, 2010 at 08:05
This tutorial has been invaluable. I’d been playing around with tons of tutorials and not one actually covered with switching activities. Thanks for the great tutorial.
November 19, 2010 at 08:59
many thanks for this tutorial…perfect
December 3, 2010 at 10:30
it really helps alot.
thanks
December 7, 2010 at 02:49
Hi
thanks a ton!!!
This helped me at the right moment to complete my project. I was trying to do a nested on click listener to listen to two activities and It was messed up until your post helped me to achieve the right way!!
December 12, 2010 at 22:58
Thanks a million!!
Your tutorial really helped me to get a clear picture and itemize the various files that must be updated to get second or third activity (screen) to fire up properly
a) AndroidManifest.xml
b) res–layout–newactivity.xml
c) newactivity.java
December 16, 2010 at 07:45
Thank you for this very helpful tutorial, greatly appreciated!
December 27, 2010 at 04:34
i want to switch from activity1 to activity2 and then from activity2 to next activity3… by clicking next button but i got an error … here is code of my activity2 NAME as Result1 have prevoius and next buttons ..
help me
package seecs.BIT.Result;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Result1 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
Button next = (Button) findViewById(R.id.widget40);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish() ;
}
});
Button next1 = (Button) findViewById(R.id.widget39);
next1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Result2.class);
startActivityForResult(myIntent, 0);
}
});
}
}
December 28, 2010 at 11:39
So… You’re saying that if I have like a hundred screens for my game, I’d be making 100 activites. That is some serious business.
December 31, 2010 at 19:53
Very nicely explained all the concepts
January 27, 2011 at 20:29
Thank you. I am new to android, and been fighting this simple task for hours. this helped out a lot.
February 3, 2011 at 05:22
first application stopped unexpectedly!
what is the error
February 3, 2011 at 08:11
Thx! been looking for this for a while.
February 9, 2011 at 06:00
great one thank you
February 10, 2011 at 02:34
this is my manifest file:
Even I have added the activity2.. still my application crashes..
my logcat:
02-10 11:59:27.792: ERROR/AndroidRuntime(1723): java.lang.RuntimeException: Unable to start activity ComponentInfo{button.pack/button.pack.Activity1}: java.lang.NullPointerException
Please any one find the solution? I have did same java code above there in forum !!..
February 10, 2011 at 02:35
Even I have added the activity2.. still my application crashes..
this is my manifest file:
my logcat:
02-10 11:59:27.792: ERROR/AndroidRuntime(1723): java.lang.RuntimeException: Unable to start activity ComponentInfo{button.pack/button.pack.Activity1}: java.lang.NullPointerException
Please any one find the solution? I have did same java code above there in forum !!..
February 11, 2011 at 06:06
Hi I am trying to do this example no error in code. But in emulator it is showing like “Sorry! The application example(process login.Example)has stopped nuexpectedly.Please try again”. Please any one help me.
February 14, 2011 at 04:53
Hi.. who are all getting the above error here i got solution for this..
I had give id as login twice so it was unable to refer dat id. When i changed n checked i got the solution.. Id should be unique.
February 16, 2011 at 10:34
Oh great great great tutorial.. thank you so much!!! I was really stuck at this switching point in android!
March 2, 2011 at 14:44
Thank you very much! This was perfect!
March 3, 2011 at 00:34
Good. finally, I find it
March 4, 2011 at 09:23
Good tutorial - better than official Android site for this topic. Thanks for taking the time…..
March 6, 2011 at 15:51
Thanks so much for this! It really helped me out.
March 7, 2011 at 00:29
hi!
i was trying to switch from one activity to another using the example given here!
but when i tried to switch from activity1 to activity2 it stops and says “that the project stopped due to an unexpected error”. kindly guide me for the same, as soon as possible!!
March 9, 2011 at 04:26
I thankful to you. This code is very very useful for me.
March 14, 2011 at 04:19
hi,
After a long run your Application has helped me a lot!!! Really useful!!! Thanks for the post!!!!
March 23, 2011 at 13:21
Sir how does the setContentView(R.layout.main); work?
The letter R has errors in it on me. every single line of R
something wrong?
March 23, 2011 at 16:01
Thanks! Your explanations solved my problems, after searching all day. Excellent tutorial
March 25, 2011 at 06:16
Good one dude..
April 10, 2011 at 10:34
Thanks man, this helped me a lot.
April 18, 2011 at 21:22
I am still confused with the usage of javascript like the picture above, is there anything easier?
but this can add to the experience for me, maybe good to try.
April 21, 2011 at 02:28
Hey guys, I read through the review and had problems moving between screens as well. I actually came across this because I was Google searching for a solution to my problem. For all of you struggling with this I have 2 huge tips for you that have already been mentioned before.
1: Make sure the class is registered in your AndroidManifest.xml
2: Check your id’s for all your items in the layout file. If any 2 items have the same id the program will bomb out. All items have to have *unique* ids.
That was my problem and it wasted 2 hours of my time before I found the two items I had ID’d the same.
April 23, 2011 at 02:39
Thank you very much for the tutorial have being searching non stop for about 13hours before i found this.
May 7, 2011 at 10:28
Thanks man,
For the such anice tutorial tutorial.It helped me a lot.
Thanks once again….
May 10, 2011 at 06:55
Hey, i get an error in both Activity1.java and Activity2.java saying that “id cannot be resolved or is not a field”. I copy pasted everything as it is. Can anyone tell me what the problem is.
June 17, 2011 at 08:37
@Tejas: save main.xml
After 3 days of searching (i’m java and android beginner) i found this. PERFECT!!! Exactlly what i was looking for.
Thank you very much
August 1, 2011 at 07:49
hi,
I am fresher on andoid even in softwair field.
thaneven it is good
August 11, 2011 at 14:27
http://stackoverflow.com/questions/7029031/navigating-between-activities-is-not-happening
in the above link i have posted my problem.. someone pls try n answer
Thanks
August 28, 2011 at 06:36
VERY NICE AND SIMPLE EXAMPLE
September 6, 2011 at 11:53
Ahhh, finally! Clear, simple and a clean explanation of how to go from one screen to the next via a button action. Why is it so difficult to get such great examples? Excellent tutorial. For all the people that are too fast out there and don’t read everything . . . you need to modify the AndroidManifest.xml file for this to work. Seems to be a recurring issue that most of us skip over.
September 8, 2011 at 00:48
wow, this tutorial is a lot easier to understand for someone new to android programming. google official android guides contains too much theory and most of them make no sense to a newbie like me! But this tutorial is very clear and achieve what I need straight to the point
September 21, 2011 at 03:04
Thank you for simple and useful explanation !!!
Hope to find more tutorials later !!! Good luck
September 26, 2011 at 06:16
hi,
I am fresher on android even in software field.
Thank you for simple and useful explanation !!!
September 27, 2011 at 08:22
thanx buddy…it is really helpful for the android beginner
October 13, 2011 at 05:07
Thank you very much for your tutorial!!
This tutorial ended my confusion for days!!
Keep up the good job =)
October 15, 2011 at 16:25
Awesome, thank you. Helped me understand Activities very fast.
October 17, 2011 at 01:03
good job~ thank
October 19, 2011 at 20:35
Thanks! I had no idea that I had to create a separate class file for each activity i the project I was working on. I tried doing everything in one class file and it couldn’t find the activity. I created a new class file and copied the class definition into it and it worked instantly. Thanks!
October 22, 2011 at 20:46
Thanks!
Just a comment, use the android:onClick to turn the code more clean.
http://developer.android.com/reference/android/widget/Button.html
October 26, 2011 at 13:01
hi thanks for your codes. I need for spinner items. How can I show the contact list in spinner ? It is very urgent.
pls help meeeeee.plsssssssssssss.
pls send the answer to my email.
thanks a lot.
October 26, 2011 at 20:03
Thank you very much for the tutorial! It’s simple and useful.
October 31, 2011 at 18:01
Still actual. Simple and easy to understand. Thanks for nice tutorial
November 2, 2011 at 06:26
senin ben da??a??n? yiyim!
November 4, 2011 at 16:53
It’s works and greate. Thanks you!
November 11, 2011 at 23:34
Thank you so much! really!
November 15, 2011 at 02:33
hi,I am trying to do a similar thing….but I keep getting an error on the code line setContentView(R.layout.itemwise); saying that main2 cannot be resolved and is not a field. itemwise is the layout of second activity of mine. Any ideas what might be causing this error to appear…Thanks folks
November 21, 2011 at 06:36
thanks for this nice tutorial.
thanks
November 22, 2011 at 02:31
Thank you for pointing out the note on avoiding the null pointer exception. I couldn’t figure out what was happening until I read through your tutorial.
November 30, 2011 at 20:40
Nice Article man!
December 19, 2011 at 11:04
Hi it is wonderful and helpful article. I have a question. The code is worked behind the button. Which event will be used to slide to the next activity.
December 19, 2011 at 12:17
Wants to move on next activity without pressing any button. just touch the screen and slide to next activity. I could not configure which event is performed this activity.
December 24, 2011 at 08:45
thanks
December 29, 2011 at 04:10
…hey…i have a question…what will i do if i want to enable a button on a different activity by checking a check box on the main activity…hope anyone can help me…thnx!!!
January 1, 2012 at 04:32
hi i did all procedure wat all u said to execute this but while running its giving a dialog box saying tat ” application force to close”
can u tell me y
thanku
January 3, 2012 at 05:26
Hi kamattian,
By passing your checkbox checked information to all activities, where your buttons are placed, can achieve button enable / disable functionality.
E.g.
Intent nextActivity = new Intent(this,nextActivity.class);
nextActivity .putExtra(”ischeck”, True); startActivityForResult(nextActivity , 0);
next Activity
Boolean blnCheckbox = (Boolean) getIntent().getSerializableExtra(”ischeck”).toString());
Based on value, can set your button visibility.
January 12, 2012 at 00:31
I’m developing a game on android. Game contains 4 players and each player has 4 pawns, each player will get a chance to throw the dice and pawns are moved according to the count indicated by dice. My problem is I need to keep track of all the pawns of each player and also i need to switch from one player to another. so, whether it is better to use activity or is it possible to handle it using if else condition??
January 19, 2012 at 08:44
Hi Boyan Tsolov,
i am very beginner in java android programming i needed
using intent in android (one page to another page) same like u have have broadcast Android: How to switch between Activities step by step taken screen shot so it is very useful to me i have followed one link (http://blogingtutorials.blogspot.com/2010/11/using-intent-in-android-one-page-to.html) but i am getting error in xml and java file may be some content missed please send me if possible thanks in advance
January 20, 2012 at 04:26
Hi all,
any one can help me please,……
January 24, 2012 at 16:43
This tutorial is a god send. Thank you ever so much. I had the “application stopped unexpectedly” message and scratched my head over why it didn’t worked when I checked the manifest which was correct. Also watch out for the setContentView where at the end bit you change it to the relevant .xml you called it!
January 25, 2012 at 11:37
Say I had two buttons on the home page, does this work on both of them?
February 4, 2012 at 12:04
Made my day =) Thanks a lot for this very useful example.
I adapted this to open another view not with a button but with an options menu entry:
(just scratchy code here)
//main activity.java
private View myViewForTheMainAction;
// override onCreateOptionesMenu(Menu menu) as described in the dev docs / guides here: http://developer.android.com/guide/topics/ui/menus.html
// Note: our menu-entry we added and want to switch the view in the menu.xml is called add_entry
// overwrite onOptionsItemSelected(MenuItem item) to access the menu item for switching:
@Overwrite
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.add_entry:
startActivity(new Intent(myViewForTheMainAction.getContext(), EditEntryActivity.class);
…
where ‘EditEntryActivity’ is the second Activity we switch to via options menu entry.
Maybe this is useful for someone else, too ;D
February 4, 2012 at 23:14
Thank you for this very helpful tutorial, greatly appreciated!