Related
I am having this issue where I am using the Android's Holo theme on a tablet project. However, I have a fragment on screen which has a white background. I am adding an EditText component on this fragment. I've tried to override the theme by setting the background of the Holo.Light theme resources. However, my text cursor (carat) remains white and hence, invisible on screen (I can spot it faintly in the edittext field..).
Does anyone know how I can get EditText to use a darker cursor color? I've tried setting the style of the EditText to "#android:style/Widget.Holo.Light.EditText" with no positive result.
Setting the android:textCursorDrawable attribute to #null should result in the use of android:textColor as the cursor color.
Attribute "textCursorDrawable" is available in API level 12 and higher
In Layout
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textCursorDrawable="#drawable/color_cursor"
/>
Then create drawalble xml: color_cursor
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<size android:width="3dp" />
<solid android:color="#FFFFFF" />
</shape>
You have a white color cursor on EditText property.
It appears as if all the answers go around the bushes.
In your EditText, use the property:
android:textCursorDrawable="#drawable/black_cursor"
and add the drawable black_cursor.xml to your resources, as follows:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<size android:width="1dp" />
<solid android:color="#000000"/>
</shape>
This is also the way to create more diverse cursors, if you need.
There is a new way to change cursor color in latest Appcompact v21
Just change colorAccent in style like this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette-->
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#088FC9</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#088FC9</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<!-- THIS IS WHAT YOU'RE LOOKING FOR -->
<item name="colorAccent">#0091BC</item>
</style>
Then apply this style on your app theme or activities.
Update: this way only works on API 21+
Update 2: I'm not sure the minimum android version that it can work. Tested by android version:
2.3.7 - didn't work
4.4.4 - worked
5.0 - worked
5.1 - worked
I found the answer :)
I've set the Theme's editText style to:
<item name="android:editTextStyle">#style/myEditText</item>
Then I've used the following drawable to set the cursor:
`
<style name="myEditText" parent="#android:style/Widget.Holo.Light.EditText">
<item name="android:background">#android:drawable/editbox_background_normal</item>
<item name="android:textCursorDrawable">#android:drawable/my_cursor_drawable</item>
<item name="android:height">40sp</item>
</style>
`
android:textCursorDrawable is the key here.
For anyone that needs to set the EditText cursor color dynamically, below you will find two ways to achieve this.
First, create your cursor drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ff000000" />
<size android:width="1dp" />
</shape>
Set the cursor drawable resource id to the drawable you created (https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564">source)):
try {
Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
f.setAccessible(true);
f.set(yourEditText, R.drawable.cursor);
} catch (Exception ignored) {
}
To just change the color of the default cursor drawable, you can use the following method:
public static void setCursorDrawableColor(EditText editText, int color) {
try {
Field fCursorDrawableRes =
TextView.class.getDeclaredField("mCursorDrawableRes");
fCursorDrawableRes.setAccessible(true);
int mCursorDrawableRes = fCursorDrawableRes.getInt(editText);
Field fEditor = TextView.class.getDeclaredField("mEditor");
fEditor.setAccessible(true);
Object editor = fEditor.get(editText);
Class<?> clazz = editor.getClass();
Field fCursorDrawable = clazz.getDeclaredField("mCursorDrawable");
fCursorDrawable.setAccessible(true);
Drawable[] drawables = new Drawable[2];
Resources res = editText.getContext().getResources();
drawables[0] = res.getDrawable(mCursorDrawableRes);
drawables[1] = res.getDrawable(mCursorDrawableRes);
drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN);
drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN);
fCursorDrawable.set(editor, drawables);
} catch (final Throwable ignored) {
}
}
Late to the party,Here's is my answer,
This is for the people who are not looking to change the colorAccent in their parent theme,but wants to change EditText attributes!
This answer demos how to change ......
Bottom line color
Cursor color
Cursor pointer color (I used my custom image).......... of EditText using style applied to the Activity theme.
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hey" />
Example:
<style name="AppTheme.EditText" parent="#style/Widget.AppCompat.EditText">
<item name="android:textColor">#color/white</item>
<item name="android:textColorHint">#8AFFFFFF</item>
<item name="android:background">#drawable/edit_text_background</item> // background (bottom line at this case)
<item name="android:textCursorDrawable">#color/white</item> // Cursor
<item name="android:textSelectHandle">#drawable/my_white_icon</item> // For pointer normal state and copy text state
<item name="android:textSelectHandleLeft">#drawable/my_white_icon</item>
<item name="android:textSelectHandleRight">#drawable/my_white_icon</item>
</style>
Now create a drawable(edit_text_background) add a resource xml for the background!You can customize as you want!
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="0dp"
android:left="-3dp"
android:right="-3dp"
android:top="-3dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#color/white"/>
</shape>
</item>
</layer-list>
Now as you did set this style in your Activity theme.
Example :
In your Activity you have a theme,set this custom editText theme to that.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Your Theme data -->
<item name="editTextStyle">#style/AppTheme.EditText</item> // inculude this
</style>
Edittext cursor color you want changes your color.
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textCursorDrawable="#drawable/color_cursor"
/>
Then create drawalble xml: color_cursor
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<size android:width="3dp" />
<solid android:color="#FFFFFF" />
</shape>
Wow I am real late to this party but it has had activity 17 days ago
It would seam we need to consider posting what version of Android we are using for an answer so as of now this answer works with Android 2.1 and above
Go to RES/VALUES/STYLES and add the lines of code below and your cursor will be black
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!--<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">-->
<!-- Customize your theme here. -->
<item name="colorControlActivated">#color/color_Black</item>
<!--Sets COLOR for the Cursor in EditText -->
</style>
You will need a this line of code in your RES/COLOR folder
<color name="color_Black">#000000</color>
Why post this late ? It might be nice to consider some form of categories for the many headed monster Android has become!
For me I modified both the AppTheme and a value colors.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorControlNormal">#color/yellow</item>
<item name="colorAccent">#color/yellow</item>
</style>
Here is the colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="yellow">#B7EC2A</color>
</resources>
I took out the android:textCursorDrawable attribute to #null that I placed inside the editText style. When I tried using this, the colors would not change.
Use this
android:textCursorDrawable="#color/white"
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimary</item>
<item name="colorAccent">#color/colorAccent</item> -- change this one
</style>
Go to styles.xml and change the color accent and this will influence the cursor from the edittext box
The only valid answer should be to change the theme of the activity:
<item name="colorAccent">#000000</item>
You should not use the android:textCursorDrawable to #null because this only concerns the cursor itself but not the pin below the cursor if you want to drag that cursor. The theming solution is the most serious one.
Here #Jared Rummler's programatic setCursorDrawableColor() version adapted to work also on Android 9 Pie.
#SuppressWarnings({"JavaReflectionMemberAccess", "deprecation"})
public static void setCursorDrawableColor(EditText editText, int color) {
try {
Field cursorDrawableResField = TextView.class.getDeclaredField("mCursorDrawableRes");
cursorDrawableResField.setAccessible(true);
int cursorDrawableRes = cursorDrawableResField.getInt(editText);
Field editorField = TextView.class.getDeclaredField("mEditor");
editorField.setAccessible(true);
Object editor = editorField.get(editText);
Class<?> clazz = editor.getClass();
Resources res = editText.getContext().getResources();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Field drawableForCursorField = clazz.getDeclaredField("mDrawableForCursor");
drawableForCursorField.setAccessible(true);
Drawable drawable = res.getDrawable(cursorDrawableRes);
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
drawableForCursorField.set(editor, drawable);
} else {
Field cursorDrawableField = clazz.getDeclaredField("mCursorDrawable");
cursorDrawableField.setAccessible(true);
Drawable[] drawables = new Drawable[2];
drawables[0] = res.getDrawable(cursorDrawableRes);
drawables[1] = res.getDrawable(cursorDrawableRes);
drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN);
drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN);
cursorDrawableField.set(editor, drawables);
}
} catch (Throwable t) {
Log.w(TAG, t);
}
}
We can do it in meterial theme as following:
<style name="Theme.App" parent="Theme.MaterialComponents.DayNight.NoActionBar">
...
<item name="android:colorControlNormal">#ff0000</item>
<item name="android:colorControlActivated">#ff0000</item>
<item name="android:colorControlHighlight">#ff0000</item>
...
</style>
And if you want to change checkbox and radio colors too, add the following line:
<item name="colorAccent">#ff0000</item>
I've tested in Android API 21+
that's called colorAccent in Android.
go to res -> values -> styles.xml add
<item name="colorAccent">#FFFFFF</item>
if not exists.
editcolor.xml
android:textCursorDrawable="#drawable/editcolor"
In xml file set color code of edittext background color
After a lot of time spent trying all these technique in a Dialog, I finally had this idea : attach the theme to the Dialog itself and not to the TextInputLayout.
<style name="AppTheme_Dialog" parent="Theme.AppCompat.Dialog">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorWhite</item>
<item name="colorAccent">#color/colorPrimary</item>
</style>
inside onCreate :
public class myDialog extends Dialog {
private Activity activity;
private someVars;
public PopupFeedBack(Activity activity){
super(activity, R.style.AppTheme_Dialog);
setContentView(R.layout.myView);
....}}
cheers :)
Pay attention to your colorAccent in your current Activity/fragment/Dialog, defined in Styles... ;)
cursor color is related to it.
Another simple solution would be to go to res>values>colors.xml in your project folder and edit the value of the color accent to the color you prefer
<color name="colorAccent">#000000</color>
The code above changes your cursor to black.
Its even easier than that.
<style name="MyTextStyle">
<item name="android:textCursorDrawable">#000000</item>
</style>
This works in ICS and beyond. I haven't tested it in other versions.
are you want specific color you must use AppCompatEditText then backround set null
works for me
<android.support.v7.widget.AppCompatEditText
android:background="#null"
android:textCursorDrawable="#color/cursorColor"/>
See this gist
In API 21 and above:
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:theme="#style/CursorColor">
// In colors.xml
<style name="CursorColor">
<item name="colorPrimary">#color/black</item>
</style>>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#36f0ff</item>
<item name="colorPrimaryDark">#007781</item>
<item name="colorAccent">#000</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
change t he color of colorAccent in styles.xm, that's it simple
If using style and implement
colorControlActivate
replace its value other that color/white.
you can use code below in layout
android:textCursorDrawable="#color/red"
android:textColor="#color/black
<style name="CustomTheme" parent="AppTheme">
<item name="colorAccent">#color/yourColor</item>
</style>
Add this in styles and then in the Edittext set the theme as follows:
android:theme="#style/CustomTheme"
And thats it!
I want to make grey my seekbar when I disable it.
I know I can programmatically make this changes by changing color but I'd like to know if it is possible to do this directly in my xml activity layout.
In fact in my code I'd like to just write this:
mySeekBarNuance.setEnabled(true);
And automatically my seekbar becomes grey.
Thanks
Do it like this
In your styles.xml
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar"> // or the themw you are using
...
...
<item name="android:seekBarStyle">#style/MySeekBarStyle</item>
</style>
Add also a new style
<style name="MySeekBarStyle" parent="android:Widget.SeekBar">
<item name="android:colorControlNormal">#color/gray</item> // put here the color for disable state
</style>
controlColorNormal is used for disabled state and colorAccent for enabled state
You can use a selector as background for the Seekbar
A selector set what you want if condition are met (here : android:state_enabled="true")
In your activity/fragment layout you put the seekbar with a background that is a selector :
<SeekBar
android:id="#+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/selector_seekbar"/>
You make the selector in your drawable folder :
selector_seekbar.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="#android:color/darker_gray" />
<item android:state_enabled="true"
android:drawable="#android:color/holo_green_dark" />
</selector>
If enabled, the seekbar will be holo green, otherwise it will be gray
You can do the same thing if the seekbar is focused, pressed etc thaht way.
And you can not only set the color but an entire drawable instead
As we all know, many Android apps display a white screen very briefly before their first Activity comes into focus. This problem is observed in the following cases:
Android apps that extend the global Application class and
perform major initializations therein. The Application
object is always created before the first Activity (a fact that can
be observed in the debugger), so this makes sense. This is the cause of the delay in my case.
Android apps that display the default preview window before the splash screen.
Setting android:windowDisablePreview = "true" obviously does not work here. Nor can I set the parent theme of the splash screen to Theme.Holo.NoActionBar as described here, because [unfortunately] my splash screen makes use of an ActionBar.
Meanwhile, apps that do not extend the Application class do not show the white screen at startup.
The thing is, ideally the initializations performed in the Application object need to occur before the first Activity is shown. So my question is, how can I perform these initializations on app startup without using an Application object? Possibly using a Thread or Service, I suppose?
This is an interesting problem to think about. I can't bypass it the usual way (by setting the NoActionBar theme), as tragically my Splash screen actually has an ActionBar due to some unrelated reasons.
Note:
I have already referred to the following questions:
How To fix white screen on app Start
up?
Android splash screen is white in the
beginning?
White screen before
splashscreen
White background when Android app start up
Why there is a white screen appears for 1sec when starting to run the apps in Android?
References:
Patterns – Launch screens.
Branded launch screen: the new splash for Android.
Splash Screens the Right Way.
please add this line into your app theme
<item name="android:windowDisablePreview">true</item>
for more information : https://developer.android.com/topic/performance/vitals/launch-time#themed
The problem with white background is caused because of android's cold start while the app loads to memory, and it can be avoided with this:
public class OnboardingWithCenterAnimationActivity extends AppCompatActivity {
public static final int STARTUP_DELAY = 300;
public static final int ANIM_ITEM_DURATION = 1000;
public static final int ITEM_DELAY = 300;
private boolean animationStarted = false;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
setTheme(R.style.AppTheme);
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_onboarding_center);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if (!hasFocus || animationStarted) {
return;
}
animate();
super.onWindowFocusChanged(hasFocus);
}
private void animate() {
ImageView logoImageView = (ImageView) findViewById(R.id.img_logo);
ViewGroup container = (ViewGroup) findViewById(R.id.container);
ViewCompat.animate(logoImageView)
.translationY(-250)
.setStartDelay(STARTUP_DELAY)
.setDuration(ANIM_ITEM_DURATION).setInterpolator(
new DecelerateInterpolator(1.2f)).start();
for (int i = 0; i < container.getChildCount(); i++) {
View v = container.getChildAt(i);
ViewPropertyAnimatorCompat viewAnimator;
if (!(v instanceof Button)) {
viewAnimator = ViewCompat.animate(v)
.translationY(50).alpha(1)
.setStartDelay((ITEM_DELAY * i) + 500)
.setDuration(1000);
} else {
viewAnimator = ViewCompat.animate(v)
.scaleY(1).scaleX(1)
.setStartDelay((ITEM_DELAY * i) + 500)
.setDuration(500);
}
viewAnimator.setInterpolator(new DecelerateInterpolator()).start();
}
}
}
layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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:background="?colorPrimary"
android:orientation="vertical"
>
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:paddingTop="144dp"
tools:ignore="HardcodedText"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:alpha="0"
android:text="Hello world" android:textAppearance="#style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
android:textColor="#android:color/white"
android:textSize="22sp"
tools:alpha="1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="8dp"
android:alpha="0"
android:gravity="center"
android:text="This a nice text"
android:textAppearance="#style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle.Inverse"
android:textSize="20sp"
tools:alpha="1"
/>
<Button
android:id="#+id/btn_choice1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:scaleX="0"
android:scaleY="0"
android:text="A nice choice"
android:theme="#style/Button"
/>
<Button
android:id="#+id/btn_choice2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:scaleX="0"
android:scaleY="0"
android:text="Far better!"
android:theme="#style/Button"
/>
</LinearLayout>
<ImageView
android:id="#+id/img_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/img_face"
tools:visibility="gone"
/>
</FrameLayout>
img face
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<item android:drawable="?colorPrimary"/>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/img_face"/>
</item>
Add this theme to your splashscreen in the manifest
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- 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">#null</item>
</style>
<style name="AppTheme.CenterAnimation">
<item name="android:windowBackground">#drawable/ll_face_logo</item>
</style>
which will produce efect like this
for more details and more solutions you can check this
BlogPost
Recommended way of solving this problem is missing in the answers. So I am adding my answer here. The white-screen-at-startup problem occurs because of the initial blank screen that the system process draws when launching the app. A common way to solve this is by turning off this initial screen by adding this to your styles.xml file.
<item name="android:windowDisablePreview">true</item>
But according to android documentation this can result in longer startup time. Recommended way of avoiding this initial white screen according to google is to use activity's windowBackground theme attribute and provide a simple custom drawable for the starting activity.
Like this:
Drawable Layout file, my_drawable.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
<!-- The background color, preferably the same as your normal theme -->
<item android:drawable="#android:color/white"/>
<!-- Your product logo - 144dp color version of your app icon -->
<item>
<bitmap
android:src="#drawable/product_logo_144dp"
android:gravity="center"/>
</item>
</layer-list>
Create a new style in your styles.xml
<!-- Base application theme. -->
<style name="AppTheme">
<!-- Customize your theme here. -->
</style>
<!-- Starting activity theme -->
<style name="AppTheme.Launcher">
<item name="android:windowBackground">#drawable/my_drawable</item>
</style>
Add this theme to your starting activity in the Manifest file
<activity ...
android:theme="#style/AppTheme.Launcher" />
And when you want to transition back to your normal theme call setTheme(R.style.Apptheme) before calling super.onCreate() and setContentView()
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// Make sure this is before calling super.onCreate
setTheme(R.style.Theme_MyApp);
super.onCreate(savedInstanceState);
// ...
}
}
This is the recommended way to solve the problem and this is from google Material Design patterns.
Please copy and paste these two lines in your manifest app theme i.e res/styles/AppTheme. then it will work like charm..
<item name="android:windowDisablePreview">true</item>
<item name="android:windowIsTranslucent">true</item>
First of all, to remove the white screen read this - https://www.bignerdranch.com/blog/splash-screens-the-right-way/
But more importantly, optimize your initial load and defer any heavy work to when you have time to run it. Post your application class here if you want us to take a look at it.
Have you tried setting theandroid:windowBackground attribute in the theme of your launcher activity, to either a color or a drawable?
For example this:
<item name="android:windowBackground">#android:color/black</item>
when added to the Launcher activity theme will show a black color (rather than the white color) on startup. This is an easy trick to hide long initialisation, while showing your users something, and it works fine even if you subclass the Application object.
Avoid using other constructs (even Threads) for doing long initialisation tasks, because you may end up not being able to control the lifecycle of such constructs. The Application object is the correct place for doing exactly this type of actions.
I added the following two lines in my theme
under styles.xml
<item name="android:windowDisablePreview">true</item>
<item name="android:windowBackground">#null</item>
Worked like a charm
I had same issue, you have to update your style.
style.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowDisablePreview">true</item>
<item name="android:windowBackground">#null</item>
<item name="android:windowIsTranslucent">true</item>
</style>
Your manifest file should looks like below.
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
// Other stuff
</application>
Outout:
Hope this would help you.
Within the lifecycle callback methods, you can declare how your activity behaves when the user leaves and re-enters the activity. Remember that the way Android is designed, there is a lifecycle for each and every app. If you put too much load to the onCreate() method (which is the method used to load the layout files and initalise any controls you have in it), then the white screen will become more visible, as the layout file will take longer to load.
I suggest using several different methods when starting an activity. Such are the onStart() (being called as the first thing once the app is loaded), onActivityCreated() (being called after the layout is displayed and useful if you are making any data processing upon starting the activity).
To make it easier for you, below is the official activity lifecycle diagram (from http://web.archive.org/web/20140218132043/http://developer.android.com/training/basics/activity-lifecycle/starting.html):
Please try this once.
Create a drawable file splash_background.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/{your color}" />
<item>
<bitmap
android:layout_width="#dimen/size_250"
android:layout_height="#dimen/size_100"
android:gravity="center"
android:scaleType="fitXY"
android:src="{your image}"
android:tint="#color/colorPrimary" />
</item>
</layer-list>
Put this in styles.xml
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/splash_background</item>
</style>
In your AndroidMainfest.xml set the above theme to Launch activity.
<activity
android:name=".SplashScreenActivity"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme"
android:windowSoftInputMode="stateVisible|adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
According to Google's recommendation Here, you should not prevent this white screen from launching. You can use this theme attribute to turn off the initial blank screen that the system process draws when launching the app.
<item name="android:windowDisablePreview">true</item>
However, This approach is not recommended because it can result in a longer startup time than apps that don’t suppress the preview window. Also, it forces the user to wait with no feedback while the activity launches, making them wonder if the app is functioning properly.
They recommend to use the activity's windowBackground theme attribute to provide a simple custom drawable for the starting activity instead of disabling the preview window.
Therefore, here is the recommended solution:
First, create a new drawable file for example startup_screen.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
<!-- The background color, preferably the same as normal theme -->
<item android:drawable="#android:color/white"/>
<!-- Product logo - 144dp color version of App icon -->
<item>
<bitmap
android:src="#drawable/logo"
android:gravity="center"/>
</item>
</layer-list>
Second, reference it from your style file. If you use Night mode. Add it in both themes.xml files.
<!-- Start Up Screen -->
<style name="AppThemeLauncher" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="android:statusBarColor" tools:targetApi="l">#color/lightGray</item>
<item name="android:windowBackground">#drawable/startup_screen</item>
</style>
If you notice, I added statusBarColor attribute to change the color of status Bar according to my custom design.
Then, Add AppThemeLauncher Theme in your current activity.
<activity
android:name=".MainActivity"
android:theme="#style/AppThemeLauncher"/>
If you want to transition back to your normal theme, call setTheme(R.style.AppTheme) before calling super.onCreate() and setContentView():
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
// Make sure this is before calling super.onCreate
setTheme(R.style.AppTheme)
super.onCreate(savedInstanceState)
// ...
}
}
Did you try to put initialization to onActivityCreated?
Inside Application class :
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
#Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
if(activity.getClass().equals(FirstActivity.class) {
// try without runOnUiThread if it will not help
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
new InitializatioTask().execute();
}
});
}
}
#Override
public void onActivityStarted(Activity activity) {
}
#Override
public void onActivityResumed(Activity activity) {
}
#Override
public void onActivityPaused(Activity activity) {
}
#Override
public void onActivityStopped(Activity activity) {
}
#Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
#Override
public void onActivityDestroyed(Activity activity) {
}
});
As you are already aware why this white screen is there, as due to background processes or application initialization or large files, so just check below idea for overcome from this.
To prevent this white screen on beginning of the app, one way is splash screen, this is just a way not final and you must have to use.
When you will show splash screen from your splash.xml file, then also this issue will be remain same,
So you have to create ont style in style.xml file for splash screen and there you have to set window background as your splash image and then apply that theme to your splash activity from manifest file. So now when you will run app, first it will set theme and by this way user will be able to see directly splash image instead of white screen.
Both properties works
<style name="AppBaseThemeDark" parent="#style/Theme.AppCompat">
<!--your other properties -->
<!--<item name="android:windowDisablePreview">true</item>-->
<item name="android:windowBackground">#null</item>
<!--your other properties -->
</style>
Just write the item in values/styles.xml:
<item name="android:windowBackground">#android:color/black</item>
For example, in the AppTheme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowBackground">#android:color/black</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Style :-
<style name="SplashViewTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/splash</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
In Manifest :-
<activity android:name=".SplashActivity"
android:theme="#style/SplashViewTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You should have colors.xml on values-night (create alongside values folder if it doesn't already exist) folder for dark theme colors.
eg.
<resources>
<color name="status_bar">#0e0e0e</color>
</resources>
(colors.xml on regular values folder will be used for light theme)
And on styles.xml which supplies your app theme you will have entry for background and statusbar which takes necessary values.
eg.
<style name="Theme.<AppName>" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/red700</item>
<item name="colorPrimaryDark">#color/red900</item>
<item name="colorAccent">#color/red700</item>
<item name="android:statusBarColor">#color/status_bar</item>
<item name="android:background">#color/status_bar</item>
</style>
This style is referenced on AndroidManifest.xml file
android:theme="#style/Theme.<AppName>">
Delete
<style name="AppTheme.Launcher">
<item name="android:windowBackground">#drawable/splashscreen</item>
</style>
from style.xml file
So I'm programmatically creating new buttons and adding them to a LinearLayout, however I want to initialize these buttons with a predefined style. I've spent some time searching for a solution and trying out answers, but I still can't seem to get it to work.
When I add a new button to the layout, it should look like the buttons (near the top) in this picture.
I've tried creating an xml file in res/values/ and initializing a button with new Button(context, null, R.style.ChoiceButton), but it doesn't work resulting in this happening.
I've also tried the workaround of creating a new layout xml for the button and using (Button)getLayoutInflater().inflate(R.layout.choice_buttton_layout, null), but that also didn't work, resulting in this (two buttons to show lack of margin).
res/values/choice_button.xml
<resources>
<style name="ChoiceButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:layout_marginBottom">7dp</item>
<item name="android:minWidth">250dp</item>
<item name="android:background">#ff27ae60</item>
<item name="android:textColor">#ffffffff</item>
<item name="android:enabled">true</item>
</style>
</resources>
Snippet from Main.java
public void btnAdd_click(View view) {
Button newBtn = new Button(getApplicationContext(), null, R.style.ChoiceButton);
newBtn.setText("new button");
newBtn.setId(Util.generateViewId());
LinearLayout layout = (LinearLayout)findViewById(R.id.layoutTop);
layout.addView(newBtn);
}
activity_main.xml
A bit long to paste in here.
Is there just something I'm missing? Is this even possible?
Ok #kin3tik, I found an old application I made with some custom button.. see what it looks like :
there is my xml for one button :
<Button
android:id="#+id/num1"
android:layout_width="110dp"
android:layout_height="100dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text="#string/num1"
android:textSize="20sp" />
I created file .xml in my drawable folder custombutton.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/bleuperso"/> // you can put #XXXXXX for the color you want
<corners android:radius="4dp"/>
And I just put the style in java :
Bfrancois.setBackgroundResource(R.drawable.custombutton);
With this you should be able to find yourself ;)
This question already has answers here:
Toggle button using two image on different state
(3 answers)
Closed 9 years ago.
I have two power button images, one red and the other is green. I want to create a button set its background resource to red power button initially. I want its resource to be changed to green when it is pressed & after another click, i want it to turn back into red again. Please Help...
Do this:
<ToggleButton
android:id="#+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/check" //check.xml
android:layout_margin="10dp"
android:textOn=""
android:textOff=""
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_centerVertical="true"/>
create check.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="#drawable/selected_image"
android:state_checked="true" />
<!-- When not selected, use white-->
<item android:drawable="#drawable/unselected_image"
android:state_checked="false"/>
</selector>
This works perfectly fine.
use ToggleButton. numerous examples available like here.
How to Make a Toggle Button with Custom On/Off Graphics
Use a CheckBox with a custom selector.
This will provide the ability to switch between checked and unchecked images for enabled and disabled states without any programmatic intervention in Java code.
Example - XML layout:
<CheckBox
android:id="#+id/my_custom_toggle"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:button="#drawable/my_selector"
/>
Example - drawable/my_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:state_enabled="false"
android:drawable="#drawable/ic_button_custom_toggle_disabled"
/>
<item
android:state_checked="false"
android:state_enabled="false"
android:drawable="#drawable/ic_button_custom_toggle_disabled"
/>
<item
android:state_checked="true"
android:drawable="#drawable/ic_button_custom_toggle_linked"
/>
<item
android:state_checked="false"
android:drawable="#drawable/ic_button_custom_toggle_unlinked"
/>
Add custom .png images for each of the above states.
you need to place two images red and green in drawable folders.
static int set = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageButton toggle = (ImageButton) findViewById(R.id.imageButton1);
toggle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(set==0)
{
toggle.setBackgroundResource(R.drawable.red);
set=1;
}
else
{
toggle.setBackgroundResource(R.drawable.green);
set=0;
}
}
});
}