0% found this document useful (0 votes)
58 views11 pages

Android Layouts: Benard Osero

The document discusses Android layouts and provides an example of creating an Android app with three screens - a main screen with call and SMS buttons, a call screen with phone number input and dial button, and an SMS screen with phone number, message inputs and send button. The layouts for the three screens are defined in XML files (phone.xml, call_layout.xml, text.xml) and referenced in the Lab2Activity Java code to display the corresponding screens.

Uploaded by

Oira Alex
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views11 pages

Android Layouts: Benard Osero

The document discusses Android layouts and provides an example of creating an Android app with three screens - a main screen with call and SMS buttons, a call screen with phone number input and dial button, and an SMS screen with phone number, message inputs and send button. The layouts for the three screens are defined in XML files (phone.xml, call_layout.xml, text.xml) and referenced in the Lab2Activity Java code to display the corresponding screens.

Uploaded by

Oira Alex
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Android Layouts

Lecture 3 Benard Osero [email protected]

Android Layouts
Android layout is the architecture for the user interface in an Activity. It defines the layout structure and holds all the elements that appear to the user. You can declare your layout in two ways: Declare UI elements in XML. Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts. Instantiate layout elements at runtime. Your application can create View and ViewGroup objects (and manipulate their properties) programmatically.

Android Layout

The advantage to declaring your UI in XML is that it enables you to better separate the presentation of your application from the code that controls its behavior. Your UI descriptions are external to your application code, which means that you can modify or adapt it without having to modify your source code and recompile. For example, you can create XML layouts for different screen orientations, different device screen sizes, and different languages. Additionally, declaring the layout in XML makes it easier to visualize the structure of your UI, so it's easier to debug problems

#Class Exercise#
Create an android app that has main screen with two buttons: call and sms. When call button is clicked, it takes you to a screen with phone number label, phone number input and dial button. When sms button is clicked, it takes you to a screen with phone number label, phone input text, message label, message input text and send button.

Exercise guidelines
Create the three layouts XML files: phone.xml, call_layout.xml and text.xml You can use your own names as long as they are legal android layout names. phone.xml is for the main screen, call_layout.xml is for call screen and text.xml is for SMS screen.

phone.xml

<?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" > <Button android:id="@+id/call" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Call"/> <Button android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Text"/> </LinearLayout>

call_layout.xml

<?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Phone Number: " /> <EditText android:id="@+id/phone_no" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:editable="true" android:textColor="#ff0000" />

<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Message: " /> <EditText android:id="@+id/msg" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:editable="true" android:textColor="#ff0000" /> <Button android:id="@+id/sendsms" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send"/> </LinearLayout>

text.xml

<?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Phone Number: " /> <EditText android:id="@+id/phone_no" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:editable="true" android:textColor="#ff0000" /> <Button android:id="@+id/dial" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Dial"/> </LinearLayout>

Manifest file .. So far!


<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cisy.lab2" android:versionCode="1" android:versionName="1.0"> <application android:label="@string/app_name" > <activity android:name="Lab2Activity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

Lab2Activity.java
1.

package cisy.lab2;

2.
3. 4.

import android.app.Activity; import android.os.Bundle;

public class Lab2Activity extends Activity 5. { 6. /** Called when the activity is first created. */ 7. @Override 8. public void onCreate(Bundle savedInstanceState) 9. { 10. super.onCreate(savedInstanceState); 11. setContentView(R.layout.text); 12. } 13. } ### change line 11 with other layouts to test them

You might also like