Escolar Documentos
Profissional Documentos
Cultura Documentos
Task 1: Layout
This practical shows the popular layouts in Android:
1. Create a new Android project.
2. Insert the following strings into the string.xml file.
ID
to
subject
message
send
Value
To
Subject
Message
Send
Page 1 of 10
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="@string/message" />
Page 2 of 10
android:layout_below="@+id/editTextSubject"
android:layout_alignParentLeft="true"/>
<Button
android:id="@+id/buttonSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send"
android:layout_below="@+id/editTextMessage"
android:layout_alignParentRight="true"/>
</RelativeLayout>
android:layout_centerVertical
android:layout_below
android:layout_toRightOf
Beginning with Android 3.0 (API level 11), the action bar is included in all activities that use
the Theme.Holo theme (or one of its descendants), which is the default theme when either
the targetSdkVersion or minSdkVersion attribute is set to "11" or greater.
So to add the action bar to your activities, simply set either attribute to 11 or higher. For example:
The action bar allows you to add buttons for the most important action items relating to the app's
current context. Those that appear directly in the action bar with an icon and/or text are known
Page 3 of 10
as action buttons. Actions that can't fit in the action bar or aren't important enough are hidden in the
action overflow.
Figure 2. An action bar with an action button for Search and the action overflow, which reveals
additional actions.
<string name="action_search">Search</string>
2. All action buttons and other items available in the action overflow are defined in an XML menu
resource. To add actions to the action bar, create a new XML file in your
project's res/menu/ directory.
Add an <item> element for each item you want to include in the action bar. For example:
res/menu/main_activity_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
app:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu>
This declares that the Search action should appear as an action button when room is available in the
action bar, but the Settings action should always appear in the overflow. (By default, all actions appear
in the overflow, but it's good practice to explicitly declare your design intentions for each action.)
The icon attribute requires a resource ID for an image. The name that follows @drawable/ must be
the name of a bitmap image you've saved in your project's res/drawable/ directory. For
example,"@drawable/ic_action_search" refers to ic_action_search.png.
Page 4 of 10
To place the menu items into the action bar, implement the onCreateOptionsMenu() callback
method in your activity to inflate the menu resource into the given Menu object. For example:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
MenuInflater(Context context) constructor - This class is used to instantiate menu XML files
into Menu objects.
Available under the Java.lang.Object
Public methods - inflate(XML file, Menu)
When the user presses one of the action buttons or another item in the action overflow, the system
calls your activity's onOptionsItemSelected() callback method. In your implementation of this
method, call getItemId()on the given MenuItem to determine which item was pressedthe
returned ID matches the value you declared in the
corresponding <item> element's android:id attribute.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Page 5 of 10
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displaymessage);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If your minSdkVersion is 11 or higher, instead use:
Page 6 of 10
// getActionBar().setDisplayHomeAsUpEnabled(true);
}
Visit:https://coolors.co/app/65adf5-a08ff3-d8cff4-cbabbd-ac8985
Task 7:
Creating
your
own
Icons
1. To create a set of icons for your app, right click on Project Explorer New Image Asset.
2. Click Launcher Icons. Select Image as foreground and find an image file you would like to set
as launcher icon.
Page 7 of 10
Page 8 of 10
Try Android Asset Studio, an online tool that you can create icons and other graphics easily
https://romannurik.github.io/AndroidAssetStudio/index.html
Page 9 of 10
Exercises
Question 1
Create the following layouts:
Hint: You may use nested layout, setting the android:maxHeight or android:minHeight to
achieve the above result.
Question 2
Create a layout with the following action bar:
Create an Activity for each Action Button. Write code to handle the click event for all the
Action Buttons.
(Note: Generate the icon set from Android Asset Studio)
Page 10 of 10