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:

57356 questions
6040
votes
34 answers

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

What is the difference between Android units of measure? px dip dp sp
1431
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
  • 19,471
  • 23
  • 60
  • 102
1395
votes
20 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
  • 122,920
  • 69
  • 234
  • 290
1094
votes
52 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
  • 30,351
  • 141
  • 365
  • 559
980
votes
9 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
  • 106,412
  • 122
  • 641
  • 1,128
912
votes
37 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
  • 176,216
  • 111
  • 293
  • 365
780
votes
37 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
  • 73,061
  • 78
  • 222
  • 327
761
votes
22 answers

How do I put a border around an Android textview?

Is it possible to draw a border around a textview?
yamspog
  • 17,114
  • 16
  • 59
  • 94
744
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
  • 95,927
  • 63
  • 178
  • 243
741
votes
26 answers

Can I underline text in an Android layout?

How can I define underlined text in an Android layout xml file?
Janusz
  • 176,216
  • 111
  • 293
  • 365
663
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,618
  • 6
  • 24
  • 25
656
votes
18 answers

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

Here's my layout code;
gav
  • 28,323
  • 22
  • 60
  • 89
593
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
  • 122,920
  • 69
  • 234
  • 290
560
votes
18 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 0.…
ngreenwood6
  • 7,536
  • 10
  • 29
  • 50
540
votes
11 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…
1
2 3
99 100