I wish I could go over details here but as I am a bit busy these days, I will just show you example of how to create a single activity from the scratch. So when you create a new Prject on Android Studio, we always find that somme xml files are auto generated + codes inside the main activity but Do we need all lines? What is the minimum requirements to create an activity?So here is the simple answer. Please go through Google android doc page to understand.
Step 1:
Create New Project suppose name is app1. Put the company name as buzz.com. As you enter finish. All files will be auto generated to launch a hello world app.
Step 2:
Delete everything inside MainActivity.java and write folowing:
Save the file.
Here launchOtherActivity method will be triggered when user will press the button. So yes, we have to create a button.
Step 3:
The Button has to be created inside the main activity xml file. Go to your layer folder (app1 -->app-->src-->main-->res --> layout). Delete all auto generated xml files here and create main.xml file and copy following code:
Here android:onClick="launchOtherActivity"/> will call the launchOtherActivity in the MainActivity.java.
Step 4:
Create another xml file for the other activity which is other.xml. Open the other.xml file and write following codes:
Step 5:
Now create OtherActivity.java in the same folder where MainActivity.java is. Copy the following lines to OtherActivity.java:
Step 6:
Now as we have a activity and it's related .xml file we have to add the activity to AndroidManifest.xml. Do not delete any line here but add the following:
so the AndroidManifest.xml will look like :
That's it. Now you will have a working OtherActivity. If you run the application. The output will be:
Step 1:
Create New Project suppose name is app1. Put the company name as buzz.com. As you enter finish. All files will be auto generated to launch a hello world app.
Step 2:
Delete everything inside MainActivity.java and write folowing:
Save the file.
Here launchOtherActivity method will be triggered when user will press the button. So yes, we have to create a button.
Step 3:
The Button has to be created inside the main activity xml file. Go to your layer folder (app1 -->app-->src-->main-->res --> layout). Delete all auto generated xml files here and create main.xml file and copy following code:
Here android:onClick="launchOtherActivity"/> will call the launchOtherActivity in the MainActivity.java.
Step 4:
Create another xml file for the other activity which is other.xml. Open the other.xml file and write following codes:
Step 5:
Now create OtherActivity.java in the same folder where MainActivity.java is. Copy the following lines to OtherActivity.java:
Step 6:
Now as we have a activity and it's related .xml file we have to add the activity to AndroidManifest.xml. Do not delete any line here but add the following:
so the AndroidManifest.xml will look like :
That's it. Now you will have a working OtherActivity. If you run the application. The output will be:
Post a Comment