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
181
votes
3 answers

Preview layout with merge root tag in Intellij IDEA/Android Studio

Let's imagine we are developing compound component based on LinearLayout. So, we create class like this: public class SomeView extends LinearLayout { public SomeView(Context context, AttributeSet attrs) { super(context, attrs); …
180
votes
11 answers

How do I create a ListView with rounded corners in Android?

How do I create a ListView with rounded corners in Android?
Legend
  • 113,822
  • 119
  • 272
  • 400
179
votes
3 answers

Border in shape XML

I am trying to make a drawable to use for a button. I would like it to have this coloring, with a 2px border around it. Everything works just fine except I cannot get the border to show up...
Matt
  • 5,461
  • 7
  • 36
  • 43
179
votes
24 answers

Move layouts up when soft keyboard is shown?

I have a few elements in a RelativeView with the align bottom attribute set, when the soft keyboard comes up the elements are hidden by the soft keyboard. I would like them to move up so that if there is enough screen space they are shown above the…
Dinedal
  • 2,646
  • 2
  • 19
  • 18
178
votes
13 answers

RecyclerView expand/collapse items

I want to expand/collapse the items of my recyclerView in order to show more info. I want to achieve the same effect of the SlideExpandableListView. Basically in my viewHolder I have a view that is not visible and I want to do a smooth…
stanete
  • 4,062
  • 9
  • 21
  • 30
177
votes
14 answers

How to dismiss the dialog with click on outside of the dialog?

I have implemented a custom dialog for my application. I want to implement that when the user clicks outside the dialog, the dialog will be dismissed. What do I have to do for this?
Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
177
votes
3 answers

Why does LayoutInflater ignore the layout_width and layout_height layout parameters I've specified?

I've had severe trouble getting LayoutInflater to work as expected, and so did other people: How to use layoutinflator to add views at runtime?. Why does LayoutInflater ignore the layout parameters I've specified? E.g. why are the layout_width and…
andig
  • 13,378
  • 13
  • 61
  • 98
177
votes
2 answers

What is the 'app' Android XML namespace?

Here is an example of the app namespace that I've seen from a res/menu/main.xml file
175
votes
10 answers

What is android:weightSum in android, and how does it work?

I want to know: What is android:weightSum and layout weight, and how do they work?
Dhiral Pandya
  • 10,311
  • 4
  • 47
  • 47
175
votes
11 answers

Difference between onCreateView and onViewCreated in Fragment

What's the essential difference between these two methods? When I create a TextView, should I use one over the other for performance? Edit: What's the difference from onCreateView() { root = some view View v = new View(some context); …
Smith
  • 1,839
  • 2
  • 12
  • 6
174
votes
19 answers

Flinging with RecyclerView + AppBarLayout

I am using the new CoordinatorLayout with AppBarLayout and CollapsingToolbarLayout. Below AppBarLayout, I have a RecyclerView with a list of content. I have verified that fling scrolling works on the RecyclerView when I am scrolling up and down the…
172
votes
6 answers

Why are nested weights bad for performance? Alternatives?

I have written a couple layout files where I used the layout_weight attribute to create a ratio between different views. At some point, I start getting lint warnings about nested weights. So, I wonder why are nested weights bad for performance, and…
MobileCushion
  • 7,065
  • 7
  • 42
  • 62
171
votes
6 answers

How to customize a Spinner in Android

I want to add a custom height to the dropdown of a Spinner, say 30dp, and I want to hide the dividers of the dropdown list of Spinner. So far I tried to implement following style to the Spinner: