Download presentation
Presentation is loading. Please wait.
Published byClaribel Osborne Modified over 9 years ago
1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 Modify Android Objects Using XML and Java
2
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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. Android Layouts
3
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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: TextView txtViewName = (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); Android Layouts
4
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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. Android Layouts
5
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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. Android Layouts
6
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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 a TableLayout 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. Android Layouts
7
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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_widththe width of a view component layout_heightthe height of a view component textthe text that will appear within a view component backgroundthe background color of a view component textColorthe text color of a view component gravityhow to justify a view component: left, right, center textSizethe text size of a view component hintthe grayed out text hint inside an EditText component inputTypethe type of input allowed in an EditText component Android Layouts
8
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 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) Android Layouts
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.