How to gray a seekbar when disabled? - java

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

Related

How To Change EditText Cursor Color In A Alert Dialog? [duplicate]

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!

Draw behind status bar

I have defined a relativeLayout with a recyclerView and an imageView. I want the imageView to draw behind the status bar. But for some reason
android:fitsSystemWindow = true
doesn't work.
How can I achieve it?
Set your full-screen theme like following
<style name="Theme.MyApp.FullScreen" parent="Theme.MyApp">
<item name="android:statusBarColor" tools:targetApi="lollipop">#android:color/transparent</item>
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
<item name="android:windowDrawsSystemBarBackgrounds" tools:targetApi="lollipop">true</item>
</style>
Don't add android:fitsSystemWindows="true" in your layout file
Also, no need to add android:windowSoftInputMode="adjustPan" in Manifest file
To draw behind status bar, You have to set your status bar have transparent background.
So, you need to set this with adding below proprieties in your theme:
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
And in your .xml you have already set below property which is useful:
android:fitsSystemWindows="true"

How to make a view transparent in android?

I'm doing some UI designing for class on an open source project. I'm trying to make the appbar and tabs of this tablayout transparent but no matter what I do they remain white. If it were transparent the background would be able to be seen but i get this white instead.
I have tried-
changing the xml values in the mainfragments xml to transparent
changing the colors.xml values to transparent (all of them because i wanted
to be sure)
changing and even removing the "light theme" and "dark theme" values in
styles.xml and removing the app theme line in the android manifest
android:theme="#style/AppTheme.FullScreen.Light"
Am I missing something else that i can try? I would be very thankful for any suggestions.
in style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Transparent" parent="android:Theme">
<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>
</resources>
in color.xml
value #color/transparent is the color value #00000000
<activity android:name=".YourActivity" android:theme="#style/Theme.Transparent">
</activity>
set alpha value in layout XML or dynamically in view to make view transparent.
view.setAlpha(40);
oR add from xml - value between 0 to 1
android:alpha="0.4"
In android Studio res->values->colors.xml
Step 1 : For selecting color
Step 2 : Changing transparency(you can move left to right for adjusting transparency )
Step 3 : Give this color as views background
<RelativeLayout
android:id="#+id/top_bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/color_name">
This may help you.

Setting Activities' Background Color

I am trying to change all of my activities' background color from white to black. I have tried everything - changing windowBackground, colorBackground, background but nothing seems to be working and I know my style is working because if I changed primary, accent colors they seem to be changing. The background tag only changes the action bar's color. Here's my code for the theme (what I have so far) -
<style name="Yoyo" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent1</item>
<item name="android:windowBackground">#color/darkBackground</item>
<item name="android:colorBackground">#color/darkBackground</item>
<item name="android:textColor">#ffffff</item>
</style>
darkBackground is black - what I need for my app to show but its not showing.
Also, I have already tried using #drawable/my_drawable_file (in which I set black color) in windowBackground but even that doesn't work.
min sdk version - 21, target sdk version - 25
Try to change background your activity root element. For example:
<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:background="your_color">
Try:
<color name="custom_theme_color">#000000</color>
<style name="CustomTheme"Parent="android:Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowBackground">#color/custom_theme_color</item>
<item name="android:colorBackground">#color/custom_theme_color</item>
</style>
and then use the custom theme in your manifest
Nevermind people, I was just being stupid, I had hard-coded the colour for the said activity in the XML file, as soon as I removed the line which was setting the colour from XML the windowBackground started working!

Android, How to change the actionbars logo button on state change?

So I'm wondering how one could implement to an action bar with a logo button, a different image per state.(pressed, released etc).
I know how to do this for a normal button, but I'm not sure if it's possible with the actionbars logo button?
And if it's not, how could one implement am action bar that supports this? externals libs?
Thank you.
Create a drawable xml file in res/drawable such as res/drawable/button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/button_normal" />
</selector>
where button_pressed and button_normalare just regular png files in one of your drawable directories (e.g/ drawables-hdpi)
Then in your menu.xml or in your code you can refer to #drawable/button_selector or R.drawable.button_selector and the image will change automatically when the button is pressed.
If you need to change the ActionBar button background you need to override the theme in your res/values/style.xml.
<style name="YourTheme.Sherlock" parent="#style/Theme.Sherlock">
<item name="actionBarItemBackground">#drawable/actionbar_item_background_selector</item>
<item name="android:actionBarItemBackground">#drawable/actionbar_item_background_selector</item>
</style>
Both lines matter: one for the SherlockActionBar and one for the standard ActionBar
Then set the theme to your activity in the manifest:
<activity
android:name=".yourActivity"
android:theme="#style/YourTheme.Sherlock" >
</activity>
And here is actionbar_item_background_selector.xml to be placed in res/drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="#drawable/scene_overlay_bar_pressed_center" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#drawable/abs__list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/scene_overlay_bar_pressed_center" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/scene_overlay_bar_pressed_center" />
<item android:state_focused="true" android:drawable="#drawable/abs__list_focused_holo" />
<item android:drawable="#android:color/transparent" />
</selector>
replace the background with the drawable of your choice where android:state_pressed="true"
It's a lot to do for just a background but that is how the ActionBar works.
you can use a selector to pick different image.
Android ImageButton with disabled UI feel
may be help
I think you are looking for this:
final ActionBar bar = getActionBar();
bar.setDisplayHomeAsUpEnabled(true);
This automatically makes the logo you have set look like its being pressed when its clicked. No need to have different images to show state change. Thats a pretty easy way. Though if you are using ActionBarSherlock this won't work, I think. Strictly 4.0 I believe. Check out the doc on ActionBar.

Categories