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
255
votes
10 answers

What does ellipsize mean in android?

I've added an EditText to my layout, and added a hint, and made it centered horizontally. When running the application, the hint was invisible. I found that I should make ellipsize value of the TextView to be start:
Hamzeh Soboh
  • 7,572
  • 5
  • 43
  • 54
254
votes
25 answers

GridLayout (not GridView) how to stretch all children evenly

I want to have a 2x2 grid with a buttons inside. This is only ICS so I am trying to use the new GridLayout given. Here's the XML of my layout:
dnkoutso
  • 6,041
  • 4
  • 37
  • 58
253
votes
7 answers

How set the android:gravity to TextView from Java side in Android

I can use android:gravity="bottom|center_horizontal" in xml on a textview to get my desired results, but I need to do this programmatically. My textview is inside a tablerow if that matters in a relativelayout. I have tried: LayoutParams…
Nate
  • 2,720
  • 2
  • 16
  • 17
249
votes
5 answers

Android what does the clipToPadding Attribute do?

I would like to know what the clipToPadding attribute does for ViewGroup in Android ? I've been through the docs and some websites but none I have come across actually explain what it does and what it means, well none that I could actually…
Ersen Osman
  • 7,067
  • 8
  • 47
  • 80
247
votes
17 answers

How do I change the android actionbar title and icon

I'm trying to do some things on the ActionBar in Android. I've already added new items in the right side of the action bar. How can I change the left side of the action bar? I want to change the icon and the text, and I want to add a "Back Button"…
Allan Ramírez
  • 2,689
  • 2
  • 17
  • 15
246
votes
14 answers

Ripple effect on Android Lollipop CardView

I'm trying to get a CardView to display the ripple effect when touched by setting the android:backgound attribute in the activity XML file as described here on the Android Developers page, but it isn't working. No animation at all, but the method in…
242
votes
15 answers

How to inflate one view with a layout

I have a layout defined in XML. It contains also: I would like to inflate this RelativeView with other XML layout file. I…
Michal Dymel
  • 4,312
  • 3
  • 23
  • 32
238
votes
9 answers

How to lay out Views in RelativeLayout programmatically?

I'm trying to achieve the following programmatically (rather than declaratively via XML):
Dan
  • 2,389
  • 2
  • 14
  • 4
230
votes
19 answers

Set android shape color programmatically

I am editing to make the question simpler, hoping that helps towards an accurate answer. Say I have the following oval shape:
Cote Mounyo
  • 13,817
  • 23
  • 66
  • 87
227
votes
12 answers

How can you tell when a layout has been drawn?

I have a custom view that draws a scrollable bitmap to the screen. In order to initialize it, i need to pass in the size in pixels of the parent layout object. But during the onCreate and onResume functions, the Layout has not been drawn yet, and so…
Plastic Sturgeon
  • 12,527
  • 4
  • 33
  • 47
225
votes
5 answers

Evenly spacing views using ConstraintLayout

A common use for LinearLayout is to evenly space (weight) views, for example: How do you implement evenly spaced views like this using the new ConstraintLayout? ConstraintLayout links for reference: blog post, I/O session video
AdamK
  • 21,199
  • 5
  • 42
  • 58
224
votes
6 answers

Usage of forceLayout(), requestLayout() and invalidate()

I'm a bit confused about the roles of forceLayout(), requestLayout() and invalidate() methods of the View class. When shall they be called?
sdabet
  • 18,360
  • 11
  • 89
  • 158
220
votes
7 answers

Android: ScrollView vs NestedScrollView

What is the difference between ScrollView and NestedScrollView? Both of them, extend FrameLayout. I want to know in depth pros and cons of both of them.
219
votes
2 answers

Android: how to draw a border to a LinearLayout

I have three files. The XML, the draw function and the main Activity. I have some LinearLayout in my XML file.
SPG
  • 6,109
  • 14
  • 48
  • 79
218
votes
4 answers

How do I use a compound drawable instead of a LinearLayout that contains an ImageView and a TextView

Ran the new Lint tool against my code. It came up with a lot of good suggestions, but this one I cannot understand. This tag and its children can be replaced by one and a compound drawable Issue: Checks whether the current node can be replaced by…