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
199
votes
17 answers

Android - how to make a scrollable constraintlayout?

I want to make a layout that lets me scroll down using constraint layout, but I don't know how to go about it. Should the ScrollView be the parent of the ConstraintLayout like this?
197
votes
29 answers

Android How to adjust layout in Full Screen Mode when softkeyboard is visible

I have researched a lot to adjust the layout when softkeyboard is active and I have successfully implemented it but the problem comes when I use android:theme="@android:style/Theme.NoTitleBar.Fullscreen" this in my activity tag in manifest file. For…
Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
197
votes
8 answers

How to implement a ViewPager with different Fragments / Layouts

When I start an activity which implements viewpager, the viewpager created various fragments. I want to use different layouts for each fragment, but the problem is that viewpager shows only two layouts at the max (second layout on all of the…
195
votes
11 answers

Disable soft keyboard on NumberPicker

I'm trying to deactivate the soft keyboard when using a NumberPicker to enter numerical values (for aesthetic reasons). This is my layout-xml-code:
195
votes
8 answers

Set the absolute position of a view

Is it possible to set the absolute position of a view in Android? (I know that there is an AbsoluteLayout, but it's deprecated...) For example, if I have a 240x320px screen, how could I add an ImageView which is 20x20px such that its center is at…
Sephy
  • 50,022
  • 30
  • 123
  • 131
194
votes
4 answers

Why not use always android:configChanges="keyboardHidden|orientation"?

I was wondering why not use android:configChanges="keyboardHidden|orientation" in every (almost every ;)) activity? Goods: no need to worry about your activity been rotated it's faster Not so nice: need to change your layouts if they…
Mikooos
  • 5,360
  • 5
  • 31
  • 45
193
votes
7 answers

Wrap_content view inside a ConstraintLayout stretches outside the screen

I am trying to implement a simple chat bubble using a ConstraintLayout. This is what I am trying to achieve: However, wrap_content does not do what I want. It respects the margins, but expands outside of the view bounds. Here is my layout:
Marcin Jedynak
  • 3,697
  • 2
  • 20
  • 19
191
votes
5 answers

What are the differences between LinearLayout, RelativeLayout, and AbsoluteLayout?

I am confused about the difference between LinearLayout, RelativeLayout, and AbsoluteLayout. Could someone please tell me the exact differences between them?
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
188
votes
4 answers

Android layout replacing a view with another view on run time

I have a xml-layout file main with two textviews A/B and a view C. I have two other xml-layout files option1 and option2. Is it possible to load either option1 or option2 in run time via Java into C? If so, what function do I have to use?
Christian
  • 25,249
  • 40
  • 134
  • 225
185
votes
46 answers

Error inflating class fragment

I get the Error Unable to start activity ComponentInfo{de.androidbuch.activiti/de.androidbuch.activiti.task.Activity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment when I switch via the portrait and the…
tsync
  • 2,387
  • 4
  • 17
  • 15
185
votes
10 answers

Toolbar overlapping below status bar

I want to have appcompat v21 toolbar in my activity. But the toolbar I'm implementing is overlapping below status bar. How can I fix it? Here is the activity layout xml:
184
votes
15 answers

Nested Recycler view height doesn't wrap its content

I have an application that manage collections of books (like playlists). I want to display a list of collection with a vertical RecyclerView and inside each row, a list of book in an horizontal RecyclerView. When i set the layout_height of the inner…
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
182
votes
2 answers

Gridview with two columns and auto resized images

I'm trying to make a gridview with two columns. I mean two photos per row side by side just like this image. But my pictures have spaces between them, due to the fact that it's not the same size. Here is what I'm getting. as you can see the first…
rogcg
  • 10,451
  • 20
  • 91
  • 133