Questions tagged [android-view]

Questions regarding Views in Android. Views may be defined in Android Layout XML or in Java code. Questions using this tag involve general View practices or advice. With regard to specific Views, refer to the info for this tag. For questions regarding layout, use the [android-layout] tag.

Introduction

A View in Android is the display of the data utilized by the application. Views range widely from simple and non-interactive to complex and interactive in many ways. In addition to the Views provided by the SDK, developers have the option to create custom Views. This means that virtually any communication or display to the user may be handled in innumerable ways.

ViewGroups

ViewGroup is a subclass of View, which has the ability to hold other Views. Children Views may be added to the ViewGroup at run-time (in Java code) or may be pre-defined in Android XML. Like Views, ViewGroups may also be extended in the same way. Follow these guidelines for questions regarding ViewGroups:

  • If the ViewGroup is provided by the Android SDK, it should be listed below in Standard Views.
  • If the ViewGroup is a Custom View, use

Standard Views

A wide variety of Views are delivered with the Android SDK for use by developers. These views make up a majority of the Views in a standard application. Each View has its own semantics and behaviors. As a result, it is often more beneficial to use the tags specifically related to those Views. Below is a list of standard views provide within the Android SDK:

Usage: vs.

While Views and layouts work hand-in-hand, questions regarding the two are often very different. These guidelines will help to properly determine which tag to use:

  • If the question refers to the properties used to display the View properly, then use .
  • If, instead, the question refers to the functionality of the View, then is appropriate.
3981 questions
150
votes
9 answers

Can I use view pager with views (not with fragments)

I am using ViewPager for swiping between Fragments, but can I use ViewPager to swipe between Views simple XML layout? This is my page Adapter for the ViewPager which is used to swipe between Fragments: import java.util.List; import…
ranjith
  • 4,526
  • 5
  • 27
  • 31
148
votes
11 answers

How to programmatically round corners and set random background colors

I'd like to round the corners of a view and also change the color of the view based on the contents at runtime. TextView v = new TextView(context); v.setText(tagsList.get(i)); if(i%2 == 0){ v.setBackgroundColor(Color.RED); }else{ …
146
votes
10 answers

Add and Remove Views in Android Dynamically?

How do I add and remove views such as TextViews from Android app like on the original stock Android contacts screen where you press a small icon on the right side of a field and it adds or deletes a field which consists of a TextView and an…
Jono
  • 17,341
  • 48
  • 135
  • 217
141
votes
13 answers

Determining the size of an Android view at runtime

I am trying to apply an animation to a view in my Android app after my activity is created. To do this, I need to determine the current size of the view, and then set up an animation to scale from the current size to the new size. This part must…
Nik Reiman
  • 39,067
  • 29
  • 104
  • 160
127
votes
20 answers

How to make a view with rounded corners?

I am trying to make a view in android with rounded edges. The solution I found so far is to define a shape with rounded corners and use it as the background of that view. Here is what I did, define a drawable as given…
Zach
  • 9,989
  • 19
  • 70
  • 107
126
votes
3 answers

What is the difference between the states selected, checked and activated in Android?

I'd like to know what differs those states. I didn't find any webpage clarifying this.
Louis
  • 2,140
  • 4
  • 19
  • 18
107
votes
9 answers

How does the getView() method work when creating your own custom adapter?

My questions are: What is exactly the function of the LayoutInflater? Why do all the articles that I've read check if convertview is null or not first? What does it mean when it is null and what does it mean when it isn't? What is the parent…
GrowinMan
  • 4,891
  • 12
  • 41
  • 58
103
votes
6 answers

What is the benefit of using Fragments in Android, rather than Views?

When developing for Android, you can set your target (or minimum) sdk to 4 (API 1.6) and add the android compatibility package (v4) to add support for Fragments. Yesterday I did this and successfully implemented Fragments to visualize data from a…
Phil
  • 35,852
  • 23
  • 123
  • 164
101
votes
10 answers

Android View.getDrawingCache returns null, only null

Would anyone please try to explain to me why public void addView(View child) { child.setDrawingCacheEnabled(true); child.setWillNotCacheDrawing(false); child.setWillNotDraw(false); child.buildDrawingCache(); if(child.getDrawingCache() ==…
Bex
  • 2,905
  • 2
  • 33
  • 36
98
votes
4 answers

Get margin of a View

How can I get the margin value of a View from an Activity? The View can be of any type. After a bit of searching I found out ways to get padding of a view, but couldn't find anything on Margin. Can anyone help? I tried something like…
Arnab Chakraborty
  • 7,442
  • 9
  • 46
  • 69
92
votes
6 answers

sendUserActionEvent() is null

I've got a real doozy here. When I click on spinners, open menu items, or open context menus on long-clicks I get the same Logcat message: 08-02 21:20:57.264: E/ViewRootImpl(31835): sendUserActionEvent() mView == null The tag is ViewRootImpl, and…
crocboy
  • 2,887
  • 2
  • 22
  • 25
86
votes
13 answers

Android SeekBar Minimum Value

How to define a SeekBar's minimum value? Is this done in the XML layout or do I need to define it programatically? Basically I need to change my minimum value from 0 to 0.2
Sachin
  • 2,667
  • 9
  • 35
  • 39
83
votes
10 answers

How to set Id of dynamic created layout?

I want to give ID to some views (textview ,imageview etc) in a layout that is programmetically created. So what is the best way to set ID.
abhishek ameta
  • 2,326
  • 7
  • 28
  • 35
82
votes
11 answers

How to center a View inside of an Android Layout?

I want to put a layout in the center of the screen.
Vicky
  • 933
  • 1
  • 9
  • 9
77
votes
4 answers

Android AppCompat 21 Elevation

Is there any way to add elevation to a View in pre-lollipop devices without wrapping them in a CardView?
Eliezer
  • 7,209
  • 12
  • 56
  • 103