window background appears on Jetpack Compose screens - java

I have a project that uses old xml and views. One fragment is refactored using Jetpack Compose.
When I try to navigate between fragments, android:windowBackground is displayed for a second and disappears. I have tried applying background color to my composables, but windowBackground displays anyway.
binding.composeView.setContent {
AppTheme {
Surface(color = Color.Blue) {
// content
}
}
}
<item name="android:windowBackground">#drawable/logo</item>
related issue

Related

CodenameOne - wrong theming for side-menu on Android device

I tried to run my Codename app on my Android phone
after sending the Android build
and I realized that the theme is perhaps wrong. I chose the native theming in the app.
Indeed I get a dark-grey side menu with black text that is very small, while the TextFields are in the right font and white background.
On the simulator (various Android devices skin) the theme seemed right, with good text size and background color.
Here's the relevant code:
mainForm.setToolbar(toolbar);
toolbar.setEnabled(true);
toolbar.showToolbar();
BackCommand backCommand=new BackCommand("Back");
mainForm.setBackCommand(backCommand);
Container topBar = BorderLayout.west(new Label());
String tagLine=.menu_tagline;
topBar.add(BorderLayout.WEST, new Label(tagLine, "SidemenuTagline"));
topBar.setUIID("SideCommand");
toolbar.addComponentToRightSideMenu(topBar);
toolbar.addMaterialCommandToRightSideMenu(help_menu_item, ' ', e -> {
toolbar.closeRightSideMenu();
new HelpViewer(appData, mainForm).show();
});
toolbar.addMaterialCommandToRightSideMenu(liability_disclaimer_menu_item, ' ', e -> {
toolbar.closeRightSideMenu();
openDialog(mainForm,liability_disclaimer_dialog_title,liability_disclaimer));
});
....
....
....
toolbar.addMaterialCommandToRightSideMenu(about_menu_item, ' ', e -> {
toolbar.closeRightSideMenu();
openDialog(mainForm,about_dialog_title,about_dialog_text);
});
What can be done to have the same theme for the menu? Or the readability for it?
This video is a bit old but the basic concepts are still applicable: https://www.codenameone.com/how_di_i/how-do-i-create-gorgeous-sidemenu.html
The same concepts can be applied in CSS if you use CSS styling for the app. It's theoretically possible to customize a side menu from code but it would be REALLY hard as the menu is created dynamically internally within the framework.

I want to change the icon color when selecting in library StickySwitch

I want to change the color of the icon upon selection, I found that the library does not exist in its properties
Just what I want to change the icon color to white when selected
<io.ghyeok.stickyswitch.widget.StickySwitch
android:id="#+id/sticky_switch"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:layout_marginHorizontal="20dp"
app:ss_animationDuration="600"
app:ss_iconPadding="10dp"
app:ss_iconSize="22dp"
app:ss_leftIcon="#drawable/ic_contact_s"
app:ss_leftText="Contact"
app:ss_rightIcon="#drawable/ic_hourglass_s"
app:ss_rightText="Requests"
app:ss_textVisibility="gone"
app:ss_selectedTextSize="13sp"
app:ss_sliderBackgroundColor="#color/white"
app:ss_switchColor="?colorPrimaryTheme"
app:ss_textColor="?colorPrimaryTheme"
app:ss_textSize="12sp"
app:ss_animationType="line"
/>
You will have to do it programmatically or using a selector.
Programatically
Set a listener and then change the icon based on that:
stickySwitch.setOnSelectedChangeListener(object : StickySwitch.OnSelectedChangeListener {
override fun onSelectedChange(direction: StickySwitch.Direction, text: String) {
if (direction == StickySwitch.Direction.RIGHT) {
stickySwitch.setRightIcon(R.drawable.ic_hourglass_s_white)
stickySwitch.setLeftIcon(R.drawable.ic_contact_s_white)
} else {
stickySwitch.setRightIcon(R.drawable.ic_hourglass_s)
stickySwitch.setLeftIcon(R.drawable.ic_contact_s)
}
}
})
If the library can support KT then the setter are not necessary:
stickySwitch.rightIcon = R.drawable.ic_hourglass_s_white
But considering the setter is passing an R.drawable resource then maybe it has to be used.
The library does support setting the drawable directly, so you could apply a tint to the drawable instead
stickySwitch.setLeftIcon(drawable)
Drawables are mutable so if you keep or get the reference you could replace the logic above and instead of using a tint. This question should help you with the tint part:
Change drawable color programmatically
Selector
For creating a selector to make the job, there are plenty of questions and answers on SO, this example should help you:
https://stackoverflow.com/a/17647520/4017501
That would only work if the library is working like a regular switch, and for applying it:
app:ss_leftIcon="#drawable/your_custom_selector"
A selector handles the view state, so if is selected it would choose a drawable otherwise the other.

Lock screen orientation when targeting Android API 27 with a non-opaque activity

I have an activity that has android:windowIsTranslucent set to true and android:windowBackground set to a translucent background. I just changed my target and compile sdk version to 27, and I get an exception when launching this activity now:
java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
Since this is a new sdk, there isn't anything online about it yet (and it seems to result from this line of code: https://android.googlesource.com/platform/frameworks/base.git/+/master/core/java/android/app/Activity.java#987 )
Is there any way to get around this? The app doesn't crash if I take out android:screenOrientation="portrait" from my manifest for this activity, but I would like to be able to keep it like that.
I also faced the same problem.
As others said, If I deleted android:screenOrientation="portrait" or overrided it with android:screenOrientation="unspecified", then the exception was gone.
And it seems that the front activity's orientation follows the behind activity's orientation.
I thought about it.
If the front activity is transparent and the behind activity's orientation is different,
the display becomes strange.
So, I can understand why this check logic was added
However, I wonder that why this problem was not occurred in Developer Preview 8.0.0.
The workaround is to set targetSdk back to 26.
The reason why is your application crashes is here in this commit.
As you can see here, you are not the only one - this behavior has been reported to Google as issue. It has been fixed, but we don't know how and when it will be released.
I can also confirm what "sofakingforever" says in comments, if there is non-translucent activity with fixed orientation behind your translucent, the translucent will not rotate. So you can just remove android:screenOrientation="portrait" from manifest as well.
The solution worked for me is deleting
android:screenOrientation="portrait"
from all the full screen transparent activities which means their theme contains
<item name="android:windowIsTranslucent">true</item>
Also to make sure that orientation works correct for below Oreo I added this to the onCreate() of the activities.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// This activity is a fullscreen transparent activity, so after Oreo Android doesn't allow fullscreen
// transparent activities to specify android:screenOrientation="portrait" in the manifest. It will pick up
// from the background activity. But for below Oreo we should make sure that requested orientation is portrait.
if (VERSION.SDK_INT < VERSION_CODES.O) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
I solved this issues by changing this line in NoActionBar styles
In target version 27 only i got this issue and i solved by using below line
<item name="android:windowIsTranslucent">false</item>
So what I did was remove any screenOrientation property from manifest and add it to my BaseActivity (from which all my activities extend), this code
if(!(this instanceof TranslucentActivity)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
The TranslucentActivity will have the orientation from the Activity behind.
it seems like it's a new feature/bug on API 27.
However, you can delete
android:screenOrientation
Or
android:screenOrientation="unspecified"
I recently faced the issue and here's the solution.
No need to change the screen orientation parameter which you set at the android manifest file.
Just add two folders in
res>values
as res>values-v26
and res>values-v27
Then copy your styles.xml and themes.xml file there.
and change the following parameters from TRUE to FALSE.
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowIsTranslucent">false</item>
It will work.
A common bug of Android 8.0
Thanks #JerabekJakub. My test result - keep sdk 27 and remove the following lines can also solve the crash.
android:configChanges="orientation"
android:screenOrientation="portrait"
1) Remove this
android:screenOrientation="portrait"
from minifiest.xml
2) on Activity add these two lines
protected void onCreate(Bundle savedInstanceState) {
setOrientation(this)
super.onCreate(savedInstanceState);
// other other all code here
}
3) Just copy-paste the code in your Activity
public static void setOrientation(Activity context) {
if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.O)
context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
else
context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
I had this problem on Android 8 using BottomSheetDialogs. It was crashing every time I open some translucent bottom sheet dialog.
I fixed the problem by adding new theme.xml in values-v26, with same theme as default one, but completely removing windowIsTranslucent item from there.
After that, I still had translucent bottom sheet dialogs in my app and it is working perfectly.
Found it interesting. It may help someone using these dialogs.

Android onCreateOptionsMenu is not displaying

In my android activity, i removed the Action Bar by using this Theme
<style name="Theme.NoTitle" parent="#android:style/Theme.NoTitleBar"></style>
but my problem is “onCreateOptionsMenu” is not display, I tried this method to create
that pls see
private void getOverflowMenu()
{
try
{
ViewConfiguration config = ViewConfiguration.get(Home.this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null)
{
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config , false);
}
} catch (Exception e)
{
e.printStackTrace();
}
}
in this “onCreateOptionsMenu” is not displaying. please help me
but it displaying in Samsung
Because: in Samsung mobile has its own hardware menu button, but not in nexus etc
please see the image
finally i got the answer
in my AndroidManifest.xml i just removed this line
android:targetSdkVersion="18"
If you don't have action bar and neither a menu button (on most devices) where do you want to display your menu??
The easiest way is to use PopupMenu. Add a button somewhere in your layout and on the click even add the PopupMenu (Dropdown menu). If you want to use it for devices starting Android 2.1 use a Support Library.
You have HERE the android documentation about PopupMenu.
From SDK Manager you can download and see the support library v7 sample which includes PopupMenuHelper.
Hope it helps. Good Luck!

Change Background Image of RelativeLayout from within Java Class (Android App)

So I am working on an Android application using Android 4.0 Library.
One of the activities of this application is made up of a RelativeLayout that has an image background and a toggle button.
The background image of the layout must change when the user toggles the button.
So it must be changed from inside the activity.java class:
if (toggleButton.isChecked()){
// Change the background of the activity to image 2 (for example)
}
else{ // when toggle button is off
// Change it back to image 1
}
Please help me with this. Thank you :)
You use the method, setBackground in the View class:
if (toggleButton.isChecked()){
// Change the background of the activity to image 2 (for example)
View myView = this.findViewById(yourViewId);
myView.setBackgroundResource(yourImage);
}
else{ // when toggle button is off
// Change it back to image 1
// Change the background of the activity to image 2 (for example)
View myView = this.findViewById(yourViewId);
myView.setBackgroundResource(yourOtherImage);
}

Categories