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
16
votes
3 answers

How to set both min and max size for an ImageView in Android

I would like so specify both min. width/height and max. width/height for an ImageView. The image should be scaled to fit. Doing this:
muscardinus
  • 679
  • 1
  • 6
  • 11
16
votes
2 answers

Fragment: which callback invoked when press back button & customize it

I have a fragment: public class MyFragment extends Fragment{ ... @Override public View onCreateView(...){...} ... } I instantiate it: MyFragment myFragment = new MyFragment(); I use the above fragment to replace the current…
16
votes
5 answers

Animation in changing LayoutParams in LinearLayout

In my app there is a LinearLayout which has 0 Layout height. When I click the button this layout height should be LayoutParams.WRAP_CONTENT. This is the code I use in onclicklistner. LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,…
Chrishan
  • 4,076
  • 7
  • 48
  • 67
16
votes
11 answers

Project has no project.properties file

I am developing Android app in Eclipse. Currently, eclipse complains: "Project has no project.properties file! Edit the project properties to set one." But I do have project.properties file under my project root folder. Why it complains? This…
Leem.fin
  • 40,781
  • 83
  • 202
  • 354
16
votes
4 answers

difference between @id and @android:id

denniss
  • 17,229
  • 26
  • 92
  • 141
16
votes
2 answers

What is "context menu" & method registerForContextMenu()

In Activity class, there is method called registerForContextMenu(View view). The android document explains that this method is used to registers a context menu to be shown for the given view (multiple views can show the context menu). What does…
Leem.fin
  • 40,781
  • 83
  • 202
  • 354
16
votes
1 answer

Android: Custom Spinner Layout

I am trying to make a fully custom spinner. I am running into difficulties with making the layout that pops up when you press on it. Here is my code for my adapter: ArrayAdapter adapter = ArrayAdapter.createFromResource( …
Flynn
  • 5,903
  • 7
  • 38
  • 55
16
votes
2 answers

Android: How do I set the textsize for a layout?

I am trying to set the textsize at a global level and cannot figure or find a way to do it. For example I have something like this:
k_man
  • 178
  • 1
  • 1
  • 7
16
votes
1 answer

How to get View object on which animation was started......?

I have 3 image view in which i started same animation (translate) I have animation listener, in onAnimationEnd(Animation animation) method, I want to know on which image view the animation is ended..? From animation object how can I know in which…
Noby
  • 6,562
  • 9
  • 40
  • 63
16
votes
2 answers

Inner padding in jetpack compose

Jetpack compose has a padding modifier that is actually analogous to the margin attribute in other UI toolkits (it adds space around the target). Is there a way to add inner padding to a component in compose without wrapping it with a Box/Surface?
thewolf
  • 417
  • 1
  • 5
  • 10
16
votes
3 answers

How to set padding between ViewPager pages while keeping the page full-width

I'm using the new ViewPager-view from the Android compatibility library, and I can't figure out how to get padding between the pages so that when you're swiping between them, there's a visible border. My ViewPager width is set to fill_parent so it…
chuz
  • 506
  • 1
  • 4
  • 10
16
votes
4 answers

generating a LayoutParams based on the type of parent

I find myself needing to create a View completely in Java without knowing what concrete type the parent is. example: public View getView(int position, View convertView, ViewGroup parent){ if(null == convertView){ convertView = new…
keyboardr
  • 841
  • 1
  • 8
  • 20
16
votes
2 answers

How can I find out which view currently has focus?

My app uses keyboard control and when moving around I've noticed that occasionally you get what I'll refer to as a "Mystery View" - I say that because nothing visible seems to be selected, yet nothing visible is focussed either Is there any way to…
Jacksonkr
  • 31,583
  • 39
  • 180
  • 284
16
votes
5 answers

How can I use duplicate IDs in different layouts?

I have two different layouts for two different Activities. There is a button in each of these layouts with the same id: "@+id/btnOK". When I set a property for one of these buttons programmatically, I get a NullPointerException. But when I change…
Bob
  • 22,810
  • 38
  • 143
  • 225
16
votes
2 answers

How to change the shape of Chips component in android?

When I added ChipGroup and Chips to XML, first it gave me rendering issue which caused activity crash. I solved it by changing the support library version in the gradle.app from implementation 'com.google.android.material:material:1.2.0-alpha05' to…