Showing posts with label beginner. Show all posts
Showing posts with label beginner. Show all posts
Thursday, February 5, 2015
Android beginner tutorial Part 63 BroadcastReceiver
Today well learn about Broadcast Receivers.
A Broadcast Receiver is a component used for catching external events and reacting to them. Events can be dispatched by other applications, servies, as well as the system. In the previous tutorials, weve already covered handling events using Intents. The components that we used as examples can also react to anonymous messages between applications using sendBroadcast() method.
Using Broadcast Receivers, it is possible to listen to Intents of other applications, change your applications behaviour based on the data, react to changes in the system and events of other applications.
Just like all activities extend Activity class and all services extend Service class, all broadcast receivers extend BroadcastReceiver class.
The class has a single callback method - onReceive().
When a message arrives to the receiver, Android calls onReceive() method and passes the Intent message to it. The Broadcast Receiver component is only active during the execution of this method. The process thats currently executing the code in onReceive() is considered top priority and will be saved, unless theres a critical lack in memory in the system.
When the program returns from onReceive(), the Broadcast Receiver object becomes inactive and the system basically thinks "this BroadcastReceivers job is done" and will get rid of it if the memory taken by this object is needed for higher priority processes.
This sounds fair and all but it can actually cause a problem. For example, imagine if your onReceive() function creates a separate thread with a long process in it. While the process is still running, the onReceive() function will already be considered inactive by the system. If onReceive() is actually expecting a response from the thread, that response may never come since the BroadcastReceiver could get deleted by then.
But if theres a problem theres always a solution. Of course, everything depends on the situation, sometimes one solution might be more optimal than the other one.
One way to solve this would be to create a Service in the onReceive() function and let the Service do the work to keep the content of the process active during the whole operation process.
Thats all for today. Next time we will do something practical.
Thanks for reading!
Read more »
A Broadcast Receiver is a component used for catching external events and reacting to them. Events can be dispatched by other applications, servies, as well as the system. In the previous tutorials, weve already covered handling events using Intents. The components that we used as examples can also react to anonymous messages between applications using sendBroadcast() method.
Using Broadcast Receivers, it is possible to listen to Intents of other applications, change your applications behaviour based on the data, react to changes in the system and events of other applications.
Just like all activities extend Activity class and all services extend Service class, all broadcast receivers extend BroadcastReceiver class.
The class has a single callback method - onReceive().
When a message arrives to the receiver, Android calls onReceive() method and passes the Intent message to it. The Broadcast Receiver component is only active during the execution of this method. The process thats currently executing the code in onReceive() is considered top priority and will be saved, unless theres a critical lack in memory in the system.
When the program returns from onReceive(), the Broadcast Receiver object becomes inactive and the system basically thinks "this BroadcastReceivers job is done" and will get rid of it if the memory taken by this object is needed for higher priority processes.
This sounds fair and all but it can actually cause a problem. For example, imagine if your onReceive() function creates a separate thread with a long process in it. While the process is still running, the onReceive() function will already be considered inactive by the system. If onReceive() is actually expecting a response from the thread, that response may never come since the BroadcastReceiver could get deleted by then.
But if theres a problem theres always a solution. Of course, everything depends on the situation, sometimes one solution might be more optimal than the other one.
One way to solve this would be to create a Service in the onReceive() function and let the Service do the work to keep the content of the process active during the whole operation process.
Thats all for today. Next time we will do something practical.
Thanks for reading!
Wednesday, February 4, 2015
Android beginner tutorial Part 6 FrameLayout containers
Today well learn more about the FrameLayout layout type.
Open up our existing Android project in Eclispse and open the activity_main.xml file in res/layout/ directory in xml mode.
Right now we have a RelativeLayout container with a TextView inside. Rename the RelativeLayout to FrameLayout, and change the attributes to the values as in the example below. Instead of the TextView, add a Button, with the attribute values as shown below:
If you take a look at the application layout using the Graphical Layout mode now, youll see this:

Now Ill explain the code. The first two attributes in the root tags, prefixed with xmlns, are namespaces. They are included in the code by default and we should leave those as they are. The next two are layout_width and layout_height. The match_parent in layout_width basically tells the component to stretch in width to match the width of its parent, which is in this case the available device screen. The tools:context attribute is also added automatically, it specifies which Activity this layout is representing (based on this value, the UI editor will know which theme to use to display your content).
The height of the FrameLayout is set to wrap_content, which makes the component only take as much space as is needed.
In this case, the required height is defined by the height of the single child of the container, which is the button. The height and width of the button are both set to wrap_content. This means the button will stretch just enough to fit the text inside it, which in this case is "Test".
Note that in this example I am strictly applying the string value to the android:text attribute of the Button. It works in this example, but normally it is recommended to store all string values in a separate xml file and refer to those values using ids. This is done to be able to support different languages, and do other manipulations with string values.
So, the frame layout container contains a single button, which is spawned in the top left corner. Any new children will also be spawned there, and put on top of the previous children. Lets add one more button, set its width and height to match_parent, and also set the FrameLayouts height to match_parent:
Results:

The second button takes up all of the screen space. It is also spawned on top of the first button, making it impossible to use the first button.
FrameLayouts are not used very often, because the content is not flexible this way, and such layouts are mostly used to create overlays.
Thats all for today.
Well take a closer look at the LinearLayout container next time.
Thanks for reading!
Read more »
Open up our existing Android project in Eclispse and open the activity_main.xml file in res/layout/ directory in xml mode.
Right now we have a RelativeLayout container with a TextView inside. Rename the RelativeLayout to FrameLayout, and change the attributes to the values as in the example below. Instead of the TextView, add a Button, with the attribute values as shown below:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity" >
<Button
android:text="Test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
If you take a look at the application layout using the Graphical Layout mode now, youll see this:

Now Ill explain the code. The first two attributes in the root tags, prefixed with xmlns, are namespaces. They are included in the code by default and we should leave those as they are. The next two are layout_width and layout_height. The match_parent in layout_width basically tells the component to stretch in width to match the width of its parent, which is in this case the available device screen. The tools:context attribute is also added automatically, it specifies which Activity this layout is representing (based on this value, the UI editor will know which theme to use to display your content).
The height of the FrameLayout is set to wrap_content, which makes the component only take as much space as is needed.
In this case, the required height is defined by the height of the single child of the container, which is the button. The height and width of the button are both set to wrap_content. This means the button will stretch just enough to fit the text inside it, which in this case is "Test".
Note that in this example I am strictly applying the string value to the android:text attribute of the Button. It works in this example, but normally it is recommended to store all string values in a separate xml file and refer to those values using ids. This is done to be able to support different languages, and do other manipulations with string values.
So, the frame layout container contains a single button, which is spawned in the top left corner. Any new children will also be spawned there, and put on top of the previous children. Lets add one more button, set its width and height to match_parent, and also set the FrameLayouts height to match_parent:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:text="Test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Test 2" />
</FrameLayout>
Results:

The second button takes up all of the screen space. It is also spawned on top of the first button, making it impossible to use the first button.
FrameLayouts are not used very often, because the content is not flexible this way, and such layouts are mostly used to create overlays.
Thats all for today.
Well take a closer look at the LinearLayout container next time.
Thanks for reading!
Labels:
6,
android,
beginner,
containers,
framelayout,
part,
tutorial
Android beginner tutorial Part 84 Styles and Themes
Today we will learn about styles and themes in Android.
A Style is a collection of properties that set the appearance of a single View or window. The idea behind styles is similar to CSS - you define styles of an object somewhere outside of the layout code and then just apply the style to one or multiple elements.
A Theme is a style that is applied to an entire Activity or all Activities of an application.
The process for creating styles and themes is the same.
Create a new XML style in res/values/ directory, you can use whatever name you want.
Add this code inside:
As you can see, its a simple XML containing a style node which has 3 items. Each item represents an attribute and its value. The style node also has a "name" attribute and a "parent" one.
The name is the unique identificator for this style, this is the name that you will later use to refer to this style.
The parent is an optional vattribute that specifies the resource ID of another style from which this style should inherit properties. This way you can override the inherited style properties if needed. The resource can be of any type as long as it contains the needed style. It is recommended that you always inherit the default styles for the specific views - directly or indirectly.
After the style is created, you can use it by simply setting the style property of a View.
Note that the style attribute does not need an android: prefix.
If you apply a style to a ViewGroup container, the style will NOT be inherited by its children. To make it inheritable, set the style as a theme.
To set a theme to the whole application (all of its Activities), go to the AndroidManifest XML file and set the android:theme property of the application node:
If you want to set it to a single Activity, just do the same with the activity tag.
There are also a few pre-built themes that you can use. Theme.Dialog, for instance, displays an Activity like a dialog window:
If you want to create a new theme based on an existing one, you can add the base theme as the parent of the style.
Thats all for today.
Thanks for reading!
Read more »
A Style is a collection of properties that set the appearance of a single View or window. The idea behind styles is similar to CSS - you define styles of an object somewhere outside of the layout code and then just apply the style to one or multiple elements.
A Theme is a style that is applied to an entire Activity or all Activities of an application.
The process for creating styles and themes is the same.
Create a new XML style in res/values/ directory, you can use whatever name you want.
Add this code inside:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="SpecialText" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#FF0000</item>
</style>
</resources>
As you can see, its a simple XML containing a style node which has 3 items. Each item represents an attribute and its value. The style node also has a "name" attribute and a "parent" one.
The name is the unique identificator for this style, this is the name that you will later use to refer to this style.
The parent is an optional vattribute that specifies the resource ID of another style from which this style should inherit properties. This way you can override the inherited style properties if needed. The resource can be of any type as long as it contains the needed style. It is recommended that you always inherit the default styles for the specific views - directly or indirectly.
After the style is created, you can use it by simply setting the style property of a View.
<TextView
style="@style/SpecialText"
android:text="Special text!" />
Note that the style attribute does not need an android: prefix.
If you apply a style to a ViewGroup container, the style will NOT be inherited by its children. To make it inheritable, set the style as a theme.
To set a theme to the whole application (all of its Activities), go to the AndroidManifest XML file and set the android:theme property of the application node:
<application android:theme="@style/MyTheme">
If you want to set it to a single Activity, just do the same with the activity tag.
There are also a few pre-built themes that you can use. Theme.Dialog, for instance, displays an Activity like a dialog window:
<activity android:theme="@android:style/Theme.Dialog">
If you want to create a new theme based on an existing one, you can add the base theme as the parent of the style.
Thats all for today.
Thanks for reading!
Android beginner tutorial Part 9 RelativeLayout container
Today well learn about the fourth layout type - RelativeLayout.
This layout type is the most flexible, because it allows the developer to position the children of this container relatively to the container itself or relatively to its neighbour children.
In RelativeLayout, children are positioned in such a way, that if you align the first child to the center of the screen, the other children (which are aligned to this child) are also aligned to the center. Therefore, to properly display elements on the screen, you need to first declare the object, relative to which other objects will be positioned.
It is not necessary to add ids to all elements in your application, but defining them for children of a RelativeLayout is important, because this way we can refer to these elements to position other children relative to the said element.
An id value of an object is set this way:
The + symbol is used only when declaring the id for the first time. When you later refer to this object, simply use @id/myElement.
To create a RelativeLayout simply rename the TableLayout from the previous tutorial to RelativeLayout. All the attributes remain the same:
Now lets add a button and lets center it. Well specify its id, set it to btn_center. To align it to the center of the parent, set its layout_centerInParent value to true.
You can type "android:" and then pause for a second if youre using Eclipse IDE, then after a moment youll see a drop down tip menu which displays all the possible attribute names and their description. This is a very useful feature, because it basically shows you the documentation quickly and when you need it. You can then add "l" or "layout" to narrow the menu down to the layout attributes.
Anyway, this is the button weve created:
Now lets add another button, which will be positioned relative to our first button. Set its toRightOf and alignTop values to @id/btn_center:
Full code:
The results:

You can see that the Center button is centered, and the second button is aligned to its right edge.
You can play around with all the possible attributes. Here is another example:
The results:

The RelativeLayout container is not used very often, because the layout looks different on each screen size and if you use it, you should always test your application on many different screen sizes, since problems such as element overlapping might arise.
Thats all for today.
Thanks for reading!
Read more »
This layout type is the most flexible, because it allows the developer to position the children of this container relatively to the container itself or relatively to its neighbour children.
In RelativeLayout, children are positioned in such a way, that if you align the first child to the center of the screen, the other children (which are aligned to this child) are also aligned to the center. Therefore, to properly display elements on the screen, you need to first declare the object, relative to which other objects will be positioned.
It is not necessary to add ids to all elements in your application, but defining them for children of a RelativeLayout is important, because this way we can refer to these elements to position other children relative to the said element.
An id value of an object is set this way:
android:id="@+id/myElement"
The + symbol is used only when declaring the id for the first time. When you later refer to this object, simply use @id/myElement.
To create a RelativeLayout simply rename the TableLayout from the previous tutorial to RelativeLayout. All the attributes remain the same:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
Now lets add a button and lets center it. Well specify its id, set it to btn_center. To align it to the center of the parent, set its layout_centerInParent value to true.
You can type "android:" and then pause for a second if youre using Eclipse IDE, then after a moment youll see a drop down tip menu which displays all the possible attribute names and their description. This is a very useful feature, because it basically shows you the documentation quickly and when you need it. You can then add "l" or "layout" to narrow the menu down to the layout attributes.
Anyway, this is the button weve created:
<Button
android:text="Center"
android:id="@+id/btn_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
Now lets add another button, which will be positioned relative to our first button. Set its toRightOf and alignTop values to @id/btn_center:
<Button
android:text="Rel right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btn_center"
android:layout_alignTop="@id/btn_center"/>
Full code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:text="Center"
android:id="@+id/btn_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<Button
android:text="Rel right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btn_center"
android:layout_alignTop="@id/btn_center"/>
</RelativeLayout>
The results:

You can see that the Center button is centered, and the second button is aligned to its right edge.
You can play around with all the possible attributes. Here is another example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Center" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/btn_center"
android:layout_toRightOf="@id/btn_center"
android:text="Rel right" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/btn_center"
android:layout_toLeftOf="@id/btn_center"
android:text="Rel left" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_center"
android:layout_centerHorizontal="true"
android:text="Rel bottom" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_center"
android:layout_centerHorizontal="true"
android:text="Rel top" />
</RelativeLayout>
The results:

The RelativeLayout container is not used very often, because the layout looks different on each screen size and if you use it, you should always test your application on many different screen sizes, since problems such as element overlapping might arise.
Thats all for today.
Thanks for reading!
Tuesday, February 3, 2015
Android beginner tutorial Part 26 AnalogClock and DigitalClock widgets
In this tutorial we will learn about the AnalogClock and DigitalClock widgets.
There are 3 classes related to time in Android SDK - AnalogClock, DigitalClock and Chronometer. They are all subclasses of TextView.
Today well cover the first two widgets. They are very simple to implement and require no java code to implement - just add them in the layout XML and they will work.
Both of these widgets just display time. No other interaction can be performed by default.
Go to activity_main.xml and create a simple layout. Lets add both of the clocks to our application.
The AnalogClock instance:
And the DigitalClock one:
Full code:
As you can see, its very easy.
As I already said, both of these classes subclass TextView. This means that all the properties and methods of TextView can be used with these classes. In this example, I set the text size using the android:textSize attribute of the DigitalClock object.
If you run the application, it looks like this:

Thanks for reading!
Read more »
There are 3 classes related to time in Android SDK - AnalogClock, DigitalClock and Chronometer. They are all subclasses of TextView.
Today well cover the first two widgets. They are very simple to implement and require no java code to implement - just add them in the layout XML and they will work.
Both of these widgets just display time. No other interaction can be performed by default.
Go to activity_main.xml and create a simple layout. Lets add both of the clocks to our application.
The AnalogClock instance:
<AnalogClock android:id="@+id/analogClock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
And the DigitalClock one:
<DigitalClock android:id="@+id/digitalClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="48sp"
/>
Full code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity" >
<AnalogClock android:id="@+id/analogClock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<DigitalClock android:id="@+id/digitalClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="48sp"
/>
</LinearLayout>
As you can see, its very easy.
As I already said, both of these classes subclass TextView. This means that all the properties and methods of TextView can be used with these classes. In this example, I set the text size using the android:textSize attribute of the DigitalClock object.
If you run the application, it looks like this:

Thanks for reading!
Labels:
26,
analogclock,
and,
android,
beginner,
digitalclock,
part,
tutorial,
widgets
Subscribe to:
Posts (Atom)