I am doing a simple lockscreen using the addview in windowmanager, although i successfully added the lockscreen view but before the view is added, a white annoying page being added before the view is displayed. i tried to set the view background to other color but it didn't affected anything so i think it might be something related to the window view, after changing background color in activity, delayed addview, it still appeared.
ps: when remove view when i called before finish(), then white view appeared, but when remove view in ondestroyed, it will delayed a bit but the white view will not appeared.
How can i remove/hide this white view when addView/removeView?
public static synchronized LockUtil getInstance(Activity act) {
if (mLockLayer == null) {
mLockLayer = new LockUtil(act);
}
return mLockLayer;
}
private LockUtil(Activity act) {
mActivty = act;
init();
}
private void init() {
isLocked = false;
if (null == mWindowManager) {
mWindowManager = ((WindowManager) mActivty.getSystemService(Context.WINDOW_SERVICE));
}
mLockViewLayoutParams = new WindowManager.LayoutParams();
mLockViewLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
mLockViewLayoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
mLockViewLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
mLockViewLayoutParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
mLockViewLayoutParams.windowAnimations = android.R.style.Animation_Translucent;
}
public synchronized void lock() {
if (mLockView != null && !isLocked) {
mWindowManager.addView(mLockView, mLockViewLayoutParams);
isLocked = true;
}
}
public synchronized void unlock() {
if (mWindowManager != null && isLocked) {
try{
mWindowManager.removeView(mLockView);
isLocked = false;
}catch(IllegalArgumentException e){
Logger.e("LockUtil unlock failed:"+e.getMessage());
}
}
mActivty = null;
}
After make some of the research, the white background is the background color for the activity (window) so in order to remove it, we just need to set the color to transparent in the theme.
<style name="Theme.Transparent" parent="#style/Theme.AppCompat.Light.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</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>
First of all, this question asks a very similar question. However, my question has a subtle difference.
What I'd like to know is whether it is possible to programmatically change the colorPrimary attribute of a theme to an arbitrary color?
So for example, we have:
<style name="AppTheme" parent="android:Theme.Material.Light">
<item name="android:colorPrimary">#ff0000</item>
<item name="android:colorAccent">#ff0000</item>
</style>
At runtime, the user decides he wants to use #ccffff as a primary color. Ofcourse there's no way I can create themes for all possible colors.
I don't mind if I have to do hacky stuff, like relying on Android's private internals, as long as it works using the public SDK.
My goal is to eventually have the ActionBar and all widgets like a CheckBox to use this primary color.
Themes are immutable, you can't.
I read the comments about contacts app and how it use a theme for each contact.
Probably, contacts app has some predefine themes (for each material primary color from here: http://www.google.com/design/spec/style/color.html).
You can apply a theme before a the setContentView method inside onCreate method.
Then the contacts app can apply a theme randomly to each user.
This method is:
setTheme(R.style.MyRandomTheme);
But this method has a problem, for example it can change the toolbar color, the scroll effect color, the ripple color, etc, but it cant change the status bar color and the navigation bar color (if you want to change it too).
Then for solve this problem, you can use the method before and:
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setNavigationBarColor(getResources().getColor(R.color.md_red_500));
getWindow().setStatusBarColor(getResources().getColor(R.color.md_red_700));
}
This two method change the navigation and status bar color.
Remember, if you set your navigation bar translucent, you can't change its color.
This should be the final code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.MyRandomTheme);
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setNavigationBarColor(getResources().getColor(R.color.myrandomcolor1));
getWindow().setStatusBarColor(getResources().getColor(R.color.myrandomcolor2));
}
setContentView(R.layout.activity_main);
}
You can use a switch and generate random number to use random themes, or, like in contacts app, each contact probably has a predefine number associated.
A sample of theme:
<style name="MyRandomTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/myrandomcolor1</item>
<item name="colorPrimaryDark">#color/myrandomcolor2</item>
<item name="android:navigationBarColor">#color/myrandomcolor1</item>
</style>
You can use Theme.applyStyle to modify your theme at runtime by applying another style to it.
Let's say you have these style definitions:
<style name="DefaultTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/md_lime_500</item>
<item name="colorPrimaryDark">#color/md_lime_700</item>
<item name="colorAccent">#color/md_amber_A400</item>
</style>
<style name="OverlayPrimaryColorRed">
<item name="colorPrimary">#color/md_red_500</item>
<item name="colorPrimaryDark">#color/md_red_700</item>
</style>
<style name="OverlayPrimaryColorGreen">
<item name="colorPrimary">#color/md_green_500</item>
<item name="colorPrimaryDark">#color/md_green_700</item>
</style>
<style name="OverlayPrimaryColorBlue">
<item name="colorPrimary">#color/md_blue_500</item>
<item name="colorPrimaryDark">#color/md_blue_700</item>
</style>
Now you can patch your theme at runtime like so:
getTheme().applyStyle(R.style.OverlayPrimaryColorGreen, true);
The method applyStylehas to be called before the layout gets inflated! So unless you load the view manually you should apply styles to the theme before calling setContentView in your activity.
Of course this cannot be used to specify an arbitrary color, i.e. one out of 16 million (2563) colors. But if you write a small program that generates the style definitions and the Java code for you then something like one out of 512 (83) should be possible.
What makes this interesting is that you can use different style overlays for different aspects of your theme. Just add a few overlay definitions for colorAccent for example. Now you can combine different values for primary color and accent color almost arbitrarily.
You should make sure that your overlay theme definitions don't accidentally inherit a bunch of style definitions from a parent style definition. For example a style called AppTheme.OverlayRed implicitly inherits all styles defined in AppTheme and all these definitions will also be applied when you patch the master theme. So either avoid dots in the overlay theme names or use something like Overlay.Red and define Overlay as an empty style.
I've created some solution to make any-color themes, maybe this can be useful for somebody. API 9+
1. first create "res/values-v9/" and put there this file: styles.xml
and regular "res/values" folder will be used with your styles.
2. put this code in your res/values/styles.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#000</item>
<item name="colorPrimaryDark">#000</item>
<item name="colorAccent">#000</item>
<item name="android:windowAnimationStyle">#style/WindowAnimationTransition</item>
</style>
<style name="AppThemeDarkActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#000</item>
<item name="colorPrimaryDark">#000</item>
<item name="colorAccent">#000</item>
<item name="android:windowAnimationStyle">#style/WindowAnimationTransition</item>
</style>
<style name="WindowAnimationTransition">
<item name="android:windowEnterAnimation">#android:anim/fade_in</item>
<item name="android:windowExitAnimation">#android:anim/fade_out</item>
</style>
</resources>
3. in to AndroidManifest:
<application android:theme="#style/AppThemeDarkActionBar">
4. create a new class with name "ThemeColors.java"
public class ThemeColors {
private static final String NAME = "ThemeColors", KEY = "color";
#ColorInt
public int color;
public ThemeColors(Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
String stringColor = sharedPreferences.getString(KEY, "004bff");
color = Color.parseColor("#" + stringColor);
if (isLightActionBar()) context.setTheme(R.style.AppTheme);
context.setTheme(context.getResources().getIdentifier("T_" + stringColor, "style", context.getPackageName()));
}
public static void setNewThemeColor(Activity activity, int red, int green, int blue) {
int colorStep = 15;
red = Math.round(red / colorStep) * colorStep;
green = Math.round(green / colorStep) * colorStep;
blue = Math.round(blue / colorStep) * colorStep;
String stringColor = Integer.toHexString(Color.rgb(red, green, blue)).substring(2);
SharedPreferences.Editor editor = activity.getSharedPreferences(NAME, Context.MODE_PRIVATE).edit();
editor.putString(KEY, stringColor);
editor.apply();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) activity.recreate();
else {
Intent i = activity.getPackageManager().getLaunchIntentForPackage(activity.getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
activity.startActivity(i);
}
}
private boolean isLightActionBar() {// Checking if title text color will be black
int rgb = (Color.red(color) + Color.green(color) + Color.blue(color)) / 3;
return rgb > 210;
}
}
5. MainActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new ThemeColors(this);
setContentView(R.layout.activity_main);
}
public void buttonClick(View view){
int red= new Random().nextInt(255);
int green= new Random().nextInt(255);
int blue= new Random().nextInt(255);
ThemeColors.setNewThemeColor(MainActivity.this, red, green, blue);
}
}
To change color, just replace Random with your RGB, Hope this helps.
There is a complete example: ColorTest.zip
You can have a look at this GitHub project from Rumit Patel.
I used the Dahnark's code but I also need to change the ToolBar background:
if (dark_ui) {
this.setTheme(R.style.Theme_Dark);
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setNavigationBarColor(getResources().getColor(R.color.Theme_Dark_primary));
getWindow().setStatusBarColor(getResources().getColor(R.color.Theme_Dark_primary_dark));
}
} else {
this.setTheme(R.style.Theme_Light);
}
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
if(dark_ui) {
toolbar.setBackgroundColor(getResources().getColor(R.color.Theme_Dark_primary));
}
You can change define your own themes, or customize existing android themes in res > values > themes, find where it says primary color and point it to the color defined in color.xml you want
<style name="Theme.HelloWorld" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/my_color</item>
<item name="colorPrimaryVariant">#color/my_color</item>
<item name="colorOnPrimary">#color/white</item>
from an activity you can do:
getWindow().setStatusBarColor(i color);
You cannot change the color of colorPrimary, but you can change the theme of your application by adding a new style with a different colorPrimary color
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
</style>
<style name="AppTheme.NewTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorOne</item>
<item name="colorPrimaryDark">#color/colorOneDark</item>
</style>
and inside the activity set theme
setTheme(R.style.AppTheme_NewTheme);
setContentView(R.layout.activity_main);
USE A TOOLBAR
You can set a custom toolbar item color dynamically by creating a custom toolbar class:
package view;
import android.app.Activity;
import android.content.Context;
import android.graphics.ColorFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.support.v7.internal.view.menu.ActionMenuItemView;
import android.support.v7.widget.ActionMenuView;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomToolbar extends Toolbar{
public CustomToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}
public CustomToolbar(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public CustomToolbar(Context context) {
super(context);
// TODO Auto-generated constructor stub
ctxt = context;
}
int itemColor;
Context ctxt;
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
Log.d("LL", "onLayout");
super.onLayout(changed, l, t, r, b);
colorizeToolbar(this, itemColor, (Activity) ctxt);
}
public void setItemColor(int color){
itemColor = color;
colorizeToolbar(this, itemColor, (Activity) ctxt);
}
/**
* Use this method to colorize toolbar icons to the desired target color
* #param toolbarView toolbar view being colored
* #param toolbarIconsColor the target color of toolbar icons
* #param activity reference to activity needed to register observers
*/
public static void colorizeToolbar(Toolbar toolbarView, int toolbarIconsColor, Activity activity) {
final PorterDuffColorFilter colorFilter
= new PorterDuffColorFilter(toolbarIconsColor, PorterDuff.Mode.SRC_IN);
for(int i = 0; i < toolbarView.getChildCount(); i++) {
final View v = toolbarView.getChildAt(i);
doColorizing(v, colorFilter, toolbarIconsColor);
}
//Step 3: Changing the color of title and subtitle.
toolbarView.setTitleTextColor(toolbarIconsColor);
toolbarView.setSubtitleTextColor(toolbarIconsColor);
}
public static void doColorizing(View v, final ColorFilter colorFilter, int toolbarIconsColor){
if(v instanceof ImageButton) {
((ImageButton)v).getDrawable().setAlpha(255);
((ImageButton)v).getDrawable().setColorFilter(colorFilter);
}
if(v instanceof ImageView) {
((ImageView)v).getDrawable().setAlpha(255);
((ImageView)v).getDrawable().setColorFilter(colorFilter);
}
if(v instanceof AutoCompleteTextView) {
((AutoCompleteTextView)v).setTextColor(toolbarIconsColor);
}
if(v instanceof TextView) {
((TextView)v).setTextColor(toolbarIconsColor);
}
if(v instanceof EditText) {
((EditText)v).setTextColor(toolbarIconsColor);
}
if (v instanceof ViewGroup){
for (int lli =0; lli< ((ViewGroup)v).getChildCount(); lli ++){
doColorizing(((ViewGroup)v).getChildAt(lli), colorFilter, toolbarIconsColor);
}
}
if(v instanceof ActionMenuView) {
for(int j = 0; j < ((ActionMenuView)v).getChildCount(); j++) {
//Step 2: Changing the color of any ActionMenuViews - icons that
//are not back button, nor text, nor overflow menu icon.
final View innerView = ((ActionMenuView)v).getChildAt(j);
if(innerView instanceof ActionMenuItemView) {
int drawablesCount = ((ActionMenuItemView)innerView).getCompoundDrawables().length;
for(int k = 0; k < drawablesCount; k++) {
if(((ActionMenuItemView)innerView).getCompoundDrawables()[k] != null) {
final int finalK = k;
//Important to set the color filter in seperate thread,
//by adding it to the message queue
//Won't work otherwise.
//Works fine for my case but needs more testing
((ActionMenuItemView) innerView).getCompoundDrawables()[finalK].setColorFilter(colorFilter);
// innerView.post(new Runnable() {
// #Override
// public void run() {
// ((ActionMenuItemView) innerView).getCompoundDrawables()[finalK].setColorFilter(colorFilter);
// }
// });
}
}
}
}
}
}
}
then refer to it in your layout file. Now you can set a custom color using
toolbar.setItemColor(Color.Red);
Sources:
I found the information to do this here: How to dynamicaly change Android Toolbar icons color
and then I edited it, improved upon it, and posted it here: GitHub:AndroidDynamicToolbarItemColor
This is what you CAN do:
write a file in drawable folder, lets name it background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="?attr/colorPrimary"/>
</shape>
then set your Layout's (or what so ever the case is) android:background="#drawable/background"
on setting your theme this color would represent the same.
I have an application that uses a preference activity to set some user settings. I been trying to figure this out all day. I am trying to theme the alert dialog when an user presses an Edit Text Preference object. A dialog opens up and the user can set the shared preference. The dialog pops up:
I want the text green. I want the divider green. The line and cursor green.
This is what I have so far.
<style name="CustomDialogTheme" parent="#android:style/Theme.Dialog">
<item name="android:background">#color/text_green</item>
<item name="android:textColor">#color/text_green</item>
</style>
Can someone point me in the right direction or maybe share some code. I am at lost. I've been surfing the net to find something most of the day. Thanks in advance.
If you don't want to create a custom layout or use a third party library, you can subclass EditTextPreference, then access each View you want to edit by using Resources.getIdentifier then using Window.findViewById. Here's a quick example.
public class CustomDialogPreference extends EditTextPreference {
public CustomDialogPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* {#inheritDoc}
*/
#Override
protected void showDialog(Bundle state) {
super.showDialog(state);
final Resources res = getContext().getResources();
final Window window = getDialog().getWindow();
final int green = res.getColor(android.R.color.holo_green_dark);
// Title
final int titleId = res.getIdentifier("alertTitle", "id", "android");
final View title = window.findViewById(titleId);
if (title != null) {
((TextView) title).setTextColor(green);
}
// Title divider
final int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
final View titleDivider = window.findViewById(titleDividerId);
if (titleDivider != null) {
titleDivider.setBackgroundColor(green);
}
// EditText
final View editText = window.findViewById(android.R.id.edit);
if (editText != null) {
editText.setBackground(res.getDrawable(R.drawable.apptheme_edit_text_holo_light));
}
}
}
Implementation
Replace <EditTextPreference.../> with <path_to_CustomDialogPreference.../> in your xml.
Note
I used Android Holo Colors to create the background for the EditText.
You can build your custom layout for your own dialog theme using your own customized components or you can use external libs, for example android-styled-dialogs
So in this case use can customize dialogs as you want:
<style name="DialogStyleLight.Custom">
<!-- anything can be left out: -->
<item name="titleTextColor">#color/dialog_title_text</item>
<item name="titleSeparatorColor">#color/dialog_title_separator</item>
<item name="messageTextColor">#color/dialog_message_text</item>
<item name="buttonTextColor">#color/dialog_button_text</item>
<item name="buttonSeparatorColor">#color/dialog_button_separator</item>
<item name="buttonBackgroundColorNormal">#color/dialog_button_normal</item>
<item name="buttonBackgroundColorPressed">#color/dialog_button_pressed</item>
<item name="buttonBackgroundColorFocused">#color/dialog_button_focused</item>
<item name="dialogBackground">#drawable/dialog_background</item>
</style>
I want to skin my Android App with multiple Color Themes.
This Guides/Tools helped me to generate the basic theme.xml and style.xml
http://android-holo-colors.com
http://jgilfelt.github.io/android-actionbarstylegenerator
http://blog.stylingandroid.com/archives/1240
But i cant figure it out to style the PopupMenu and AlertDialog Header?
I want to change only the Color to streamline with my App Theme!
Example:
to look like
I’m using Theme.Holo and Android SDK 21.1 RC3/Tools 16.0. I've tried a lot with styles and themes but nothing worked
public class AlertDialogThemed extends AlertDialog {
public AlertDialogThemed(Context context) {
super(context);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Resources resources = getContext().getResources();
final int color = resources.getColor(R.color.dialog_color);
final View title = findViewById(resources.getIdentifier("alertTitle", "id", "android"));
if (title != null) {
((TextView) title).setTextColor(color);
}
final View titleDivider = findViewById(resources.getIdentifier("titleDivider", "id", "android"));
if (titleDivider != null) {
titleDivider.setBackgroundColor(color);
}
}
}
In my android app I have a custom dialog box. I want to set the height of dialog's Title bar. My style is as follows:
<resources>
<style name="customDialogStyle" parent="android:Theme.Dialog">
<item name="android:background">#04a9ee</item>
<item name="android:height">5dp</item>
</style>
</resources>
But there is no effect of "height" attribute on title bar. So, how can the height of custom dialog's title bar can be changed ?
Yeh I just check, you want to use "android:layout_height" other heights you can use also like: "android:minHeight"
This works for me
Your theme:
<resources>
<style name="MyDialog" parent="android:Theme.Holo.Dialog">
.......
</style>
</resources>
Your Custom Dialog Class:
public class CustomDialog extends Dialog
{
public CustomDialog(Context context, int theme) {
super(context, theme);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...........
Resources res = getContext().getResources();
int titleId = res.getIdentifier("title", "id", "android");
View title = findViewById(titleId);
if (title != null) {
title.getLayoutParams().height = 5; // your height
}
}
}
Create dialog and show in your code:
CustomDialog customDialog = new CustomDialog(this, R.style.MyDialog);
customDialog.show();