This shows you the differences between two versions of the page.
osp:lectures:lecture-sdk [2016/10/05 11:53] laura.gheorghe |
osp:lectures:lecture-sdk [2016/11/06 18:44] (current) laura.gheorghe [Lecture] |
||
---|---|---|---|
Line 9: | Line 9: | ||
*{{:osp:lectures:lecture-sdk.pdf | Lecture Slides}} | *{{:osp:lectures:lecture-sdk.pdf | Lecture Slides}} | ||
+ | *{{:osp:lectures:2.sdk_notes.pdf | Lecture Notes}} | ||
{{url>http://ocw.cs.pub.ro/courses/_media/osp/lectures/lecture-sdk.pdf}} | {{url>http://ocw.cs.pub.ro/courses/_media/osp/lectures/lecture-sdk.pdf}} | ||
Line 69: | Line 70: | ||
setContentView(R.layout.activity_main); | setContentView(R.layout.activity_main); | ||
</code> | </code> | ||
- | This sets the layout for the Activity. R is a class generated by the SDK and it maps every resource to a number. It contains subclasses for every folder found in the **res** folder, in this case **layout**. | + | This sets the layout for the Activity. **R** is a class generated by the SDK and it maps every resource to a number. It contains subclasses for every folder found in the **res** folder, in this case **layout**. |
- | Open the res/layout/activity_main.xml file. You will see the layout editor. In the **Palette** pane, find the button, and drag it to the activity. You will see the **Outline** pane to the right of the screen change to reflect this. Select the button and check its id field in the **Properties** panel, under the list of UI elements. Change the text of the button to "Press me!". Also check the id field of the TextView element. | + | |
+ | Open the //res/layout/activity_main.xml// file. You will see the layout editor. In the **Palette** pane, find the button and drag it to the activity. You will see the **Outline** pane to the right of the screen change to reflect this. Select the button and check its ID field in the **Properties** panel, under the list of UI elements. Change the text of the button to "Press me!". Also check the ID field of the TextView element. | ||
Go back to MainActivity.java. | Go back to MainActivity.java. | ||
Line 76: | Line 78: | ||
Add the following code to the onCreate method. Make sure the ids match the ones in your layout. | Add the following code to the onCreate method. Make sure the ids match the ones in your layout. | ||
<code Java> | <code Java> | ||
- | final Button b1 = (Button) findViewById(R.id.button); /* make sure the ID matches */ | + | final Button b1 = (Button) findViewById(R.id.button); /* make sure the button ID matches */ |
- | final TextView tv1 = (TextView) findViewById(R.id.textView); /* make sure the ID matches */ | + | final TextView tv1 = (TextView) findViewById(R.id.textView); /* make sure the textView ID matches */ |
| | ||
b1.setOnClickListener(new OnClickListener() { | b1.setOnClickListener(new OnClickListener() { | ||
Line 88: | Line 90: | ||
}); | }); | ||
</code> | </code> | ||
- | This gets the references to both the Button you added and the TextView that was already declared. They are declared final so they can be visible inside the new OnClickListener. The OnClickListener objects contains an onClick method which gets executed when the button is pressed. In this case, it updates the TextView to reflect the number of clicks on the button. | + | |
+ | The **findViewById** calls are used to get the objects corresponding to the Button and the TextView that were already declared. These objects are declared as final variables in order for them to be visible inside the **OnClickListener** call (anonymous class). The **OnClickListener** object contains an onClick method which gets executed when the button is pressed. In this case, it updates the TextView to reflect the number of clicks on the button. | ||
=== Task 4 - Use logcat for debugging === | === Task 4 - Use logcat for debugging === | ||
- | Start the application installed in Task 2. Make sure the **Android Monitor** tab is expanded at the panel at the bottom and that **logcat** tab is selected inside this tab. Type any text in the box and then click one of the buttons. A new line containing your text should appear in the **Logcat**. Depending on what button you press, the line colour will vary. It is also possible to filter these log entries: above the logs themselves there is a text entry input which allows textual filtering. It allows you to limit scope by prefixing searches with pid:, tag:, app: or text: and it accepts Java-style regular expressions. To the left of this text field there is a dropdown which allows you to select verbosity. Choosing a level will only allow messages of that level or higher to be displayed. | + | Start the application installed in Task 2. Make sure the **Android Monitor** tab is expanded at the panel at the bottom and that **logcat** tab is selected inside this tab. Type any text in the box and then click one of the buttons. A new line containing your text should appear in the **Logcat**. Depending on what button you press, the line color will vary. It is also possible to filter these log entries: above the logs themselves there is a text entry input which allows textual filtering. It allows you to limit scope by prefixing searches with pid:, tag:, app: or text: and it accepts Java-style regular expressions. To the left of this text field there is a drop-down which allows you to select verbosity. Choosing a level will only allow messages of that level or higher to be displayed. |
Import the LogLevel project into Android Studio (**File > New > Import Project** and navigate to the project in the archive. Look over the code. Each button has a method attached which calls a static method from the Log class. These methods generate log entries on different levels of importance: d=debug, i=info, w=warning, e=error. | Import the LogLevel project into Android Studio (**File > New > Import Project** and navigate to the project in the archive. Look over the code. Each button has a method attached which calls a static method from the Log class. These methods generate log entries on different levels of importance: d=debug, i=info, w=warning, e=error. | ||
- | Add another two buttons to the application: one that generates an assert level log entry, and one that repeats the previously generated log entry, but with a higher level of importance. | + | Add another two buttons to the application: one that generates an assert level log entry and one that repeats the previously generated log entry, but with a higher level of importance (e.g. previously a debug entry was created, now the button click should generate an info entry). For the second button if the previous entry was an assert entry, then the next entry will also be an assert entry. |
=== Task 5 - View weight === | === Task 5 - View weight === |