Questions tagged [android-layout]

A layout defines the visual structure for a user interface, such as the UI for an activity, fragment or app widget.

An Android layout defines everything the user can see and touch. A layout is made up of View (like buttons and text) and ViewGroup (like lists, tables, or more Views) objects, all combined to make a View Hierarchy:
View Hierarchy
(from Android's UI Overview)

Designing a Layout:

You can create your layout in any combination of these two ways:

  1. Declare UI elements in XML.
    Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts.

  2. Instantiate layout elements at runtime.
    Your application can create View and ViewGroup objects (and manipulate their properties) programmatically.

Common Layouts:

Each subclass of the ViewGroup class provides a unique way to display the views you nest within it. Below are some of the more common layout types that are built into the Android platform.

  1. Linear Layout: A layout that organizes its children into a single horizontal or vertical row. It creates a scrollbar if the length of the window exceeds the length of the screen.
  2. Relative Layout: Enables you to specify the location of child objects relative to each other (child A to the left of child B) or to the parent (aligned to the top of the parent).
  3. Constraint Layout: Allows positioning children relative to each other and the parent. But also offers other powerful positioning and sizing strategies, including horizontal/vertical child "chains" with custom spacing/weighting, arbitrary horizontal/vertical "guidelines," and custom child size aspect ratios.
  4. Web View: Displays web pages.
  5. Frame layout: FrameLayout is designed to block out an area on the screen to display a single item.
  6. Grid View: GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid.

Useful Links:

59114 questions
6334
votes
32 answers

What is the difference between px, dip, dp, and sp?

What is the difference between the units of measure px, dip, dp, and sp?
1470
votes
17 answers

What is the difference between match_parent and fill_parent?

I'm a little confused about two XML properties: match_parent and fill_parent. It seems that both are the same. Is there any difference between them?
vnshetty
  • 20,051
  • 23
  • 64
  • 102
1457
votes
22 answers

What is the difference between gravity and layout_gravity in Android?

I know we can set the following values to the android:gravity and android:layout_gravity properties: center center_vertical center_horizontal, etc. But I am confused regarding both of these. What is the difference between the usage of…
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
1214
votes
64 answers

You need to use a Theme.AppCompat theme (or descendant) with this activity

Android Studio 0.4.5 Android documentation for creating custom dialog boxes: http://developer.android.com/guide/topics/ui/dialogs.html If you want a custom dialog, you can instead display an Activity as a dialog instead of using the Dialog APIs.…
ant2009
  • 27,094
  • 154
  • 411
  • 609
1026
votes
12 answers

What's "tools:context" in Android layout files?

Starting with a recent new version of ADT, I've noticed this new attribute on the layout XML files, for example:
android developer
  • 114,585
  • 152
  • 739
  • 1,270
939
votes
38 answers

How to hide the title bar for an Activity in XML with existing custom theme

I want to hide the titlebar for some of my activities. The problem is that I applied a style to all my activities, therefore I can't simply set the theme to @android:style/Theme.NoTitleBar. Using the NoTitleBar theme as a parent for my style would…
Janusz
  • 187,060
  • 113
  • 301
  • 369
831
votes
39 answers

How to change fontFamily of TextView in Android

So I'd like to change the android:fontFamily in Android but I don't see any pre-defined fonts in Android. How do I select one of the pre-defined ones? I don't really need to define my own TypeFace but all I need is something different from what it…
Tarik
  • 79,711
  • 83
  • 236
  • 349
816
votes
23 answers

How to put a border around an Android TextView?

Is it possible to draw a border around an Android TextView?
yamspog
  • 18,173
  • 17
  • 63
  • 95
804
votes
29 answers

Can I underline text in an Android layout?

How can I define underlined text in an Android layout xml file?
Janusz
  • 187,060
  • 113
  • 301
  • 369
751
votes
20 answers

Standard Android Button with a different color

I'd like to change the color of a standard Android button slightly in order to better match a client's branding. The best way I've found to do this so far is to change the Button's drawable to the drawable located in…
emmby
  • 99,783
  • 65
  • 191
  • 249
690
votes
13 answers

What does android:layout_weight mean?

I don't understand how to use this attribute. Can anyone tell me more about it?
Mojiiz
  • 7,898
  • 6
  • 26
  • 25
674
votes
18 answers

How do I align views at the bottom of the screen?

Here's my layout code;
gav
  • 29,022
  • 23
  • 65
  • 90
626
votes
20 answers

Can the Android layout folder contain subfolders?

Right now, I'm storing every XML layout file inside the 'res/layout' folder, so it is feasible and simple to manage small projects, but when there is a case of large and heavy projects, then there should be a hierarchy and sub-folders needed inside…
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
625
votes
13 answers

Why fragments, and when to use fragments instead of activities?

In Android API 11+, Google has released a new class called Fragment. In the videos, Google suggests that whenever possible (link1, link2), we should use fragments instead of activities, but they didn't explain exactly why. What's the purpose of…
613
votes
28 answers

View's getWidth() and getHeight() returns 0

I am creating all of the elements in my android project dynamically. I am trying to get the width and height of a button so that I can rotate that button around. I am just trying to learn how to work with the android language. However, it returns…
ngreenwood6
  • 8,108
  • 11
  • 33
  • 52
1
2 3
99 100