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

getSize() giving me errors

When I implement WindowManager wm = ((WindowManager)context.getSystemService(context.WINDOW_SERVICE)); Display display = wm.getDefaultDisplay(); m_nDisplayWidth = display.getWidth(); m_nDisplayHeight = display.getHeight(); I can run fine, but…
sayhaha
  • 769
  • 3
  • 12
  • 25
17
votes
6 answers

Error getting window size on android: "The method getWindowManager is undefined"

I need to get the window height and the weight. I have this in my code: DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); width1 = metrics.widthPixels; height1 = metrics.heightPixels; but I…
Harsan Valentin
  • 213
  • 1
  • 2
  • 7
17
votes
3 answers

Popup over incoming-call screen

I want to create a customized popup over the android's incoming call screen where I wish to add information for the user. I want the popup to not disable any clickability from the background so that the user could still answer the call. I know it is…
Guy
  • 5,370
  • 6
  • 25
  • 30
17
votes
5 answers

How to configure an Android emulator for the Kindle Fire?

I want to configure an emulator to be compatible with the Kindle Fire, including its resolution. What do I have to change in the emulator settings (or elsewhere)?
17
votes
3 answers

Patterns when to use Activity Transition vs Dynamic Fragments

Are there any patterns on how to handle UI Transitions in Android Activities vs Fragments? I am currently looking into a UI that has at most 3 columns in Landscape. I would like the UI to start with 1 column all the way across the screen and then on…
17
votes
2 answers

XML drawable Bitmap tileMode bug?

I'm having issues where my drawable resource uses the tileMode repeat. In some cases the image is just stretched and sometimes it is properly repeated. The following are the xml files I use to create the button states: Image drawable used for tile…
Jona
  • 13,325
  • 15
  • 86
  • 129
17
votes
5 answers

Android + ListFragment with a custom view hierarchy

I'm attempting to customize the fragment layout by returning my own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle). This inflates my custom view, but seems to stack the views instead of inflating within, I see everything at…
17
votes
8 answers

Android libraries not found

In my android project I am writing a program to connect to a server and below are the libraries that I need to use. But I get an error saying the following cannot be resolved. How to resolve this error? I am using eclipse JAVA EE on Windows. …
Rajeev
  • 44,985
  • 76
  • 186
  • 285
17
votes
5 answers

Text below RadioButtons

I need your help to solve my layout problem.. I have 4 RadioButtons, they appear as usual in this scheme: O text1   O text2   O text3   O text4 where O is the button.. I would know if it's possible to change this layout in this way:   O …
Erenwoid
  • 983
  • 6
  • 13
  • 25
17
votes
2 answers

Android View Binding - Clear Binding in Fragment Lifecycle

Why is it important to clear the binding in onDestroyView when implementing Android View Binding in a Fragment? As the documentation shows, Use view binding in fragments, and the Android Architecture Components sample, the binding is initiated in…
17
votes
2 answers

Setting Layout parameters in android

Working with the XML file was easy as I could specify the parameters as But I am confused while specifying it through code. For each view I specify the parameters using…
Jerrin George
  • 317
  • 1
  • 2
  • 13
17
votes
5 answers

Is it safe to pass LifeCycleOwner into RecyclerView.Adapter

Consider a layout data-bound to a viewModel. More specifically, the layout uses a layout variable for accessing this underlying viewModel. Whenever the binding is inflated, its viewModel and lifeCycleOwner are to be set. (Of course the viewModel…
lineage
  • 792
  • 1
  • 8
  • 20
17
votes
1 answer

How to get ViewSwitcher to wrap only the currently displayed view height?

I'm using a ViewSwitcher that lets me easily flip back and forth between two different List Headers with Animations built in. However, the problem is the two views are of greatly varying height and it appears that the ViewSwitcher with…
CodeFusionMobile
  • 14,812
  • 25
  • 102
  • 140
17
votes
0 answers

LinearLayout: showDividers="middle" not working on RTL layout direction

So I have this LinearLayout where I use dividers to add some spacing between the items:
gonçalo
  • 171
  • 1
  • 4
17
votes
6 answers

Get status bar height dynamically from XML

I need to optimize my Android app to look good on phones with a notch, like the Essential Phone. This phones have different status bar heights than the standard 25dp value, so you can't hardcode that value. The Android P developer preview released…
fergaral
  • 2,077
  • 6
  • 17
  • 34