How to change a layout background image depending on Dark/Light theme? - java

I've got two images. I want to do so: when user opens an app with a Light theme - the first image is being used as a background for layouts. When user opens the app with a Dark theme - the second image is being used as a background for layouts.
To solve this problem with text colors, we can just use styles.xml and colors-day/night.xml and one line of code: <item name="android:textColor">#color/textColor</item>
I've tried to the same with images and two styles files: <item name="android:background">#drawable/day</item>
But this feature applies background to each element on a screen, not only to the main layouts.
I know, that I can do it programatically by changing a background with if statements and layout.setBackgroundResource(R.drawable.day/night);
But maybe it can be done with XML as in the case of the text color?

To solve this problem with text colors, we can just use styles.xml and
colors-day/night.xml and one line of code: #color/textColor
You need to do the same thing, but instead of res/values/colors.xml, it should be done on the drawable res/drawable/day
So, you'll have a drawable & drawable-night folders in the res directory each of which should have a unique version of the day image with the same name.
They should look like in Android Studio (Android preview):
And normally use the the attribute name="android:background">#drawable/day</item>

Related

Change Android status bar color on home/lock screen based on custom events within an app

I am currently developing a simple android app which should change the status bar color for home/lock screen based on certain events happening into the background(similar with how the status bar color turns orange when battery level drops under 10%). I know that status bar color can be changed within the application itself but can this be done at the system level without root access?
Many thanks,
Lucian
there is no way to make change in color of home/lock screen because just you access to make a change in an activity status bar color and it does not work in all android sdks,
but you can develop a launcher and then make a service for it after that you can pass data to service and then change home/lock screen status bar color.
and the code of services and change status bar color and making launcher is in internet and you can access to them
i hope it was helpful for you !!
if you have some other questions or needs to help ask and then i try to help (:
good luck
To change android status Bar Color see How to Customize the Status Bar on Android (Without Rooting)
Add on MainActivity.java, getWindow().setStatusBarColor(0x00000000); also
see if backgroundColor of AppBar is transparent maybe status bar fonts can't read it.
1)First go
On colors.xml file
see if you have add already the code inside the 2 elements , if not put that
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black_overlay">#66000000</color>
<color name ="purple">#770B7A;</color>
</resources>
Write a color in this element <color name ="purple">, in my example I name it purple, the color, purple is an example, give whatever color you want.
2)Second go
On MainActivity.java file
Notice that there is a comment which say //change status color , so under this commend write code
if(android.os.Build.VERSION.SDK_INT >= 21) {
Window.window=this.getWindow();
Window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKBROUNDS);
Window.clearFlag(windowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
Window.setStatusBarColor(this.getResources().getColor(R.color.your_color));
}
this is a plugin but be sure to write the name of color you want in this code (R.color.your_color)
write the name of color like this (R.color.purple), the java doesn't exist the code color so that's why I write
the name of color.
Notice: be sure to write the same color name, you use on color.xml file and on MainActivity.java file.

Toast inheriting theme background

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>

Android DatePicker Selector Color

So, this question is kind of an extension of this question, I solved the issue with the headerBackground but I realised that I had manually set the theme of my DatePicker in my layout using android:theme="#style/State0DatePickerTheme. But when I removed that line from my XML, the day selector just turned back to white (my accent colour defined in my base theme). Any thoughts on why this is, or how to fix it?
For my purposes, I need to have the theme for DatePicker set in my styles.xml with android:datePickerStyle because I'm setting my theme dynamically in my onCreate() and I can't change the theme of the datePicker in java (at least not that I'm aware of).
So, I figured out how to fix it, I simply created a new theme (which I called State0HistoryTheme) and then made its parent State0BaseTheme and applied that theme to my entire activity in my onCreate() using setTheme()
This is what my State0HistoryTheme looks like for those curious:
<style name="State0HistoryTheme" parent="State0BaseTheme">
<item name="colorAccent">#color/state0Dark</item>
</style>
This was able to override the colorAccent that I had set in State0BaseTheme

Android using 9-patch works for some buttons but not others?

In my main activity, I was able to use my 9-patch images for the backgrounds of the 4 main buttons by coding into the XML.
main activity xml
<button
style="#style/button_default"/>
^ This works, and the buttons on the main activity use the button_default.xml images for the button states.
Then, I try to do the exact same thing in another activity using the exact same method, and it doesn't work. The buttons are still the default gray. I then tried using the java method to set the background
java file for different activity
//This works
buttonOne.setBackgroundColor(Color.RED);
//This does NOT work
buttonOne.setBackgroundResource(R.drawable.button_default);
So, whether I try setting the background for the buttons through XML or Java, I can't get it to work. But, I can get it to work only in the main activity. Is this common, or is there a reason why it isn't working?
#alanv, here's the very simple style/button_default that's located in styles.xml
<style name="button_default">
<item name="android:background">#drawable/button_default</item>
</style>

Custom Tab Disappears on Click?

In my Android application I have three tabs in TabHost.
Tab number 2 and 3 loads some data that will take around 4-5 seconds to load. When I click on any of these tabs, the tab disappeared until the data got loaded. Is there any reason for that? How can I handle this disappearing tab ?
One more thing that I should have to mention here is that I am creating Custom Tabs. But no rocket science in custom tabs just follow this links tutorial: http://joshclemm.com/blog/?p=136.
Well I have fixed the problem and find it out what I have done wrong.
The tab_bg_selector.xml file in the example is using android:state_pressed="true" and android:state_focused="true" item states which I have copy/paste in my code as it is. When the Tab is pressed the android:drawable="#android:color/transparent" will make the image transparent until the selected Image will display
So both these item are removed from the .xml file and now it working good :)

Categories