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
218
votes
12 answers

How to make layout with View fill the remaining space?

I'm designing my application UI. I need a layout looks like this: (< and > are Buttons). The problem is, I don't know how to make sure the TextView will fill the remaining space, with two buttons have fixed size. If I use fill_parent for Text View,…
Luke Vo
  • 17,859
  • 21
  • 105
  • 181
217
votes
4 answers

Can I Set "android:layout_below" at Runtime Programmatically?

Is it possible when creating a RelativeLayout at runtime to set the equivalent of android:layout_below programmatically?
AlanH
  • 2,191
  • 2
  • 13
  • 6
215
votes
21 answers

String Resource new line /n not possible?

It doesn't seem like it's possible to add a new line /n to an XML resource string. Is there another way of doing this?
Andi Jay
  • 5,882
  • 13
  • 51
  • 61
214
votes
10 answers

ConstraintLayout: change constraints programmatically

I need help with ConstraintSet. My goal is to change view's constraints in code, but I cant figure out how to do this right. I have 4 TextViews and one ImageView. I need to set ImageView constraints to one of the TextViews. check_answer4 =…
Big Coach
  • 2,485
  • 2
  • 12
  • 31
214
votes
18 answers

How can I change default dialog button text color in android 5

I have many alert dialogs in my app. It is a default layout but I am adding positive and negative buttons to the dialog. So the buttons get the default text color of Android 5 (green). I tried to changed it without success. Any idea how to change…
213
votes
15 answers

Getting activity from context in android

This one has me stumped. I need to call an activity method from within a custom layout class. The problem with this is that I don't know how to access the activity from within the layout. ProfileView public class ProfileView extends LinearLayout { …
OVERTONE
  • 11,797
  • 20
  • 71
  • 87
211
votes
15 answers

Font size of TextView in Android application changes on changing font size from native settings

I want to specify my own text size in my application, but I am having a problem doing this. When I change the font size in the device settings, the font size of my application TextView also changes.
Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
209
votes
11 answers

How do I add a bullet symbol in TextView?

I have a TextView and I want to add a bullet symbol in my text through XML. Is it possible?
Pria
  • 2,743
  • 4
  • 27
  • 30
207
votes
6 answers

How to center the content inside a linear layout?

I'm trying to center an ImageView inside a LinearLayout horizontally and vertically, but I just can't do it. The main reason why I'm not using a RelativeLayout for that is because I need the layout_weight (my Activity consists of four columns that…
horta
  • 2,292
  • 2
  • 14
  • 16
207
votes
21 answers

Android Spinner : Avoid onItemSelected calls during initialization

I created an Android application with a Spinner and a TextView. I want to display the selected item from the Spinner's drop down list in the TextView. I implemented the Spinner in the onCreate method so when I'm running the program, it shows a value…
Grant
  • 4,413
  • 18
  • 56
  • 82
206
votes
9 answers

Setting background colour of Android layout element

I am trying to, somewhat clone the design of an activity from a set of slides on Android UI design. However I am having a problem with a very simple task. I have created the layout as shown in the image, and the header is a TextView in a…
Bjarke Freund-Hansen
  • 28,728
  • 25
  • 92
  • 135
204
votes
18 answers

How to adjust layout when soft keyboard appears

I would like to adjust/re-size the layout when the soft-keyboard activated, as below: Before and After: Found couple resources in SO: How to keep all fields and texts visible while the soft keyboard is shown android soft keyboard spoils layout…
Roy Lee
  • 10,572
  • 13
  • 60
  • 84
202
votes
8 answers

Android Paint: .measureText() vs .getTextBounds()

I'm measuring text using Paint.getTextBounds(), since I'm interested in getting both the height and width of the text to be rendered. However, the actual text rendered is always a bit wider than the .width() of the Rect information filled by…
salezica
  • 74,081
  • 25
  • 105
  • 166
200
votes
20 answers

How do I make WRAP_CONTENT work on a RecyclerView

I have a DialogFragment that contains a RecyclerView (a list of cards). Within this RecyclerView are one or more CardViews that can have any height. I want to give this DialogFragment the correct height based on the CardViews that are contained…
200
votes
5 answers

How to display count of notifications in app launcher icon

samsung galaxy note 2 android version 4.1.2 I know that this question was asked before and the reply was not possible How to display balloon counter over application launcher icon on android Nevertheless yesterday I updated the facebook app and…
Waqleh
  • 9,741
  • 8
  • 65
  • 103