Questions tagged [android-xml]

Android projects use XML in several ways: defining the project and components, building layouts, defining animations, creating menus and specifying resources (static and dynamic) for the project.

Introduction

In addition to Java code, Android projects (and their developers) have the ability to utilize XML to perform many standard tasks. Some XML usage is required, such as the definition of the projects and its components. Much of XML's usage is optional, making many common tasks easier. The Android XML schema is highly flexible and may be used in combination with code, exclusively, or not at all. Below is a list of common usage of XML:

  • Manifest - definition of the project
  • Layout - creation of partial or complete layouts for Activities, Dialogs, and Widgets
  • Colors - Constant color values used throughout the project
  • Style & Themes - application of custom standardized looks of Views
  • Animations - Standardized animations that may be applied to Views.
  • Drawables - Some specialized icons and graphics that may not be created wholely with an image editor. (StateDrawables, TranstionDrawables, Shapes and VectorGraphics)
  • Menu - A resource used to aid in standardized menus for an Activity
  • Integers, Strings, and Arrays - Constants used by the Application as resources.

AndroidManifest.xml

This file is a core component for every Android project. In order for nearly every component to work, it must be declared in the Manifest. The Manifest may affect everything from type of instancing to meta-data, even security restrictions. A comprehensive resource for the AndroidManifest.xml file exists at the Android Developers Dev Guide. Questions regarding the Android Manifest should use the android-manifest tag instead.

Android Layout XML

Android Layout may be partially or fully defined in XML to modularize presentation and limit code. The capabilities of Android Layout XML are quite extensive, allowing nearly every aspect of presentation to be managed in pure XML. Questions regarding layout should also use the android-layout tag.

Resources (Integers, Arrays, Strings, Colors, Styles, Themes, Menus)

One of the predominant uses of Android XML is to define resources that may be used by the entire application code base, similar to that of constants. Since so much of resource definition has been standardized throughout software development as a whole, XML provides a way to define these without having to manually generate much code that puts these to use. Before posting a question, it is best to make sure it is not covered by these documents:

Drawables and Animations

Android allows you to define special drawables or animations via XML. The ability to dynamically generate graphics and animation can significantly reduce code and further separate presentation from implementation. Questions regarding either of these should also utilize the animation tag or drawable tag respectively.

3013 questions
44
votes
7 answers

Android getMeasuredHeight returns wrong values !

I'm trying to determine the real dimension in pixels of some UI elements ! Those elements are inflated from a .xml file and are initialized with dip width and height so that the GUI will eventually support multiple screen size and dpi (as…
oberthelot
  • 1,310
  • 1
  • 11
  • 23
44
votes
2 answers

How do you create Preference Activity and Preference Fragment on Android?

As I was following an old tutorial (Créez des applications pour Android -> openclassroom) I got stuck on this deprecated method addPreferencesFromResource(int id) from the PreferenceActivity class. So my question is : What is the new way of…
40
votes
2 answers

How to add a horizontal 1px line above image view in a relative layout?

How do I add a horizontal 1px white line above image view in a relative layout?
dropsOfJupiter
  • 6,763
  • 12
  • 47
  • 59
40
votes
4 answers

Android button with different background colors

i want to change the background-color of a button using a selector-xml-file. My approach is basically the one from the example at the bottom this page: http://developer.android.com/guide/topics/resources/color-list-resource.html i have a…
Daniel
  • 539
  • 1
  • 6
  • 7
40
votes
4 answers

Programmatically change color of shape in layer list

How can I programmatically change the color (#000000) of a shape in a layer list? Here is my layer list:
39
votes
5 answers

Android scrollview hiding top content in layout

Im having a problem where the android scrollview starts hiding a pair of textviews I have at the top of my layout, I have found another person on this very site who had that problem and was able to get help unfortunately the person who helped them…
Edmund Rojas
  • 6,376
  • 16
  • 61
  • 92
39
votes
6 answers

re-setting a TextView height programmatically

I want to reset a textView height after I have added it to the main window in the xml file. inside a RelativeLayout,
Q8yDev
  • 395
  • 1
  • 3
  • 7
39
votes
6 answers

How to combine BottomAppBar + FAB with BottomNavigationView

I want to use the FloatingActionButton, along with its behaviour when anchored on a BottomAppBar, on top of a BottomNavigationView. I came up with a rather "hacky" trick to just place the BottomNavigationView on top of the BottomAppBar without…
39
votes
3 answers

Plural definition is ignored for zero quantity

I use plurals to compile a quantity string for an Android application. I follow exactly what one can find in the tutorials: res.getQuantityString( R.plurals.number_of_comments, commentsCount, commentsCount); Here is the definition of the…
JJD
  • 50,076
  • 60
  • 203
  • 339
38
votes
4 answers

Rendering issue in XML preview : Unable to locate mode 0

After updating the android studio exception occured during rendering. Exception raised during rendering: Unable to locate mode 0 java.lang.IllegalStateException: Unable to locate mode 0 at android.view.DisplayInfo.findMode(DisplayInfo.java:458) at…
Aman Jain
  • 2,975
  • 1
  • 20
  • 35
38
votes
3 answers

Android XML: android:elevation vs. app:elevation

When do I use android:elevation and when app:elevation? What's the difference between those two?
37
votes
1 answer

Eclipse Run button doesn't work when Android xml file is selected

I have Eclipse Juno SR1 and Android SDK Tools 21.1, SDK Platform-tools 16.0.2 (latest) on Mac. When I have some XML file selected and hit Run button, nothing happens. I have to select some Java File or project in the Package Explorer. Then it runs.…
David Vávra
  • 18,446
  • 7
  • 48
  • 56
36
votes
4 answers

How to align CheckBox to the top of its description in Android

I would like to align CheckBox "symbol" to the top of its description. What I have now: What I want to have: Current xml:
fragon
  • 3,391
  • 10
  • 39
  • 76
36
votes
6 answers

Heterogeneous GridLayout

I am trying to implement the layout below: I guess GridLayout is suitable for my needs but after 2 hours of struggle I couldn't create even a similar layout.. The layout is resizing itself wrongly, it exceeds the screen of the phone and it also…
C.d.
  • 9,932
  • 6
  • 41
  • 51
35
votes
3 answers

Using an array reference as an XML attribute for custom android view

This problem has been solved, see comments for details. I am extending an existing Android View and loading some custom attributes, as described in Declaring a custom android UI element using XML and Defining custom attrs. Attributes with boolean…