I feel like this is listed somewhere and is extremely easy but I cannot seam to find a straightforward answer. How do you set a custom background image. For instance set a PNG as the default background for my app instead of the black screen. Manifest, Layout, Main code thing? An example would be extremely amazing.
xml/values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme.Light"> <!-- Or any other parent you want -->
<item name="android:windowBackground">#drawable/yourcustombackgroundimage.png</item>
</style>
</resources>
and use this style in the manifest file.
<application android:theme="#style/MyTheme">
For those of you that are still looking to do this, the correct way to do it has changed slightly. Piggy-backing off of 500865's answer, here is the adjusted code:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyTheme" parent="android:Theme.Holo">
<item name="android:background">#drawable/dummy_background_image</item>
</style>
</resources>
Basically, you have to change windowBackground to just background, you have to include the line about xmlns:android, and you should (although not required) remove the .png extension.
Related
I know I might be making more than enough mistakes here. But do lemme know.
I've tried going through MaterialIO documentation the best way I can, and it all looks so spun up and complicated with a dozen links in each page redirecting me to another minor component.
That's when I decided to TRY it.
I added materialIO stuff to my dependencies
I then added Material to my manifest file, and wrote this
<application ...
android:theme="#style/Theme.MaterialComponents.DayNight" >
</application>
Next, I made a values/themes.xml and values-night/themes.xml as instructed by some part of the material IO page.
My intuition was to write a style tag in those resources that extended the Material theme and set my color values. The colors I found from the materialIO page :
Background (0dp elevation surface overlay)-->
Surface (with 1dp elevation surface overlay)-->
Primary
Secondary
On background
On Surface
On Primary
On Secondary
So I started adding the tags to values-night/themes.xml. I couldn't apply android: to some tags and some tags raised errors when I did.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppCompat.." parent="Theme.MaterialComponents.DayNight">
<item name="android:colorBackground">#color/JDBG</item>
<item name="colorSurface">#color/JDSurface</item>
<item name="colorPrimary">#color/JDPrimary</item>
<item name="android:colorSecondary">#color/JDSecondary</item>
<item name="android:OnBackground">#color/JDOnBackground</item>
<item name="android:OnSurface">#color/JDOnSurface</item>
<item name="OnPrimary">#color/JDOnPrimary</item>
<item name="OnSecondary">#color/JDOnSecondary</item>
</style>
</resources>
Then I added the colors to the respective colors.xml and colors-night.xml files, which told me that those values didn't have a declaration in the base values folder and it might raise issues.
I do not want to toil at nothing with very abstract documentation that leads me on ten directions at every point. Hence the question . . .
Is there anything wrong I've done ?
What do I do next ?
Just define your theme in a resources file.
For example res/values/styles.xml:
<style name="MyTheme" parent="Theme.MaterialComponents.DayNight">
<item name="colorPrimary">#color/....</item>
<item name="colorSecondary">#color/....</item>
<item name="colorOnPrimary">#color/....</item>
<item name="colorOnSecondary">#color/....</item>
.....
</style>
Define the colors in res/values/colors.xml and res/values-night/colors.xml.
Then apply the theme in the Manifest:
<application ...
android:theme="#style/MyTheme" >
Check also the official guide.
The application I'm developing has several lists of items (including one in NavigationDrawer, one in a dialog, etc).
When the user press one item on the list on pre-lollipop smartphones, the background changes to a light blue color (including preferences on PreferenceActivity). On lollipop devices, however, the background changes to a light grey color with a beautiful ripple effect. What I want is to have the same light grey color for all devices (the ripple effect is not needed).
I've seem some posts of people styling ListView items setting backgrounds, but I need an approach to style all of them at once.
ps: my app uses Theme.AppCompat.Light.NoActionBar as base theme.
Create a file res/drawable/listitem_background.xml with the following content :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="#color/purple_dark" />
<item android:drawable="#android:color/transparent" />
</selector>
Replace the #color/purple_dark with the color of your choice.
Then, in your theme, add the following line :
<item name="android:activatedBackgroundIndicator">#drawable/listitem_background</item>
EDIT:
To mantain the lollipop effects and colors instead of placing the theme in the values folder, place it in the values-v22
Define a style resource file like this one
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme.AppCompat.Light.NoActionBar">
<item name="android:listViewStyle">#style/MyListViewStyle</item>
</style>
<style name="MyListViewStyle" parent="android:Widget.ListView">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<!-- Other stuff you need -->
</style>
</resources>
In your manifest set
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyTheme">
Now all of your ListView's will look like you define in style by default. You can of course change some of them if you want.
You can find a more complex, but very useful sample HERE
I am trying to change text color globally in my application, so by the instructions of http://developer.android.com/guide/topics/ui/themes.html I defined two themes in a xml file and set it in the manifest file. But the result was: some text change and major don't, I tried a lot of things, did a lot of research but I wasn't successful in find a way to do this. I observed that the text that changed the color was normally in TextView's that were in first level of the layouts. I am not sure, but I think second level and, so, and the ones that were in listviews didn't change. Can anyone give me a clue? Thanks in advance.
my theme file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Dark" parent="#android:style/Theme.Black.NoTitleBar">
<item name="android:textColor">#color/green</item>
</style>
<style name="Light" parent="#android:style/Theme.Light.NoTitleBar">
<item name="android:textColor">#color/yellow</item>
</style>
</resources>
Manifest file entry:
<application
android:name=".TestApplication"
android:icon="#drawable/test"
android:label="#string/app_name"
android:theme="#style/Dark">
I am trying to customize my styles.xml file to display the same background for each one of my activities.
I tried this code in my styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.MyTheme" parent="android:Theme.NoTitleBar">
<item name="android:background">#drawable/mybg</item>
</style>
</resources>
this worked pretty well untill I put a TextView in my activity.. Every View I put in my layout get the same background, and that is really annoying
What is the best practice to set only the background of the activity?
I think I have found my answer:
<item name="android:windowBackground">
fits all my needs
May be you can do it like this :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="yourstylename">
<item name="android:background">#drawable/mybg</item>
</style>
and set this style to all your activities root element in xml file.
I am developing one android application which use transparent activity.. I used the following link as reference but my problem is when i set background color as black i am not able see previous. i need transparent like this .
Thanks in Advance...
I had requirements to do the same thing in My Application, and I tried the above solution but unfortunately it didn't work for me.
After searching a lot for the same Issue, I have found one another solution That I would like to share here,
I have one Demo.java activity for top and Test.java Activity to show in background..So for that I have created one Style and applied to Demo.java ...and Magic..All Worked.!!
style.xml
<style name="Theme.D1NoTitleDim" parent="android:style/Theme.Translucent">
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:background">#android:color/transparent</item>
</style>
and in Manifest.xml
<activity android:name="Demo"
android:theme="#style/Theme.D1NoTitleDim"></activity>
Hope This will help Others Too.
Enjoy Coding.!!
And you must make the following changes in the target activity (let's say splash activity):
public class SplashScreenActivity extends Activity {}
Be sure to extend it to the activity
Try setting your background color to somewhat transparent, i.e. #55000000 or '#5000'. Create colors.xml in your res/values folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="background">#55000000</color>
</resources>
Then use the color in your theme style:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="YourTheme" parent="android:#Theme.Translucent">
<item name="android:windowBackground">#color/background</item>
</style>
</resources>
Key thing here is to set Theme.Translucent as parent of your style, I've tried same with Theme.Light and that didn't work.