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
306
votes
45 answers

RecyclerView: Inconsistency detected. Invalid item position

Our QA has detected a bug: when rotating the Android device (Droid Turbo), the following RecyclerView-related crash happened: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 2(offset:2).state:3 To me, it…
KarolDepka
  • 7,188
  • 9
  • 37
  • 55
302
votes
9 answers

how to change color of textview hyperlink?

I am using this code for hyperlink: By default it is…
super
  • 3,869
  • 4
  • 20
  • 20
277
votes
11 answers

How to animate RecyclerView items when they appear

How can I animate the RecyclerView Items when there are appearing? The default item animator only animates when a data is added or removed after the recycler data has been set. I'm new developing applications and don't have any clue where to…
PaulNunezM
  • 2,957
  • 3
  • 12
  • 10
277
votes
33 answers

Android list view inside a scroll view

I have an android layout which has a scrollView with a number of elements with in it. At the bottom of the scrollView I have a listView which is then populated by an adapter. The problem that I am experiencing, is that android is excluding the…
Zapnologica
  • 20,003
  • 39
  • 136
  • 229
276
votes
25 answers

How to change the color of a CheckBox?

How do I change the default CheckBox color in Android? By default the CheckBox color is green, and I want to change this color. If it is not possible please tell me how to make a custom CheckBox?
Piyush
  • 5,047
  • 4
  • 25
  • 27
276
votes
9 answers

How to set layout_weight attribute dynamically from code?

How can I set the value for the attribute layout_weight for button in android dynamically from java code ?
Adham
  • 58,520
  • 96
  • 215
  • 339
272
votes
25 answers

Android: How to create a Dialog without a title?

I'm trying to generate a custom dialog in Android. I create my Dialog like this: dialog = new Dialog(this); dialog.setContentView(R.layout.my_dialog); Everythings works fine except for the title of the Dialog. Even if I don't set the title of the…
Janusz
  • 176,216
  • 111
  • 293
  • 365
268
votes
10 answers

Android LinearLayout Gradient Background

I am having trouble applying a gradient background to a LinearLayout. This should be relatively simple from what I have read but it just doesn't seem to work. For reference sakes I am developing on 2.1-update1. header_bg.xml:
Genesis
  • 7,478
  • 3
  • 18
  • 22
262
votes
12 answers

Add padding on view programmatically

I am developing Android v2.2 app. I have a Fragment. In the onCreateView(...) callback of my fragment class, I inflate an layout to the fragment like below: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle…
Leem.fin
  • 35,699
  • 70
  • 166
  • 297
250
votes
7 answers

Avoid passing null as the view root (need to resolve layout parameters on the inflated layout's root element)

Passing null for root studio gives me this warning: Avoid passing null as the view root (need to resolve layout parameters on the inflated layout's root element) It is showing a null value in getGroupView. Please help. public class…
hash
  • 4,936
  • 7
  • 31
  • 50
249
votes
14 answers

Android: set view style programmatically

Here's XML:
Jim
  • 7,796
  • 16
  • 63
  • 118
245
votes
10 answers

Differences between ConstraintLayout and RelativeLayout

I am confused about the difference between ConstraintLayout and RelativeLayout. Could someone please tell me the exact differences between them?
243
votes
11 answers

Placing/Overlapping(z-index) a view above another view in android

I have a linear layout which consists of imageview and textview , one below another in a linear layout.
sat
  • 36,230
  • 27
  • 86
  • 101
239
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,580
  • 2
  • 14
  • 17
238
votes
8 answers

How to center the elements in ConstraintLayout

I am using ConstraintLayout in my application to make applications layout. I am trying to a create a screen wheren one EditText and Button should be in center and Button should be below of EditText with a marginTop only 16dp. Here is my layout and…
N Sharma
  • 28,073
  • 81
  • 228
  • 405