Before Android 11, I've adjusted my app to fullscreen easily
My old phone had the camera hole and base buttons outside the screen area, my new phone has a camera hole and the base buttons inside a screen.
With few settings, my app was fullscreen in the old phone.
Styles.xml
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
AndroidManifest.xml
<application
...
android:theme="#style/AppTheme.NoActionBar">
</application>
In my new phone with Android 11, I've searched many Stackoverflow question. I've tried many different solutions. In the end, I put in my app the following code in that start of OnCreate().
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
window.attributes.layoutInDisplayCutoutMode =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowCompat.setDecorFitsSystemWindows(window, false)
else {
#Suppress("DEPRECATION")
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
}
I wanted a narrow margin near the base buttons and the camera hole, like Chrome.
But all can I get is a wide margin between the app and button (downward) and between the app and the camera hole (upward):
But I would want a narrow margin:
similar to Chrome browser for Android:
How can I do this programatically?
I haven't been able to find a single clue to my problem in Internet (and StackOverflow)
I also haven't been able to figure out how to identify if a given cell phone has the camera hole on the screen or if it has the base buttons on the screen. It looks like it's based on DisplayCutout, WindowInsets and boundingRectTop and boundingRectBottom, but there is no real and clear usage example in Internet.
The answer will surprise many, as it surprised myself. It was months and months of attempts and errors, frustrated help requests, until I reached the solution myself.
It is of a disconcerting simplicity, but at the same time, It was the most complicated thing in the galaxy, including the boldness of trying it
The magic and secret line is:
<meta-data android:name="android.max_aspect" android:value="2.4"/>
2.4 is maximum screen height / screen width allowed for the app area in the screen.
If you omit this statement, the system still assumes that the maximum aspect ratio is 1.86.
I don't know why the system assumes a default so low, if many devices today has a ratio screen height /screen width greater than 1.86.
From what I've written inside the question, one really needs to change one of the standard styles of Android
So in styles.xml:
<style name="AppTheme"> parent="#style/Theme.AppCompat.Light.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
In AndroidManifest.xml includes
<application
...
android:theme="#style/AppTheme">
<meta-data android:name="android.max_aspect" android:value="2.4"/>
...
<\application>
It's all...
The only remote reference I found on this subject was on the official blog of developers on a 2017 page in this post.
Related
I recently started working with PreferenceFragmentCompat and MaterialComponents and noticed that in Dark mode, Dialogs generated by preferences like ListPreference always have that weird grey android background no matter how many background attributes I change in the style.
(source: pocketnow.com)
This I've tried:
<item name="android:windowBackground">#color/colorPrimaryDark</item>
<item name="android:colorBackground">#color/colorPrimaryDark</item>
<item name="android:colorBackgroundFloating">#color/colorPrimaryDark</item>
I've also tried with android:background and worked but it brokes everything.
So I investigated a bit in-depth at elements like EditTextPreferenceDialogFragment and most of them create and show an AlertDialog instance in the same function of PreferenceDialogFragment without any possibility to change its style.
Or at least that's the conclusion I've come to after some research on the subject.
My question is, has anyone found a workaround for this? Am I doing something wrong? Cause I would like to have those dialogs matching my app theme even if it's just the background color.
Btw, sorry if it has been already answered before. I also searched in here but found nothing and similar answers for different problems without results. Thanks.
Found it! After reading more the AndroidX package found that by default the AlertDialog.Builder retrieves a default theme from an attribute when no specified in the constructor. You can see it here
So a solution would be to add a specific theme for dialogs in the activity like this:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="alertDialogTheme">#style/AlertDialogCustom</item>
</style>
And then you setup your dialog theme like:
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#FFC107</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:background">#4CAF50</item>
</style>
And this is the result:
Bonus tip: If you want to also setup the default theme for
MaterialAlertDialogBuilder you must change de attribute
materialAlertDialogTheme
I'm having an issue with developing on Android that I have tested on multiple devices and am having the same issues on all devices.
I'm creating a style named SplashTheme, in which I am setting android:background to a green color which matches my app identity. The issue I am having is whenever I create a Toast in this activity, the Toast is partially green. I have tried removing the styling to android:background and it resolves the issue, however, I am needing to style the background in that way.
Any ideas as to why this could be happening?
Instead of:
<item name="android:background">#color/your_color</item>
Do:
<item name="android:windowBackground">#color/your_color</item>
i'm just getting started with developing Android Apps. Therefore i have a question, for designing my GUI. Maybe there is a possible build-in widget, but i don't have the right wording to find it (English is not my native language).
I'm looking for something like the Rating Bar, where you can select a value between 1 and 5. Instead of the stars, i'd like to have some bars (the left one is the smallest, the right one the highest). Should look like the "Stairs" within a Signal strength notification. To make it a bit more challenging i'd like to have different colors for every bar (least value is green, highest value red). Therefore i don't think that the Rating Bar with different drawings will fit my needs, because afaik i could just change the star layout, not like the first star should become a 5dp bar, the second a 15dp and so on.
Is there any build in widget i could use?
I attached a picture to show my idea.
I hope you can help me
what you want is easily achievable with a level-list. In your case you need five different drawables, on for each level and then combine them in a drawable xml file, like:
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/no_level"
android:maxLevel="0" />
<item
android:drawable="#drawable/first_level"
android:maxLevel="1" />
<item
android:drawable="#drawable/second_level"
android:maxLevel="2" />
<item
android:drawable="#drawable/third_level"
android:maxLevel="3" />
<item
android:drawable="#drawable/forth_level"
android:maxLevel="4" />
</level-list>
and set this drawable as source for an ImageView, for instance. At runtime, retrieve the ImageView and call setImageLevel, with a value from 0 and 4, and the corresponding level will be displayed
I am working on an android app that receives content from another apps sharing it via an android intent. I did the intent filter to receive the shared content into the app, but I saw that apps like Pocket made that without leaving the original app with an overlay over the app and I don't know how to do that. Does anyone know how to do that or give me some hints?
Start activity that has transparent theme with following attributes (test this on API 18 for bug):
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
Set child elements as you desire, as you would do in normal activity.
After receiving your Intent, you should start a Service which create and attach your View/ViewGroup on the Window. This will allow you to keep the calling application in foreground.
To create a floating overlay take a look here: http://www.piwai.info/chatheads-basics/
I would look at the WindowManager Class:
http://developer.android.com/reference/android/view/WindowManager.html
Here is a link to a question that better explains what it is:
What is WindowManager in android?
First excuse me for my english.
Im designing a theme (With HoloEveryWhere) and im trying to style default spinner.
The problem is that the spinnerStyle is not working. I'm testing with this layout.
<Spinner
android:id="#+id/spCountry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/etPhone" />
And im using this for my theme.xml..
<item name="android:spinnerStyle">#style/Coloquium.Spinner.Ruby</item>
and in styles.xml
<style name="Coloquium.Spinner.Ruby" parent="#style/Holo.Spinner.Light">
<item name="android:background">#drawable/spinner_holo_white</item>
</style>
The problem is that the spinner style is never applied (Others styles like edittextstyle or buttonstyle works fine), the background never change, But i tried this style as spinnerItemStyle and work fine, but spinnerItemStyle is the inner view, and i want to style the outer, the spinner.
If i set background directly in the layout node, the background change fine, but i need to set spinnerStyle for all spinners in the theme, not in layout.
I test a lot of post looking for a solution without success.
The solution is:
HoloEveryWhere appear use "spinnerStyle" and not "android:spinnerStyle".
But really... I can't find the HoloEveryWhere documentation about this. :S