Differences

This shows you the differences between two versions of the page.

Link to this comparison view

dapm:laboratoare:02_2 [2021/03/28 11:56]
ioana_maria.culic
dapm:laboratoare:02_2 [2021/03/28 12:51] (current)
ioana_maria.culic [Exercices]
Line 30: Line 30:
 For each event, do not forget to call the parent function, e.g: ''​super.onCreate()''​. For each event, do not forget to call the parent function, e.g: ''​super.onCreate()''​.
 </​note>​ </​note>​
 +
 +====onCreate====
 +This event is called when the activity is created. Here is where we initialize the window components. To make the elements on the window visible, it is necessary to call the ''​setContentView''​ function. There are cases in which when starting the application,​ it must return to a previous state (e.g. after a forced stop). Thus, it is recommended that we check if the window is a new one or not, e.g. if the ''​Bundle''​ variable of passed as parameter is null or not. 
 +
 +<code java>
 +if (savedInstanceState! = null) {// window is not new and some remaining data must be loaded} ​
 +else {// window is new} 
 +</​code>​
 +
 +====onPause====
 +When this event is called, the window prepares to stop. That is why it is important to save all the changes we need. Here we will stop processes that are no longer needed when the application is not running, such as video or audio elements. ​
 +
 +=====Building the interface=====
 +One of the most important elements of an application is the graphical interface. In order to be able to differentiate the graphical interface from the writing of the application code, the two are in different files. Also, the interface design is done in another language, a descriptive one. It resembles the HTML language, being made up of bookmarks.
 +
 +<code XML>
 +<?xml version="​1.0"​ encoding="​utf-8"?>​
 +<​LinearLayout xmlns:​android="​http://​schemas.android.com/​apk/​res/​android"​
 +              android:​layout_width="​match_parent"​
 +              android:​layout_height="​match_parent"​
 +              android:​orientation="​vertical"​ >
 +    <​TextView android:​id="​@+id/​text"​
 +              android:​layout_width="​wrap_content"​
 +              android:​layout_height="​wrap_content"​
 +              android:​text="​Hello,​ I am a TextView"​ />
 +    <Button android:​id="​@+id/​button"​
 +            android:​layout_width="​wrap_content"​
 +            android:​layout_height="​wrap_content"​
 +            android:​text="​Hello,​ I am a Button"​ />
 +</​LinearLayout>​
 +
 +</​code>​
 +
 +There is a bookmark for each item on the window. The name of the bookmark is the same as the class name on the code side. The elements have certain characteristics:​ positioning,​ dimensions, color, text. To set each property we write a parameter in the following way: ''​android:​ property = "​value"​ ''​
 +
 +=====Interacting with the interface=====
 +The UI elements such as buttons and TextViews can also be modified from Java code, not just the XML file, because almost all XML properties have an equivalent function in JAVA. We use this option when we want an element of the window to change when calling a certain event. To do this, we need to create a link between the XML element and the code. First, in addition to the already set properties of the element, we will add another: ''​id. android: id = "@ + id / idElement"''​. After that, the element can be identified by id and we can link it to Java code: ''​Button button = findViewById (R.id.buton1);'' ​
 +
 +=====External resources=====
 +Inside any application,​ we will want to use different resources, such as images. All these elements that we want to add to the application must be saved in the ''​res''​ directory. They can be added to the UI from the XML code or from the Java gift. In XML there are certain elements that support these resources, such as ''​ImageView''​. These are displayed using the following assignment: ''​android:​ src = "@ drawable / picture_name"''​ From the Java code, they are added as follows: ​
 +<code java>
 +ImageView i=(ImageView) findViewById(R.id.img);​
 +i.setImageResource(R.drawable.image); ​
 +</​code>​
 +
 +=====Exercices=====
 +  - Create an application that displays 3 gray buttons, one next to the other, each having one centered text representing one color: Red, Green, Blue.
 +  - Under the 3 buttons place the text ''​Gray''​ and when one of the buttons is clicked, change the text to the color written on the pressed button.
 +  - Change the application behavior so when one of the buttons is pressed, it also changes the color according to the text written on it.
 +  - Create a new application that contains a form for a student that needs to register for an extra-curricular course. The student needs to insert the following information:​ Name, Birthdate, Faculty, Name of the course to enroll in. When a submit button is pressed, all the information will be displayed using a ''​Toast''​.
 +**BONUS**
 +  - Change the form so the student can choose the course from a ''​Spinner''​.
dapm/laboratoare/02_2.1616921760.txt.gz ยท Last modified: 2021/03/28 11:56 by ioana_maria.culic
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0