Set an image as a background in an android application [duplicate] - android

This question already has answers here:
set background Image to whole application in android
(2 answers)
Closed 6 years ago.
How to Set an image as a background for whole activities in an android application? ( I know how to set it for each activity. I want to know is there any way to write one line of code that can affect all activities?)

You can do this:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowBackground">#drawable/PICTURE_NAME</item>
</style>

define it in your theme in style.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:background">#drawable/</item>
</style>

Put your image file .png in the drawables file and do this in all your xml layouts for all your activities
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:clipToPadding="false"
android:paddingBottom="4dp"
android:background="#drawable/yourImage"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>

Well, depending on how many activities you have, you would need to set background attribute to each of the activities' root element like this:
<LinearLayout
...
...
android:background="#drawable/background_image"
</LinearLayout>
I have used LinearLayout as an example above. You should replace it with whatever is your root element in your activity layout file!
I hope this helps!

Related

Change the default color of main activity when it starts, The starting and opening default color

When user opens any application by clicking on app icon, the app starts with mainactivity.java. and its xml layout is shown to user.
I am making a splash screen with simple background color and text. But what my problem is, when it starts it shows a default layout color for a very little fraction of time and then it draws my given color. I do not want default color to be there even for that small fraction of time.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rushi.demon.MainActivity"
android:background="#color/SplashScreen" >
<RelativeLayout
android:id="#+id/SplashScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/SplashScreen">
<TextView
android:id="#+id/SplashText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="#string/splash_name"
android:textColor="#color/SplashText"
android:textSize="40sp"
android:textStyle="bold"/>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
The SplashScreen color is shown after a lag and starting default color is shown for little time, which i do not want. I want to open this activity with my set color only from starting.
Add this to your AppTheme (theme of MainActivity) ..
<item name="android:windowDisablePreview">true</item>
Edit
Go to app -> res -> values -> styles.xml open styles.xml there you do this to the theme you have..
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowDisablePreview">true</item> //add this line
</style>
The best way to achieve this is well described here, By this way Splash Activity will start instantly
Refer this: https://medium.com/#ssaurel/create-a-splash-screen-on-android-the-right-way-93d6fb444857

Android find default background color

I have an app with a simple recyclerview.
The layout is declared as follow
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/contentView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/some_id" />
</RelativeLayout>
I notice that on different devices, the recyclerView actually had a different background. In newer devices, the background is just white. On older devices, the background is light gray.
In android studio, the background color is blank in the design view.
So my question is, where does this gray color come from? How can I change it universally to white?
I can obviously just add background:white to this particular view. But is there a way to overwrite the system default?
Android default background color
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:colorBackground">
</RelativeLayout
use
android:background="?android:colorBackground"
What you are seeing is actually the background of the activity, not the recyclerview, which is transparent by default. The color did change a view time per Android version.
You can override this in your app theme.
First define the color in values/colors.xml
<resources>
<color name="background">#FF0000 </color>
</resources>
Create a themes.xml file in res/values that references that color:
<resources>
<style name="MyTheme" parent="#android:style/Theme.Light">
<item name="android:windowBackground">#color/background</item>
</style>
</resources>
and then in your AndroidManifest.xml specify this as
the theme for your activities to use.
<activity
android:name=".MyActivity"
android:theme="#style/MyTheme" />
see https://stackoverflow.com/a/10157077/4623782
it's because of theme. specify your theme in res/values/style.xml. or set manually in view definition.
<android.support.v7.widget.RecyclerView
android:id="#+id/contentView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/some_id"
android:background="#android:color/white"
/>
it will give you white background on all device
The color of the background which is set by default in any android studio project is #fafafa

Style displayed in emulator but not on my device

I've following style.xml in res/values/ folder:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:colorBackground">#000000</item>
</style>
and the following layout for my main activity, where I've used style="#style/AppTheme":
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
style="#style/AppTheme" // here I've used It.
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.kaushal28.wakeupplease.MainActivity">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="#+id/startAlarm" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/stop"
android:text="Stop"/>
</LinearLayout>
</RelativeLayout>
I've device with Android 4.2.2 Jelly Bean and emulator has latest Marsh mellow. I think the problem is because of this difference only. I've minimum SDK version 15 for this project.
I tried to change targetSDKVersion to 15 in Build.Gradle. But it couldn't solve my problem.
What should be displayed:
What is displaying:
I'm expecting Black color in background.
Try to use android:windowBackground instead of android:colorBackground
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowBackground">#android:color/black</item>
</style>
Why?
By your screen, I saw that you are trying to set window background. So, the best attribute to that is android:windowBackground.
Your AppTheme is not overriding android:windowBackground.. it was only overriding android:colorBackground.
Since you are not overriding android:windowBackground, Android will use the color defined in your parent theme (Theme.AppCompat.Light.DarkActionBar).
So, Android was using:
<item name="colorBackgroundFloating">#color/background_floating_material_light</item>
and
<color name="background_material_light">#color/material_grey_50</color>
<color name="material_grey_50">#fffafafa</color>
It is almost a white color.
Device vs Studio
I'll be honest.. I'm not totally sure why your device was ignoring android:colorBackground while emulator was using it...
Probably, is the API version or probably, it is just a normal behavior since your device shows the theme applied to an Activity and inside a whole context.. Your device will be always a little bit different from Android Studio Layout Preview tool.
For any instance, I recommend to use android:windowBackground if you want to sent whole window background...
android:colorBackground can affects not only the background of the main window but also of all the components e.g. dialogs unless you override it in the component layout. (this part was retrieved from here)

Android material design - Creating Tool Bar

I am trying to follow this tutorial to create material design tool bar. I am at step 7 where my activity should have a color tool bar but my app looks like
for reference, here are my style.xml, color.xml, tool_bar.xml files
style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:colorPrimary">#color/ColorPrimary</item>
<item name="android:colorPrimaryDark">#color/ColorPrimaryDark</item>
<!-- Customize your theme here. -->
</style>
</resources>
There was another style.xml in the same folder which has (21) next to it. I didn't make any changes to it though.
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ColorPrimary">#FF5722</color>
<color name="ColorPrimaryDark">#E64A19</color>
</resources>
tool_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/ColorPrimary"
android:elevation="4dp">
</android.support.v7.widget.Toolbar>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
UPDATE
If i make changes in stylev21.xml file, I can see my app looks like whats in the tutorial. But if leave style-v21.xml unchanged and make changes in style.xml I don't see those changes. What style*.xml file shall i edit ?
Purpose of having different styles.xml is that you can add changes specific to the Android version. But then you need to maintain same changes in both the files.
If you are testing your application on device having Android version 5.0 lollipop then style defined in v21/styles.xml will reflect in your application. Otherwise common styles.xml
If you want the same output as displayed in tutorial then I would suggest few changes.
Edit your activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
P.S. I have just removed margin from RelativeLayout.
Edit both styles.xml and define style which contain no Action bar. E.g. "Theme.AppCompat.Light.NoActionBar"
After doing these changes your app will work perfectly on all devices.
I hope it help.
Change your theme to Theme.AppCompat.Light.DarkActionBar instead of NoActionBar.
Then you don't need to add add it in your layout XML file
Also change android:colorPrimary to colorPrimary (take off android:) and android:colorPrimaryDark to colorPrimaryDark.
Your image has the three dots for the context menu , its still showing the action bar . You must have the following line for tool bar to take effect
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
setSupportActionBar(toolbar); // Setting toolbar as the ActionBar with setSupportActionBar() call
}
Make your style.xml like this-
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/ColorPrimary</item>
<item name="colorPrimaryDark">#color/ColorPrimaryDark</item>
<!-- Customize your theme here. -->
</style>
</resources>
Reason - AppCompat search for text without "android:"

Theme background

I need to set a background color to every activitity in my app. I would prefer to set it globally. For this reason I change the style setted as application theme.
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:background">#color/app_bg</item>
</style>
</resources>
The problem is that now all ImageViews and TextViews that have not a specified Background get the app background and not the background of parent Layout, for example
<RelativeLayout
android:id="#+id/mmc_rl"
android:layout_width="match_parent"
android:layout_height="#dimen/mm_central_height"
android:background="#color/main_menu_central_bg">
<ImageView
android:id="#+id/mmc_iv_goto"
android:layout_width="#dimen/mm_central_height"
android:layout_height="#dimen/mm_central_height"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:src="#drawable/button_goto_big3" />
</RelativeLayout>
The ImageView has the background app_bg and not main_menu_central_bg as expected. How can I solve this problem?
include this line on the ImageView xml:
android:background="#android:color/transparent"

Categories

Resources