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
Related
I just recently updated my Android Studio and ever since doing that the ThemeOverlay property is not showing on my buttons in the preview/design editor which it did prior to the update. However when I run the App on the Device the ThemeOverlay works. Below is some test code for the button. I have set the background attribute of the Button to Blue and the text of the button to Test Me
<LinearLayout
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=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Me"
android:background="#0000FF"
android:layout_marginTop="20dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark"/>
</LinearLayout>
The theme attribute is set to TheOverLay.AppCompat.Dark so that the text on the button is light but it's not showing in the design editor/preview in Android Studio. The text still shows as dark/Black. When I run this on a connected device it works fine.
Is this because of something that has changed with the current update?
You can set this inside your base theme:
<item name="android:colorButtonNormal">#color/some_color</item> which is global basically, all the buttons will have the color you defined inside your theme file.
On the other hand, I believe you cannot get the button theme from ThemeOverlay. Instead, create a new style and set the parent to Button.
<style name="CustomButton" parent="Widget.AppCompat.Button.Colored">
<item name="colorButtonNormal">#color/Red</item>
<item name="android:colorButtonNormal">#color/Red</item>
<item name="android:textColor">#color/White</item>
</style>
And you can apply the style as:
android:theme="#style/CustomButton"
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!
I had translucent activity backgrounds working on Android Wear 4.4W, by not specifying a theme, and setting the background in the activity layout to #BB000000.
Now I have Android Wear 5.0.1, the background is not translucent, and weird effects occur with elements of the foreground appearing in random places in the background, and jumping around - completely messed up.
I've tried to use a custom theme as follows:
<style name="Theme.Transparent" parent="android:Theme.DeviceDefault">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
I also tried using
android:theme="#android:style/Theme.Translucent"
Both with no luck - the same weird opaque effects.
My activity layout is as follows:
<android.support.wearable.view.BoxInsetLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#BB000000"
android:layout_height="match_parent"
android:layout_width="match_parent">
<FrameLayout
android:id="#+id/frame_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_box="left|bottom|right">
<android.support.wearable.view.WearableListView
android:id="#+id/wearable_list"
android:layout_height="match_parent"
android:layout_width="match_parent">
</android.support.wearable.view.WearableListView>
</FrameLayout>
</android.support.wearable.view.BoxInsetLayout>
Any ideas anyone?
Just tested in Android Wear 5.1.1
android:theme="#android:style/Theme.Translucent"
now gives a translucent background.
I am trying to have a transparent Activity, with a faint grey Color tint, over another activity. I am able to invoke the transparent activity, but I am not able to get the Greyish Tint. Please help me. Following is my code.
<style name="Theme.Transparent">
<item name="android:windowBackground">#drawable/transparent_background</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>
</style>
<drawable name="transparent_background">#FFFECA11</drawable>
No matter what colour value I provide for the drawable, it is not being reflected as the background. The definition in manifest
<activity
android:name=".DisplayHelpActivity"
android:label="#string/title_activity_display_help"
android:theme="#style/Theme.Transparent">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.INFO" />
</intent-filter>
</activity>
Can you please let me know wat is missing?
P.S. Please forget the color value. Its just random I used to test if it works.
I used the following as well
<item name="android:windowBackground">#color/transparent</item>
instead of the drawable, and in my color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="transparent">#80ffeeca</color>
</resources>
it still has no effect. Please help me.
I don't think it's a good idea to put activities on top of each other. If you want to show something on top of your normal activity, then using a FrameLayout is a posible solution:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_layout" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff"
android:id="#+id/underlying_layout" >
<!--Put the parts of your normal activity here -->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#88666666"
android:id="#+id/top_layout">
<!--Put the parts of the layout here,
what should be on top of your activity-->
</LinearLayout>
</FrameLayout>
Now if you want to make the top level stuff appear or disappear, then you can use the Views setVisibility() function like this:
View topLevelLayout = findViewById(R.id.top_layout);
//make it disappear with all its sub-Views
topLevelLayout.setVisibility(View.INVISIBLE);
//make it appear with all its sub-Views
topLevelLayout.setVisibility(View.VISIBLE);
UPDATE
I believe the problem lies within the value of the color itself.
#FFFECA11
The reason being that the color is broken down into four values, #AARRGGBB. The first two are the transparent values, and by using FF in your first two values, you are ensuring that it is not transparent. Try using the following value to get around 50% transparency:
#7FFECA11
For more information, see the following link: http://developer.android.com/guide/topics/resources/more-resources.html#Color
I have defined a custom theme, where I am drawing a dark gradient on my window background. My ListView background is set to be transparent, however whenever I scroll, the background color turns black, and then after scrolling has stopped, goes back to the gradient color. Why is this?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme is the default theme. -->
<style name="Theme" parent="android:Theme">
</style>
<!-- Variation on our application theme that has a translucent
background. -->
<style name="Theme.DarkGradient">
<item name="android:windowBackground">#drawable/dark_gradient</item>
</style>
</resources>
Your question is answered here: http://developer.android.com/resources/articles/listview-backgrounds.html
You just need to set the cacheColorHint in order to fix your problem. :-)
Just set cacheColorHint="#00000000" in your xml listView its a transparent color
<ListView android:id="#id/android:list" android:layout_width="320dp"
android:layout_height="fill_parent" android:layout_centerHorizontal="true"
android:divider="#color/whitetxtcolor"
android:layout_weight="2" android:drawSelectorOnTop="false"
android:layout_below="#id/new_layout"
android:cacheColorHint="#000000"
></ListView>
or U can do like this also
this.getListView.setCacheColorHint(0);