- How to set foreground attribute to other non FrameLayout view
- 3 Answers 3
- android:foreground attribute / setForeground() method does not work with Button elements
- 2 Answers 2
- How to set background color of a View
- 21 Answers 21
- Android Studio Image Asset Launcher Icon Transparent Background Color
- Tried solutions:
- 4 Answers 4
- Icons for Android
- Android O and beyond
- Layer specs
- Foreground (scrolling parallax)
- Background (subtler parallax)
- Workaround for 7.1 or lower
- Update #1:
- Update #2:
- In Nexus 5X device (Android 8.1)
- In Nexus 5 emulator (Android 8.1)
- Opaque white background in some launcher apps
- Launcher3 source code
How to set foreground attribute to other non FrameLayout view
I would like to know how to apply or emulate foreground effect in a view different from FrameLayout, as LinearLayout or RelativeLayout
This is what I have now:
And I want something like:
Thanks in advance!!
3 Answers 3
The idea is to surround your layout with a FrameLayout, and set the selector and the onClick event to this layout.
You can find a full explanation at my blog:
Checkout ForegroundView library with Gradle integration. It supports following views
- ForegroundImageView
- ForegroundButton
- ForegroundTextView
- ForegroundImageButton
- ForegroundEditText
- ForegroundWebView
- ForegroundLinearLayout
- ForegroundRelativeLayout
- ForegroundGridLayout
- ForegroundGridView
- ForegroundHorizontalScrollView
- ForegroundListView
- ForegroundScrollViewForegroundImageView
Here is a possible implementation, one which also supports checking a layout.
A nearly identical solution can be applied to any layout view you wish (only the CTOR is different).
attr.xml
Here’s a more minimal way to do it, using Kotlin, and without the part of being checkable :
Источник
android:foreground attribute / setForeground() method does not work with Button elements
Since Android 23, the android:foreground XML attribute (and corresponding setForeground() method) should be available to all Views, and not just FrameLayout instances as was previously the case.
Yet, for some reason, whenever I create a Button instance which inherits from TextView -> View, I can’t seem to get a foreground to show at all.
Here is an example button definition that doesn’t work appear to show the foreground:
And here is the defintion of the Button.Primary style:
I’ve confirmed that setting the foreground attribute in the style or in the definition does not change anything, the foreground fails to show regardless. The background shows correctly, as does the text, it’s just the foreground that’s missing.
I’ve also tried setting the foreground to a solid color instead of a state selector drawable, with no success.
2 Answers 2
As stated by Keith setHorizontallyScrolling(true) , scrolling makes the foreground disappear. One can easily reproduce the problem.
But why does horizontal scrolling affect the foreground?
Setting horizontal (or vertical scroll) tells the TextView it contains a larger area than it’s layout bounds.
If you print the scrollX / scrollY values, normally it won’t be zeroes but a very big number instead, even if the content is not scrollable by users.
On the other hand, the foreground is designed to scroll with the content, so it’s basically drawn starting from (0, 0) of the canvas, not the top left corner of the visible content. So it’s drawn far far away from the actual view bounds. You can open the View.java for the source code of onDrawForeground for that.
What can I do
- Disable scroll by calling setHorizontallyScrolling(false) after setting the singleline attribute or anything that implies scrolling. Beware not to get scroll enabled trying to initialize / update the view.
- translate the foreground drawable, e.g. update the bounds from setBounds by adding scrollX & scrollY
After removing the style elements one by one and retesting, I narrowed down the issue to the inclusion of the android:singleLine attribute in the style.
Removing this attribute made it so any TextViews or Buttons using my style would properly show the foreground drawable as desired. Looking through the implementation of TextView.java where singleLine is defined, I’m still struggling to determine how setting this attribute causes foregrounds to be ignored, but I will update this answer if I find out.
I know that use of singleLine is deprecated but unfortunately I still need the functionality provided by the combination of the singleLine / ellipsize attributes which are not available when replacing with maxLines usage.
Edit: After writing out my answer above, I decided to do some more investigation and uncovered a few more details.
First, I created a custom view that extended from AppCompatButton so I could attempt to re-implement the functionality singleLine / ellipsize provided (namely, showing text on a single line by replacing newline characters with spaces and then also adding ellipsis if the text ran off the view).
Reading through the TextView source code, I found a section of code that is called when the singleLine attribute is set to true:
So I tried extracting those lines and adding to my own implementation:
But when this code ran, I saw the same issue from before where foreground drawables would no longer be visible.
After further testing, I was able to narrow down the issue to the setHorizontallyScrolling(true) method call. If I commented just this line out of my custom implementation, I’m able to preserve the singleLine / ellipsize functionality I had before and foreground drawables show as expected! Narrowing it down to that method still didn’t help me figure out the root cause in the TextView base implementation, but if anyone has any details on that, feel free to comment with more information.
Here is my final custom Button class which simply looks at the maxLines attribute and simulates the old singleLine functionality when maxLines is set to 1, avoiding the method call that would prevent foregrounds from showing. In case it’s useful.
Источник
How to set background color of a View
I’m trying to set the background color of a View (in this case a Button).
I use this code:
It causes the Button to disappear from the screen. What am I doing wrong, and what is the correct way to change the background color on any View?
21 Answers 21
You made your button transparent. The first byte is the alpha.
When you call setBackgoundColor it overwrites/removes any existing background resource, including any borders, corners, padding, etc. What you want to do is change the color of the existing background resource.
Experiment with PorterDuff.Mode.* for different effects.
Several choices to do this.
Set background to green:
Set background to green with Alpha:
Set background to green with Color.GREEN constant:
Set background to green defining in Colors.xml
or the longer winded:
You can set the hex-color to any resource with:
The code does not set the button to green. Instead, it makes the button totally invisible.
Explanation: the hex value of the color is wrong. With an Alpha value of zero, the color will be invisible.
The correct hex value is 0xFF00FF00 for full opacity green. Any Alpha value between 00 and FF would cause transparency.
For setting the first color to be seen on screen, you can also do it in the relevant layout.xml (better design) by adding this property to the relevant View:
and what is the correct way to change the background color on any View?
On any View ? What you have is correct, though you should drop the invalidate() call.
However, some Views already have backgrounds. A Button , for example, already has a background: the face of the button itself. This background is a StateListDrawable , which you can find in android-2.1/data/res/drawable/btn_default.xml in your Android SDK installation. That, in turn, refers to a bunch of nine-patch bitmap images, available in multiple densities. You would need to clone and modify all of that to accomplish your green goals.
In short, you will be better served finding another UI pattern rather than attempting to change the background of a Button .
Источник
Android Studio Image Asset Launcher Icon Transparent Background Color
You might be thinking this question is a duplicate of this one. But since then, Android Studio has been updated and the solution given there is not working anymore.
I am trying to set my app logo using image asset in android studio. This is because if I put my app logo directly in drawable or mipmap, then it causes many problems like: If it’s size is big then app crash occurs, if device running on oreo then logo will not showed and default ic_launcher is showing etc.
While trying to set my app’s logo using image asset, I am facing a problem: I can’t keep app logo’s background transparent.
I have a png logo made in photoshop and I want to set this as my app logo and I don’t want any background but android studio image asset doesn’t provide any option to remove background. I tried following solutions from google:
but none of these worked for me.
Tried solutions:
- By setting shape to none
- by Deleting ic_launcher_background file from it’s default location
- Tried in different devices
None of these works. please help me. Any help will be appreciated.
4 Answers 4
Android 8.0 Oreo (API level 26) introduced adaptive launcher icons, which consist of two layers: a foreground and a background. The material design guidelines state that the background layer must be opaque in Android O and beyond, as you can see at the bottom of the following citation. So, the adaptive launcher icon for Android 8.0 or higher must have opaque background color at least, if targetSdkVersion of your app is 26 or higher.
Icons for Android
Android O and beyond
Android O icons represent your app on a device’s Home and All Apps screens. The following guidelines describe how icons can receive unique visual treatments, animations, and behaviors.
Layer specs
Icons consist of two layers: a foreground and a background. Each layer can animate and receive treatments independently from the other layer.
Foreground (scrolling parallax)
- 108 x 108 dp
- 72dp masked section
- Transparency recommended (optional)
Background (subtler parallax)
- 108 X 108 dp
- 72dp masked section
- Must be opaque
Workaround for 7.1 or lower
Although launcher icons for 8.0 or higher must have opaque background color, the other legacy launcher icons for 7.1 or lower can revert to transparent background color, if you can omit round launcher icons from your app.
- First, create Launcher Icons (Adaptive and Legacy). It will create adaptive launcher icons and legacy launcher icons. All of them have opaque background color, at first.
- After that, create Launcher Icons (Legacy only). This will overwrite only the existing legacy launcher icons, as you can see in the second screenshot below. If you set the shape to none , they will have transparent background color.
- Delete folder res/mipmap/ic_laucher_round in the project window.
- Open AndroidManifest.xml and remove attribute android:roundIcon=»@mipmap/ic_launcher_round» from the application element.
In the left-side pane above, the following XML files define adaptive launcher icons for Android 8.0 or higher.
As seen in the right-side pane, they refer to the following drawable XML files.
Update #1:
In Android 8.0 or higher, the background color of launcher icons can be transparent, as shown in the Android 8.1 (Nexus 5X) screenshots. The sample app «NoAdaptive» is without any resource for the adaptive launcher icons in folder mipmap-anydpi-v26 , and the other app «Adaptive» has the resource.
Update #2:
Although the background color of launcher icon can be transparent in Android 8.0 or higher, it depends on user’s launcher app. Some launcher apps will convert your legacy icon to opaque adaptive icon.
In Nexus 5X device (Android 8.1)
The default launcher app is Google Now Launcher, according to its package name com.google.android.launcher . The background color can be transparent, as in the screenshots of Update #1.
In Nexus 5 emulator (Android 8.1)
The default launcher app is Pixel Launcher, according to its package name com.google.android.apps.nexuslauncher . The background color can be transparent in Recents screen, as in the screenshots below:
- opaque in Home screen
- transparent in Recents screen
- opaque in All Apps screen
Opaque white background in some launcher apps
These GMS apps are closed-source:
- com.google.android.launcher Google Now Launcher
- com.google.android.apps.nexuslauncher Pixel Launcher
In contrast, AOSP apps are open-source. Most launcher apps in Android are based on the source code of the following launcher apps:
- com.android.launcher
- Launcher (discontinued)
- Launcher2 (legacy)
- com.android.launcher2
- Launcher2 (original package name)
- com.android.launcher3
- Launcher3 (for Android 5.0 Lollipop or higher)
Launcher3 source code
In the git branch oreo-release of Launcher3, LauncherIcons.java has the method wrapToAdaptiveIconDrawable that wraps legacy launcher icon in adaptive launcher icon.
The flag FeatureFlags.LEGACY_ICON_TREATMENT is defined in FeatureFlags.java:
So, the background color of legacy launcher icon depends on this flag, and it can be opaque in some launcher apps such as Pixel Launcher.
Источник