Android: How I can I add a background image to a GridLayout? - android

I have a GridLayout full of buttons - similar to a calculate with spaces between the buttons. I wants to skin the background including the those areas between the buttons but not the buttons themselves.
What is the best way to do this? Is there a means of setting a background image on the GridLayout itself or do I need to create a View hierarchy?
I had started experimenting thinking that child Views can be added to parent Views but it would appear that you can only add Views to ViewGroup derived classes?
Without a background, I'm dynamically constructing my GridLayout and adding it to the activity's default layout using setContentView( gridLayout ).
How would I go about doing this please?
EDIT: What I would really like to understand is how to compose complex view hierachies. It also appears the simple approach of gridLayout.setBackground() requires API 16 and I'm using API 14 for my devices too.

GridView is derived from View class. The View class has a method called void setBackgroundResource (), you can call it on your grid view object, pass an id of a drawable resource to it. This method was introduced since API level 1, so you don't have to worry about API compatibility issues.

You can add a background image to a GridLayout easily like this:
A) gridLayout.setBackgroundResource(R.drawable.background_image);
B)
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.background_image);
gridLayout.setBackground(drawable);
However, please note, that I found when OTA upgraded from Android 6.0 to 7.1.1 (Galaxy J5 (2016)), that GridLayout doesn't support displaying a background drawable since API 24. (Neither of the above code seems to be working above API 23, not even on Android Emulator images)
However, TableLayout's background image is properly displayed even at API 24+.
(Therefore I had to rewrote all my relevant code from GridLayout to TableLayout, but these layouts quite similar, so it not much a problem.)

Related

"Empty" container to set as another UI element's parent

I'm re-writing my app with this layout in Android (using Android Studio 3.6 & Java 8) and noticed that I can't directly set the background color of an EditText or Spinner because the background used contains the e.g. EditText's line (the line the text is written on).
To work around that in my iOS app I simply gave that UI element (with an otherwise transparent background) a View parent that uses the right background color. In Android Studio this works the same way if I use an e.g. LinearLayout as parent but I don't need that layout's full functionality and I'm worried that "abusing" LinearLayouts like this multiple times might affect performance.
There is a View widget in Android Studio but it's not possible to add an EditText or Spinner as its child.
Are there any layouts/views that can be added via Android Studio's UI builder or through xml and that act as the "empty" container I need? What's the best layout to use performance-wise?

android lollipop ViewGroup child hierarchy changes?

I have an app that has a minSdk of 15 and I'm working out all the bugs that came with the lollipop upgrade. Our software is very complex and it dynamically creates views using custom ViewGroups and then an array of elements that are explicitly sized and placed inside the group. I'm running into an issue where for example I'll have a ViewGroup and the first child object is a Button...this button is sized to fill the view (not clickable). The second child is a FrameLayout containing a single view. This single view is a video object. In all prior versions of Android this works just fine. The FrameLayout is layered over the button (that is acting as a background) and the video is inside the framelayout. You can interact with the video without any issues.
Something changed in lollipop - suddenly, even though the button is showing up as the 0 index element, it is laying OVER the rest of the children...so I cannot get to the video underneath. If I remove that button element, the video renders and plays just fine...I have no issues interacting with it.
I ran the app in UI Automator Viewer just to make sure I was really setting up the UI as I expected (keep in mind the entire view is dynamically rendered at runtime using image/video assets and xml config files).
I'm not able to share code since this is proprietary software, but I am working on a little test project to see if I can manually recreate the issue with static objects. Once I get that up and running I'll be sure to update this ticket. For now, here is a screenshot of the hierarchy:
https://goo.gl/photos/a8on9CJDnN66XYnV6
Notice the highlighted object, this is the custom ViewGroup, the children below it are what I am describing above.
Does anyone know of a change in Lollipop that would effect the ordering of things? I found earlier that if you have a disabled object but don't have a disabled state drawable assigned to that object it would become invisible, previous versions just used one of the other state drawables..okay that makes sense and it was very easy to fix, but this object is not invisible...so it must be something different.
Any direction would be greatly appreciated.
~A
UPDATE -- FIXED
With the help of #alanv and #BladeCoder I figured out this functionality was due to the new elevation feature of Material design. I was able to fix my particular issue by first checking what version of android the device was using, and if lollipop, I just add this new property to the button:
android:stateListAnimator="#null"
This prevents my explicit child hierarchy from being overridden by the OS.
Lollipop introduced elevation as a way to position the elements on the Z axis and cast shadows between them depending on their difference of elevation.
Enabled buttons have a default elevation of 2dp (and it increases when you press them). So, your button has a higher elevation than the FrameLayout (0dp by default) so it will be drawn on top of it.
Disabled buttons have an elevation of 0dp. That's why disabling the button solved your issue.
Using buttons as backgrounds looks like a bad idea (why not setting a custom Drawable background on your FrameLayout instead?) but if you really need that, you can disable the button like you did and, just to be sure, enforce its elevation to 0dp. Another workaround is to increase the elevation of the FrameLayout but then it may cast a big shadow under Lollipop if it has a background, and maybe that's not what you want.
Okay, UPDATE! I figured out how to fix the issue, although I'm still not sure (even after pouring over the diffs between several classes in grepcode) what changed in lollipop that is causing a change in how this works.
If the button is enabled...and you are placing it using something equivalent to AbsoluteLayout (We have our own ViewGroup we created called Explicit layout, but it does pretty much the same thing as AbsoluteLayout), it will always be on top of anything else in the stack that isn't also a button of some sort (at least that's what I'm finding...I didn't test through every possible widget).
Setting the button that is acting purely as a background image to enabled=false solves this issue. I know, it doesn't make sense that we use Buttons as background images, but our code uses it for dynamic element creation so there are many possible states and uses for each element.
Anyway, not sure if anyone else would even run into this issue, but just in case you do...here it is.
Thanks!

Android Tabs with overlaying / overlapping Image

i have a conceptual android question. I use the appcompat library (API min:15, target:21).
And want to place an image over the Top-Tabs (or even within) and the content below. (Content could be a swipeView)
Like this:
Important is that the image should be fixed within the last tab and laying over the content below. (should be fragment independent)
How would you do it?
Is there something new in Api 21 that can handle this easily?
Thanks!!
API 21 will not make this easier. What you can make use of API21 is ViewOutline to make your floating action view oval.
However, positioning of the view is likely unrelated to the API version. You need to place the floating button between your tabs holder and content layout. Oval view bottom should be aligned with content and has the bottom margin usually to the half of it's height.

Which layout should I use (css inline-block equivalent)

I would like to build a specific layout for an android application. Actually, I would like to obtain something with the same behavior as inline-block div in html/css... I don't really know if I should use a LinearLayout or a GridLayout or something else...
It's like a horizontal LinearLayout but when the line is full, I would like my objects to go on the next line... I'm going to add the items programmatically to the container...
Here's an example of what I would like to do:
Do you have any idea?
Thanks by advance,
Valentin
Instead of using a layout, it might be easier to use a GridView because they will only use memory for items on screen and can be added using an adapter.
Read more here: http://developer.android.com/guide/topics/ui/layout/gridview.html
Android provides a GridLayout component (along with the helper view Space which can be used to build a flexible layout with multiple rows and columns, as explained in The Android Developers Blog. It is available as of Ice Cream Sandwich (v14) or as part of the v7 support library, and hence available to all 2.1+ devices.

Android: Adding a Button at a Specific Location

After reading the very helpful answer here: Why does LayoutInflater ignore the layout_width and layout_height layout parameters I've specified?, one thing I would like to add is to have a button (or any other View for that matter) be placed at a specific point on the screen.
You can use FrameLayout and specify margins for objects using parameters android:layout_marginRight, android:layout_marginBottom and so on. This will help you to set view position relative to the parent view.
You can also use AbsoluteLayout, but it is discouraged to do so. AbsoluteLayout was used when Android was supposed to work with the screens of a fixed resolution, and when this changed with Android 1.6, AbsoluteLayout became deprecated since it is inflexible and can't work properly on screens with different resolutions.
Hi you can use AbsoluteLayout for positioning a view to a particular location if you know the coordinate of the view.use this link for the overview of available layout.
http://mobiforge.com/designing/story/understanding-user-interface-android-part-1-layouts.

Categories

Resources