1 / 8

Modify Android Objects Using XML and Java

Modify Android Objects Using XML and Java. Android Layouts.

lavada
Download Presentation

Modify Android Objects Using XML and Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Modify Android ObjectsUsing XML and Java

  2. Android Layouts Most Android projects that have GUIs that are built using the Layout editor use one of the following Layouts: RelativeLayout, Linear Layout, and TableLayout. Each of these layouts have attributes or properites that help you design certain types of GUIs. The attributes can be seen in the XML file for the layout with the word “android:” in front of them. These attributes are the standard Android attributes that you can control from XML or from within Java methods.

  3. Android Layouts To modify an Android view component’s attributes from XML, you just open up the XML file and change it from within the XML tags. To modify the component’s attributes from Java code you must use the components ID value and then load it using findViewById() as shown below for a TextView and a Button: TextViewtxtViewName = (TextView) findViewById(R.id.txtViewName); Button btnName = (Button) findViewById(R.id.btnName); Then you can access or change an attribute like the background color of the object using the its accessor and mutator methods: txtViewName.setBackgroundColor(someColor); btnName.setBackgroundColor(someColor);

  4. Android Layouts RelativeLayout The RelativeLayout is the default layout when you are using the Layout editor, but it can be difficult to use because it tries to relate different components that are added to the layout to other components. The following are some of the key attributes used with the RelativeLayout: layout_alignLeft layout_alignRight layout_alignTop layout_alignBottom These attributes cause an view component to be lined up with another view component either on its left, right, top or bottom.

  5. Android Layouts LinearLayout In a LinearLayout components are arranged in a row horizontally or in a column vertically. If there is not enough space, the ones on the end (right or bottom) may not show at all. More complex layouts can be constructed by putting horizontal LinearLayout’s inside vertical a LinearLayout or vice versa. The following attributes for this layout help with aligning: orientation layout_marginLeft layout_marginRight layout_marginTop layout_marginBottom The first of these attributes (orientation) determines if the LinearLayout is vertical or horizontal, but the other determine the left, right, top or bottom boundaries of any of the view components.

  6. Android Layouts TableLayout The TableLayout holds components in rows, and when used with the TableRow tag, it organizes the components into columns that line up with each other over different rows. If you place an view component inside a aTableLayout but outside of a TableRow tag, it will fill an entire row by itself without any columns being created. The following attributes are use dfor determining how much space goes in between the columns and rows: layout_margin layout_padding The first of these attributes controls the space outside the border between components, and the second one controls the space inside the border.

  7. Android Layouts Other Important Attributes The following are other important attributes used in different types of layouts: id the id to use to load the component in Java code layout_width the width of a view component layout_height the height of a view component text the text that will appear within a view component background the background color of a view component textColor the text color of a view component gravity how to justify a view component: left, right, center textSize the text size of a view component hint the grayed out text hint inside an EditText component inputType the type of input allowed in an EditText component

  8. Android Layouts Other Important Attributes Note: match_parent (aka fill_parent which has been depricated since API 8) and wrap_content are two common values given for the layout_width and layout_height attributes, and they have the following meaning: match_parent means that the view component should only be as big in that dimension as its parent (minus padding) wrap_content means that the view component should be just big enough to enclose its content (plus padding)

More Related