34,333
edits
Changes
→Using View IDs
So far in this tutorial it has not been necessary to use view IDs. In order to demonstrate the use of IDs in Java code, the example will now be extended to add another view in the form of an EditText view that will be configured to be aligned 80 pixels above the existing button and centered horizontally. The following code modifications add the new view to the activity and then set IDs of 1 and 2 on the Button and EditText views respectively. Layout parameters are then created for the EditText view so that it is aligned with the button (note the call to getId() on the button to get the view ID) and centered horizontally within the layout. Finally, the margins property of the EditText field is configured to set the bottom margin to 80 pixels before adding the new view to parent layout:
<google>BUY_ANDROID</google>
<pre>
</pre>
A test run of the application should show the EditText field centered above the button with a margin of 80 pixels.
== Converting Density Independent Pixels (dp) to Pixels (px) ==