Override activity's background that was set by theme - android

I currently have an Activity for my splash screen. The following is the way I apply a theme to the activity:
<application
...
<activity
android:name=".activities.SplashActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
SplashTheme is defined as follows:
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:background">#drawable/splash_background</item>
</style>
splash_backround.xml is defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:src="#drawable/login_background"
android:tileMode="disabled" />
</item>
<item>
<bitmap
android:src="#drawable/login_siren"
android:tileMode="disabled"
android:gravity="bottom|center_horizontal" />
</item>
<item android:top="120dp">
<bitmap
android:src="#drawable/splash_welcome"
android:tileMode="disabled"
android:gravity="top|center_horizontal" />
</item>
I am not calling setContentView() in onCreate() of my SplashActivity class, so thing being displayed is what is set by the SplashTheme.
A situation has come up where I want to display an AlertDialog if something fails to load upon Application.onCreate(). The splash screen is displaying at the time I use this to build, create and show the AlertDialog. However, when I show the AlertDialog, it gets assigned the background from the Activity's theme. Even if I define a custom style for the AlertDialog (via android:alertDialogTheme or android:alertDialogStyle) that explicitly defines a different background, it gets overruled by the background defined by the activity. As a result, I've changed SplashTheme to the following:
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/splash_background</item>
</style>
This now releases control of the alert dialog background as I am not explicitly setting android:background in my Activity theme. However, the window background does not account for the status bar (i.e. the background applies underneath the status bar as well). As a result, I get a bit of jump when the splash screen transitions to the next activity that uses this same background in its layout (due to accounting for the status bar). So, this solution still isn't working as I'd like.
If I do use the original SplashTheme (where I define android:background), is there a way to override the theme's defined android:background for an AlertDialog?

It is not possible to change a theme of activity once it is applied.
Is there a way to override the theme's defined android:background for an AlertDialog?
AlertDialog has a constructor, which accepts themeResId as an input. From docs of AlertDialog (Context context, int themeResId):
Creates an alert dialog that uses an explicit theme resource.
Create a custom theme, overriding android:background attribute, then create AlertDialog using newly created theme:
Dialog dialog = new AlertDialog(context, R.style.my_dialog_theme);

Related

Changing background color in a activity.

I used Android Studio to create basic activity using New -> Activity -> Basic Activity. But the activity background colour is black. I know it is to reduce battery consumption. But is there a way to change it to white colour, without changing style.
I tried to change the background colour of the root layout, but then it won't show hint text of edit text field. Because it's colour is white.
Then I tried to change theme from Material to Material Lite, then it wouldn't show the change when I run the app in the device.
Here is the activity declaration in AndroidManifest.xml
<activity
android:name=".SearchBus"
android:label="#string/title_activity_search_bus"
android:parentActivityName=".MapsActivity"
>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="uk.co.stableweb.iroute.MapsActivity" />
</activity>
In your styles.xml set the android:windowBackground attribute for whatever theme you are using for application or activity. You can find the theme you are using in your AndroidManifest.xml.
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowBackground">#color/somecolor</item>
</style>
</resources>
Note: If you have views in your layout that aren't transparent, it will block you from seeing the color you set here.
Did you try :android:background="#ffffff" i? It works for me

Activity background in Android

I have an activity with background colour black.
It has edittext and right after soft keyboard goes away, for a moment I see white area under keyboard; and then view gets resized.
This looks like blinking.
Question is, is it possible to set some kind of default background color for entire app? (Assuming that my activity already has background attribute)
windowSoftInputMode is also not an option
If you want to specify the background to your whole application, you may edit the app theme or add custom theme to your application manifest. But this will be overridden when background is specified in the xml layout.
Add custom theme in style.xml
<style name="MyTheme" parent="android:Theme">
<item name="android:background">#android:color/black</item>
</style>
Specify the theme in manifest as
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyTheme" >
Adding theme for each activity in manifest
<activity
android:name=".Login"
android:configChanges="orientation"
android:theme="#style/MyTheme>
</activity>
You can also modify the existing theme in style.xml which is already defined in manifest by changing the background as desired
<item name="android:background">#android:color/black</item>

Android - Disable dummy starting window of application

I want to know how to achieve this effect.
My app (default everything), when launcher icon is clicked, right away display some kind of a empty dummy window, where nothing is happening and then loads layout into it.
"Heavier" apps like YouTube, Drive, Dropbox etc. when started, seem to wait after launch, without showing that dummy window and load right into ready layout.
Any idea how to do this or where should I look into?
Thanks
//EDIT: this has nothing to do with something like loading database, where I should display progressBar, imho this has to do with stuff before activity exists.
I suggest you don't disable the dummy loading window using:
<style name="Theme.MyTheme" parent="android:style/Theme" >
....
<item name="android:windowDisablePreview">true</item>
....
</style>
because you could run into several problems. For example, if you try to animate an AlertDialog, enter animation will not work (personal experience).
The right solution is to set the theme of your application like to your main activity screen (same ActionBar style, same background color).
In my application after severals experiments i found the right solution for my needs.
(I have a main activity with blank background, no ActionBar, just full screen personal layout)
1 - styles.xml: add new style like this (remove ActionBar and set the same background color of my main activity)
<style name="LoadingTheme" parent="#android:style/Theme.Light.NoTitleBar.Fullscreen">
<item name="android:windowBackground">#color/white_smoke</item>
</style>
2 - AndroidManifest.xml: set my theme for main activity to "LoadingTheme"
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.company.appname.MainActivity"
android:label="#string/app_name"
android:theme="#style/LoadingTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
....
</application>
And finally i have full blank dummy loading window without ActionBar with soft MainActivity load and working animations.
Hope to be helpful.
<style name="Theme.MyTheme" parent="android:style/Theme" >
....
<item name="android:windowDisablePreview">true</item>
....
</style>
but use with caution
This will create a custom preview window for your activity.
Create a style in styles.xml like
<style name="MyTheme" parent="#android:style/Theme.Holo.NoActionBar">
<item name="android:windowBackground">#color/myBackgroundColor</item>
</style>
In your Manifest file use the theme created above.
For eg:
<activity android:name=".MyActivity"
android:label="#string/app_name"
android:theme="#style/MyTheme"
/>
In your activity change the background of the window back to null in onCreate()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setBackgroundDrawable(null);
setContentView(R.layout.main);
}
You can call the setContentView method anywhere you'd like in your activity. Before calling this method it will display a blank screen. If you wanted a splash screen, you can call the setContentView method within the onCreate method to display the splash screen view. Then when you're ready to display the "main" view you can use a LayoutInflater to display it. I also suggest using a ProgressDialog to show the user that information is loading.
Load your resources before setting the contentview. There will be a dummy window until you would not set view using setContentView.
EDIT
To avoid this screen. Make there changes to your code.
1) Do not do any heavy work in activity onCreate method. Run a separate thread for it.
2) Set content view right after the super.onCreate() line
super.onCreate(savedInstanceState);
setContentView (R.layout.splash_screen);
3) Apply following theme to your application.
Define theme in style
<resources>
<style name="Theme.MyTheme" parent="android:style/Theme" >
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowBackground">#drawable/any_default_background</item>
</style>
</resources>
In your Manifest
<application
....
android:theme="#style/Theme.MyTheme"
....
>
Thats It.

Android: Changing the windowBackground of PreferenceActivity causes weird effect

I want to add a background image to my preference activity.
I tried by creating custom Theme:
<style name="MyPreference" parent="android:Theme.Light.NoTitleBar">
<item name="android:windowBackground">#drawable/preference_background</item>
</style>
and in the Manifest:
<activity android:name="SettingsActivity" android:theme="#style/MyPreference">
...
</activity>
But for some reason that caused to the PreferenceScreen to have a transparent background, like that:
Any idea how can i overcome that?

Change the Color of Highlighted text in Android

I'm not sure that this is possible, perhaps someone can set me straight. I have an EditText View in an android application that has white text on a blue background. When the text is selected (via a long press and the edit dialog), I'd like have the highlight be white and change the text color to be black. Annoyingly though there does not seem to be a way to set the color of the text on highlight. You can set the highlight color using textColorHighlight, but not the text color, so setting a white highlight with white text results in a big white block.
It seems like it should be something trivial you can do declaratively in the xml, but though I've tried many combinations of styles and colors, I've not been able to change the color.
Checking other standard applications it seems that the text color never seems to change so I'm thinking this is something that's not easily done. I'd rather not have to subclass the EditText if at all possible just to something so simple. Am I missing something? Can this be done in the view xml?
Did you try #android:attr/textColorPrimaryInverse ?
Try:<item name="android:textColorHighlight">your_color</item>
in the style that you as your theme.
For e.g.
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">your_color1</item>
<item name="colorPrimaryDark">your_color2</item>
<item name="colorAccent">your_color3</item>
<item name="android:textColor">your_color4/item>
<item name="android:textColorHighlight">your_color</item>
</style>
then use this style as the theme for your activity in the manifest file. For e.g.-
<activity
android:name=".youractivity"
android:label=""
android:theme="#style/AppTheme.NoActionBar"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
As referred to by this SO post, you should be using a selector to change text colors like so:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Pressed State -->
<item android:state_pressed="true"
android:color="#FFFFFF" />
<!-- Focused State -->
<item android:state_focused="true"
android:color="#FFFFFF" />
<!-- Default State -->
<item android:color="#000000" />
</selector>
And then set your textColor property to #drawable/selector_name
Try with textColorHighlight - "Color of highlighted text." http://developer.android.com/reference/android/R.attr.html#textColorHighlight
Object mSelectionForegroundColorSpan;
#Override
protected void onSelectionChanged(int selStart, int selEnd) {
super.onSelectionChanged(selStart, selEnd);
if(mSelectionForegroundColorSpan == null){
mSelectionForegroundColorSpan = new ForegroundColorSpan(Color.GREEN);
}else{
getText().removeSpan(mSelectionForegroundColorSpan);
}
if(selStart > selEnd){
int swap = selStart;
selStart = selEnd;
selEnd = swap;
}
getText().setSpan(mSelectionForegroundColorSpan, selStart, selEnd, Spanned.SPAN_INTERMEDIATE);
}
Override the onSelectionChanged method of TextView, get the text and set a ForegroundColorSpan

Categories

Resources