Activity and Layout xml
Last post we looked at AndroidManifest.xml here. We talked about activities and an entry point to it. Here we will look at that activity.
<activity android:name=".SendActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Here,
SendActivity
is the
MAIN
activity. A
SendActivity.java
must be created in the
"src"
directory of your code. Note that the file name and the activity name should match. A code snippet of it looks like this:
public class SendActivity extends Activity {
/* variables can be defined here */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Here we see that
SendActivity
is a class that inherits from the
Activity
. We also see that there is the
onCreate
function which is the entry function for the
SendActivity
class. Inside it, the view for application is mentioned:
setContentView(R.layout.main);
So the screen will display the contents of the
main.xml
file which will be found under the
layout
folder in the
res
folder.
The
main.xml
file looks like:
<?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"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/msgTextField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:hint="Enter Message"
/>
<Button
android:text="Send"
android:id="@+id/sendButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="send"
android:lines="1"
/>
</LinearLayout>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
/>
</LinearLayout>
Your app can have a
LinearLayout
or a
RelativeLayout
on which suits you. Here the screen will display an
EditText
field for the user to input data, a Send
Button
and a
WebView
below it. A
WebView
is used to display a webpage within the app. A screenshot on the emulator looks like this.
Here ends the short tutorial for beginners!