39
Resources and RelativeLayouts

Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

Embed Size (px)

Citation preview

Page 1: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

Resources and RelativeLayouts

Page 2: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

Resources

Page 3: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

Android ResourcesWe’ve already talked about the different types of Android Resources

Directory Resource Typeanim/ XML files that define tween animations.color/ XML files that define a state list of colors.drawable/ Bitmap files (.png, .9.png, .jpg, .gif) or XML files that are compiled into

drawable resourceslayout/ XML files that define a user interface layout.menu/ XML files that define application menus, such as an Options Menu,

Context Menu, or Sub Menu.values/ XML files that contain simple values, such as strings, integers, and

colors.xml/ Arbitrary XML files that can be read at runtime.

Page 4: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

Using Resources in XML

• Three Important Pieces– @– resource_type – resource_name/id

• These Three pieces create a path to a resource– @string/app_name

Page 5: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

Using Resources in Code

• How to I access the string is Code?– @string/app_name???

• In code, you gain access to the resources via the R class

• For each type of resource, there is an R subclass

Page 6: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

R subclassespublic final class R { public static final class dimen { public static final int activity_horizontal_margin=0x7f040000; public static final int activity_vertical_margin=0x7f040001; } public static final class drawable { public static final int ic_launcher=0x7f020000; } public static final class id { public static final int action_settings=0x7f080006; public static final int canvas=0x7f080004; } public static final class layout { public static final int activity_main=0x7f030000; public static final int test=0x7f030001; } public static final class menu { public static final int main=0x7f070000; } public static final class string { public static final int action_settings=0x7f050001; public static final int hello_world=0x7f050002; public static final int red=0x7f050005; }}

Dimen subclass for all dimensions

Drawble subclass for all images

ID subclass for all ids specified in layout

Page 7: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

What does the static keyword mean?public final class R { public static final class dimen { public static final int activity_horizontal_margin=0x7f040000; public static final int activity_vertical_margin=0x7f040001; } public static final class drawable { public static final int ic_launcher=0x7f020000; } public static final class id { public static final int action_settings=0x7f080006; public static final int canvas=0x7f080004; } public static final class layout { public static final int activity_main=0x7f030000; public static final int test=0x7f030001; } public static final class menu { public static final int main=0x7f070000; } public static final class string { public static final int action_settings=0x7f050001; public static final int hello_world=0x7f050002; public static final int red=0x7f050005; }}

Page 8: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

Static Keyword

• Static variables are associated with the class, rather than with any object. Every instance of the class shares a class variable.

• Since static variables are associated with the class, we don’t need an instance of the object to access the static vars.

R r = new R(); Log.d("CRR", "App name = " + r.app_name);

Page 9: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

Static Keyword

• To access static variables all you need to know is the class name.

• If the static variable is public, you can easily get the variable.

Log.d("CRR", "App name = " + R.app_name);

Page 10: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

The R file only contains integerspublic final class R { public static final class dimen { public static final int activity_horizontal_margin=0x7f040000; public static final int activity_vertical_margin=0x7f040001; } public static final class drawable { public static final int ic_launcher=0x7f020000; } public static final class id { public static final int action_settings=0x7f080006; public static final int canvas=0x7f080004; } public static final class layout { public static final int activity_main=0x7f030000; public static final int test=0x7f030001; } public static final class menu { public static final int main=0x7f070000; } public static final class string { public static final int action_settings=0x7f050001; public static final int hello_world=0x7f050002; public static final int red=0x7f050005; }}

Page 11: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

The R file only contains integers

• The R class by itself won’t get you the resource you want.

• Instead you’ll have to – use methods which take resource ids to get the

resource you’re interested in.– Use the Resources Object to obtain the real

resource

Page 12: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

Common methods that take Resource Ids

1. findViewById()2. TextView.setText() – This method has two

signatures, one for a String and another for Resource Id

3. View.setBackgroundResource()

Page 13: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

Using the Resources Object

• The Activity provides a method getResources().

• getResources() returns an instance of the Resources class that is specific to your application. That means you only have access to your resources not resources in another application.

Page 14: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

Resources Class

• The Resources class provides getter methods to retrieve any type of resource available to your app.– String– String Array– Integer– Integer Array– Color– Drawable– Dimension– Animation– Etc.

Page 15: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

How to get a color value from resources

//getResources() is a method provided by the ActivityResources res = getResources();

//res.getColor(id) takes a resource identifier and returns a color or 0 if id not found

//We must use the R class to access the color subclass to access the identifier for the color blueint blueColor = res.getColor(R.color.blue);

Page 16: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

How to get a string value from resources

//getResources() is a method provided by the ActivityResources res = getResources();

//res.getString(id) takes a resource identifier and returns a string or throw an exception if the id not found

//We must use the R class to access the string subclass to access the identifier for the string app_nameString appName = res.getString(R.string.app_name);

Page 17: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

RelativeLayout

Page 18: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

About RelativeLayout

• RelativeLayout is a view group that displays child views in relative positions.

Page 19: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

About RelativeLayout• The position of each child view can be specified as relative to other sibling

elements (such as to the left-of or below another view) or in positions relative to the parent area (such as aligned to the bottom, left of center).

layout_alignParentTop

layout_belowlayout_alignParentLeft

layout_belowlayout_alignParentRight

Page 20: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

About RelativeLayout• By default, all child views are drawn at the top-left of the layout, so you

must define the position of each view using the various layout properties.

Page 21: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

RelativeLayout Layout Parameters

Page 22: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

About RelativeLayout

• Unless you specify a vertical position/alignment, a child view will match the top of its parent.

• Unless you specify a horizontal position/alignment, a child view will match the left of its parent.

Page 23: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

Can we simplify the layout params used?

layout_alignParentTop

layout_belowlayout_alignParentLeft

layout_belowlayout_alignParentRight

Page 24: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

Can we simplify the layout params used?

layout_below layout_belowlayout_alignParentRight

Page 25: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

How do we make the bottom views pushed down to the bottom of their parent?

layout_below layout_belowlayout_alignParentRight

Page 26: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

How do we make the bottom views pushed down to the bottom of their parent?

layout_alignParentBottom layout_alignParentBottomlayout_alignParentRight

Page 27: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

Using RelativeLayoutParams

• Some layout parameters take true or false as values and others take View ids.

Page 28: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

RelativeLayout Layout Params

• Layout params that use true or false– All layout parameters that have the word parent – layout_centerHorizontal– layout_centerVertical

Page 29: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

RelativeLayout Layout Params

• Layout params that use View ids– Need to reference an existing id or create one for

a view that will be defined later in the layout.

– Even if you aren’t planning on using an ID in code for finding a View. You still need it for RelativeLayout to layout views correctly.

Page 30: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

Using existing ids<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" >

<View android:id="@+id/top" android:layout_width="match_parent" android:layout_height="200dp" android:background="#F00" android:layout_marginBottom="5dp" /> <View android:id="@+id/left" android:layout_width="150dp" android:layout_height="75dp" android:background="#000" android:layout_below="@id/top" /> <View android:id="@+id/right" android:layout_width="100dp" android:layout_height="75dp" android:background="#000" android:layout_below="@id/top" android:layout_alignParentRight="true" /> </RelativeLayout>

Page 31: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

Creating an id for layout params<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" >

<View android:id="@+id/top" android:layout_width="match_parent" android:layout_height="200dp" android:background="#F00" android:layout_marginBottom="5dp" /> <View android:id="@+id/left" android:layout_width="150dp" android:layout_height="75dp" android:background="#000" android:layout_below="@id/top"

android:layout_toLeftOf="@+id/right" android:layout_alignParentLeft="true" android:layout_marginRight="5dp" /> <View

android:id="@id/right" android:layout_width="100dp" android:layout_height="75dp" android:background="#000" android:layout_below="@id/top" android:layout_alignParentRight="true" />

</RelativeLayout>

Page 32: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

Controlling Dimension of View with RelativeLayout params

• With the available options, you can not only position views in relationship to other views, but you can also size views.

Page 33: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

Controlling Size with Relative Layout<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" >

<View android:id="@+id/top" android:layout_width="match_parent" android:layout_height="200dp" android:background="#F00" android:layout_marginBottom="5dp" /> <View

android:id="@+id/left" android:layout_width="75dp" android:layout_height="75dp" android:background="#000" android:layout_below="@id/top" android:layout_alignParentLeft="true" android:layout_marginRight="5dp" /> <View

android:id="@+id/right" android:layout_width="100dp" android:layout_height="75dp" android:background="#000" android:layout_below="@id/top" android:layout_alignParentRight="true" />

</RelativeLayout>

How to make the View @id/left’s right edge extend to View @id/right?

Page 34: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

RelativeLayout Layout Parameters

Page 35: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

Controlling Size with RelativeLayout<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" >

<View android:id="@+id/top" android:layout_width="match_parent" android:layout_height="200dp" android:background="#F00" android:layout_marginBottom="5dp" /> <View android:id="@+id/left"

android:layout_width="75dp" android:layout_height="75dp" android:background="#000" android:layout_below="@id/top" android:layout_alignParentLeft="true"

android:layout_toLeftOf="@+id/right" android:layout_marginRight="5dp" /> <View

android:id="@id/right" android:layout_width="100dp" android:layout_height="75dp" android:background="#000" android:layout_below="@id/top" android:layout_alignParentRight="true" />

</RelativeLayout>

Page 36: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

How to make a new View’s left edge match @id/left and right’s edge match @id/right?

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" >

<View android:id="@+id/top" android:layout_width="match_parent" android:layout_height="200dp" android:background="#F00" android:layout_marginBottom="5dp" /> <View android:id="@+id/left" android:layout_width="75dp" android:layout_height="75dp" android:background="#000" android:layout_below="@id/top" android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/right" android:layout_marginRight="5dp" /> <View android:id="@id/right" android:layout_width="100dp" android:layout_height="75dp" android:background="#000" android:layout_below="@id/top" android:layout_alignParentRight="true" /> </RelativeLayout>

Page 37: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

How to make a new View’s left edge match @id/left and right’s edge match @id/right?

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" > <View android:id="@+id/top" android:layout_width="match_parent" android:layout_height="200dp" android:background="#F00" android:layout_marginBottom="5dp" /> <View android:id="@+id/left" android:layout_width="75dp" android:layout_height="75dp" android:background="#000" android:layout_below="@id/top" android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/right" android:layout_marginRight="5dp" /> <View android:id="@id/right" android:layout_width="100dp" android:layout_height="75dp" android:background="#000" android:layout_below="@id/top" android:layout_alignParentRight="true" /> <View android:layout_width="0dp" android:layout_height="75dp" android:layout_marginTop="5dp" android:background="#000" android:layout_below="@id/left"

android:layout_alignLeft="@id/left" android:layout_alignRight="@id/right" /> </RelativeLayout>

Page 38: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

The Power of RelativeLayouts

• With all the different layout parameters, one can see why RelativeLayout is super powerful.

Page 39: Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type

The Power of RelativeLayouts

• Many novice Android Devs will over use LinearLayouts by having several nested inside each other to accomplish a task that is done much easier with RelativeLayout.

• Good Android Devs will use the minimum numbers of Views required to accomplish a layout.