This article is going to be a basic application like "Hello World!". But Before we'll be on welcome page and see the welcome message on it. Let's see how to do it!
Necessary equipment (Install all of them)
- Eclipse Editor
- SDK (Software Development Kid)
- Android: Windows, Linux, Mac
First, open the Eclipse editor, then follow this directory: File/New/Project/Android/Android Project
My Project Name : WelcomePage My Android Version : V2.2 My Package Name : welcome.page
You can fix this properties as you wish.
I think, you know how to do an "hello world" app on Android, so I won't explain this. The important thing of the app for me is relationship of Activities and Threads. For those, we're going to do this pages:
src/WelcomePageActivity.class //This is a default file src/WelcomePage.class res/layout/main.xml //This is also a default file res/layout/page2.xml AndroidManifest.xml //This is configuration file
Create a class file:
- directory: src/(Right Click)New/Class
- call: WelcomePage
- Click the "Finish" Button
Code for WelcomePage.java:
The "R" is a library which used for xml files. Every xml file can used on it. We'd just used main.xml file for we need to go home page of our Android app after showing welcome page.
Create a xml file:
package welcome.page; import android.app.Activity; import android.os.Bundle; public class WelcomePage extends Activity { //Created WelcomePage Activity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //The content to display from main.xml } }
The "R" is a library which used for xml files. Every xml file can used on it. We'd just used main.xml file for we need to go home page of our Android app after showing welcome page.
Create a xml file:
- directory: res/layout/(Right Click)Other/File/Android/Android Xml File
- call: page2
- Click the "Finish" Button
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is a welcome page!" /> </LinearLayout>
When we look at this page up, should see like that:
page2.xml file view |
package welcome.page; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class WelcomePageActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.page2); //The content to display from page2.xml file Thread WelcomeScreen = new Thread() { //Must create a thread, because opening another page is a different event, as sync public void run() { try { //This is try-catch sleep(5000); //Waiting five seconds startActivity(new Intent(getApplicationContext(),WelcomePage.class)); //After waiting, shown the content from WelcomePage.java file } catch (InterruptedException e) { e.printStackTrace(); } finally { finish(); //We finish this thread. } } }; WelcomeScreen.start(); //Starting the thread created. } }
Maybe, you may not get the line,
"startActivity(new Intent(getApplicationContext(),WelcomePage.class));".
Just remember that we used main.xml content to get on WelcomePage.java page.
And the last one we have to do, update AndroidManifest.xml file! Remember that we create a new Activity, so must define this Activity. For that reason, just add this code to AndroidManifest.xml file:
And yes, finished! Let's work this on our Emulator! For this, call:
WelcomePage Project(Right Click)/Run as/Android Project
Welcome page view |
main.xml file on my application |
Would you have an example like this, using Fragments as pages instead of Activities?
ReplyDeleteHi, where should I put this Activity line in the AndroidManifest.xml file?
ReplyDelete