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
536
votes
58 answers

How do I get the currently displayed fragment?

I am playing with fragments in Android. I know I can change a fragment by using the following code: FragmentManager fragMgr = getSupportFragmentManager(); FragmentTransaction fragTrans = fragMgr.beginTransaction(); MyFragment myFragment = new…
Leem.fin
  • 40,781
  • 83
  • 202
  • 354
534
votes
14 answers

How to programmatically set drawableLeft on Android button?

I'm dynamically creating buttons. I styled them using XML first, and I'm trying to take the XML below and make it programattic.
490
votes
14 answers

Percentage width in a RelativeLayout

I am working on a form layout for a Login Activity in my Android App. The image below is how I want it to look like: I was able to achieve this layout with the following XML. The problem is, it's a bit hackish. I had to hard-code a width for the…
Sarson
  • 4,921
  • 3
  • 15
  • 5
435
votes
19 answers

android:drawableLeft margin and/or padding

Is it possible to set the margin or padding for the image which we added with the android:drawableLeft?
androidNewbies
  • 4,351
  • 2
  • 16
  • 3
429
votes
14 answers

How do I remove lines between ListViews on Android?

I'm using two ListViews like this:
deepthi
  • 8,477
  • 12
  • 35
  • 36
420
votes
41 answers

Android toolbar center title and custom font

I'm trying to figure out the right way to use a custom font for the toolbar title, and center it in the toolbar (client requirement). At the moment, i'm using the good old ActionBar, and I was setting the title to empty value, and using…
409
votes
5 answers

Navigation Drawer (Google+ vs. YouTube)

Does anyone know how to implement a sliding menu like some of the top apps of today? Other Stack Overflow questions haven't had any answers on how to do this, so I'm trying to gather as much info to help out others. All the applications I mention…
EGHDK
  • 17,818
  • 45
  • 129
  • 204
403
votes
20 answers

How to set layout_gravity programmatically?

My question is simple, How to set my buttons layout_gravity programmatically? I found this on internet, but it simply throws me a Nullpointer exception: Button MyButton = new Button(this); LinearLayout.LayoutParams …
Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222
400
votes
15 answers

How to build a horizontal ListView with RecyclerView

I need to implement a horizontal listview in my Android application. I did a bit of research and came across How can I make a horizontal ListView in Android? and Horizontal ListView in Android?. However, these questions were asked before…
Andre Perkins
  • 7,640
  • 6
  • 25
  • 39
398
votes
6 answers

What is the purpose of Android's tag in XML layouts?

I've read Romain Guy's post on the tag, but I still don't understand how it's useful. Is it a sort-of replacement of the tag, or is it used like so: . . …
cesar
  • 8,944
  • 12
  • 46
  • 59
390
votes
7 answers

LinearLayout not expanding inside a ScrollView

I have a LinearLayout inside a ScrollView that has android:layout_height="fill_parent", but it doesn't expand to the full height of the ScrollView. My layout looks something like: level layout layout_width layout_height 1 LinearLayout …
Felix
  • 88,392
  • 43
  • 149
  • 167
389
votes
19 answers

How to set space between listView Items in Android

I tried to use marginBottom on the listView to make space between listView Item, but still the items are attached together. Is it even possible? If yes, is there a specific way to do it? My code is…
Sammy
  • 4,538
  • 8
  • 33
  • 52
373
votes
11 answers

Calling setCompoundDrawables() doesn't display the Compound Drawable

After I call the setCompoundDrawables method, the compound Drawable is not shown.. Drawable myDrawable = getResources().getDrawable(R.drawable.btn); btn.setCompoundDrawables(myDrawable, null, null, null); Any thoughts?
hunterp
  • 15,716
  • 18
  • 63
  • 115
341
votes
51 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…
341
votes
26 answers

RecyclerView inside ScrollView is not working

I'm trying to implement a layout which contains RecyclerView and ScrollView at the same layout. Layout template: ... …