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
548
votes
8 answers

Android : Difference between View.GONE and View.INVISIBLE?

What is the difference between View.INVISIBLE and View.GONE for the View visibility status?
Rob
  • 15,732
  • 22
  • 69
  • 107
493
votes
12 answers

Difference between "@id/" and "@+id/" in Android

What is the diffirence between the @id/ and @+id/? In @+id/ the plus symbol + instructs to create a new resource name and add in to the R.java file but what about @id/? From the documentation of ID: when referencing an Android resource ID, you do…
Vikas Patidar
  • 42,865
  • 22
  • 93
  • 106
194
votes
8 answers

How can you get the Manifest Version number from the App's (Layout) XML variables?

I would like to have a way to reference the project's manifest version number in the main part of the code. What I have been doing up until now is to link the version number in a String XML file to the manifest (@string/Version). What I would like…
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
188
votes
6 answers

Custom attributes in styles.xml

I have created a custom widget, and I'm declaring it in layout.xml. I have also added some custom attributes in attr.xml. However, when trying to declare these attributes in a style in styles.xml, it's giving me No resource found that matches the…
Tyler
  • 19,113
  • 19
  • 94
  • 151
183
votes
17 answers

Vertical line using XML drawable

I'm trying to figure out how to define a vertical line (1dp thick) to be used as a drawable. To make a horizontal one, it's pretty straightforward:
Kaspa
  • 2,169
  • 3
  • 16
  • 18
176
votes
7 answers

How can I access a BuildConfig value in my AndroidManifest.xml file?

Is it possible to access a BuildConfig value from AndroidManifest.xml? In my build.gradle file, I have: defaultConfig { applicationId "com.compagny.product" minSdkVersion 16 targetSdkVersion 21 versionCode 1 versionName "1.0" …
anthony
  • 7,653
  • 8
  • 49
  • 101
175
votes
6 answers

Add a background image to shape in XML Android

How do you add a background image to a shape? The code I tried below but no success: //here is…
DevC
  • 6,982
  • 9
  • 45
  • 80
174
votes
3 answers

dpi value of default "large", "medium" and "small" text views android

Does the documentation ( or anyone) talks about the dpi values of the default Large TextView {android:textAppearance="?android:attr/textAppearanceLarge"} Medium TextView {android:textAppearance="?android:attr/textAppearanceMedium"} Small TextView …
Vinay W
  • 9,912
  • 8
  • 41
  • 47
158
votes
5 answers

Storing R.drawable IDs in XML array

I would like to store drawable resources' ID in the form of R.drawable.* inside an array using an XML values file, and then retrieve the array in my activity. Any ideas of how to achieve this?
gammaraptor
  • 1,886
  • 3
  • 14
  • 16
113
votes
4 answers

Is it possible to rotate a drawable in the xml description?

I am creating an app, with resources that can be reused (because buttons are always the same, but mirrored or rotated). I do want to use the same resource so I don't have to add 3 more resources that are exactly like the original but rotated. But I…
Goofyahead
  • 5,874
  • 6
  • 29
  • 33
105
votes
9 answers

How to get accent color programmatically?

How would one fetch the accent color set in styles, like below, programmatically? @color/material_green_500
Jakob Harteg
  • 9,587
  • 15
  • 56
  • 78
90
votes
16 answers

Unknown attribute android:layout_width, layout_height, id, gravity, layout_gravity, padding

Getting Unknown attribute error on all android tag. In layout XML, Auto suggestion is not showing all attributes (like layout_width, layout_height, orientation, orientation & all others android attributes.) Here is a Snap shot Things i have…
84
votes
11 answers

How to use specified weights for fonts in XML

Using the Fonts in XML feature you can specify various font weights for a font family. For example:
Bryan
  • 14,756
  • 10
  • 70
  • 125
84
votes
8 answers

Different resolution support android

Edited Question: Mobile Resolution: I would like to design different screen dpi like following resolutions. 320x480,480×800,540x960,720x1280 (Samsung S3),1080x1920 (S4, Nexus5,Nexus 5x, Moto G4),2560 x 1440 (Nexus 6, Nexus 6p, Samsung edge) Tablet…
81
votes
1 answer

How to concatenate multiple strings in android XML?

Problem I would like to be able to do something like that in Android XML code: @string/app_name test @string/app_name @string/version_name Regarding first line of code compiler shows this…
Andrzej Duś
  • 1,338
  • 1
  • 10
  • 16
1
2 3
99 100