How to change android indeterminate ProgressBar color? - android

I would like to know how I can change indeterminate ProgressBar color from basis white/grey color to black ? When I change the indeterminateDrawable, I get a static image instead of a moving animated progressBar. Is there any way to do it simply in XML?

progressBar.getIndeterminateDrawable().setColorFilter(
getResources().getColor(Your_Color),
android.graphics.PorterDuff.Mode.SRC_IN);
and replace Your_Color with the desired color like: R.color.your_color_code

To get a ProgressBar in the default theme that is to be used on white/light back ground, use one of the inverse styles:
<ProgressBar style="#android:style/Widget.ProgressBar.Inverse"/>
<ProgressBar style="#android:style/Widget.ProgressBar.Large.Inverse"/>
<ProgressBar style="#android:style/Widget.ProgressBar.Small.Inverse"/>
This will usually give you a black on transparent ProgressBar, but some OS installs use custom assets. If you're looking for a specific color, you'll have to roll your own drawables by following the instructions provided by CommonsWare.

I see other answers are quite old, in order to change the indeterminate ProgressBar color you have just to set android:indeterminateTint and android:indeterminateTintMode attributes in your ProgressBar item directly in XML:
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateTint="#color/colorPrimary"
android:indeterminateTintMode="src_in"
android:indeterminate="true" />
android:indeterminateTint - Tint to apply to the indeterminate progress indicator.
Must be a color value, in the form of "#rgb", "#argb", "#rrggbb", "#aarrggbb", or a reference to a resource #color/colorPrimary.
android:indeterminateTintMode - Blending mode used to apply the progress indicator tint.
Must be one of the following constant values:
add, multiply, screen, src_atop, src_in or src_over
Getter and Setter methods for these attributes are:
getIndeterminateTintList()
getIndeterminateTintMode()
setIndeterminateTintList(ColorStateList tint)
setIndeterminateTintMode(PorterDuff.Mode tintMode)
All of them were added in API level 21

I make sample project and post in in my blog. Please, look at. http://www.hrupin.com/2011/09/21/how-to-make-custom-indeterminate-progressbar-in-android-or-how-to-change-progressbar-style-or-color
Hope, it help you

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background" android:drawable="#drawable/bg" />
<item android:id="#android:id/secondaryProgress" android:drawable="#drawable/secondary" />
<item android:id="#android:id/progress" android:drawable="#drawable/progress" />
</layer-list>

For API less than 23 use
progressBar.getIndeterminateDrawable().setColorFilter(
getResources().getColor(Your_Color),
android.graphics.PorterDuff.Mode.SRC_IN);
else use
progressBar.getIndeterminateDrawable().setColorFilter(
ContextCompat.getColor(context, Your_Color),
android.graphics.PorterDuff.Mode.SRC_IN);

Override android:colorControlActivated in your AppTheme which should be in your styles.xml:
<style name="AppTheme" parent="...">
...
<item name="android:colorControlActivated">#color/beautiful_color</item>
...
</style>
Works on API 21+

With the Material Component Library you can use the CircularProgressIndicator with these attributes:
indicatorColor
trackColor
Something like:
<com.google.android.material.progressindicator.CircularProgressIndicator
android:indeterminate="true"
app:trackColor="#color/red600Light"
app:indicatorColor="#color/red600Dark"
app:indicatorSize="50dp"
.../>
You can also use a Multi-color indeterminate indicator.
<com.google.android.material.progressindicator.CircularProgressIndicator
android:indeterminate="true"
app:trackColor="#color/red600Light"
app:indicatorColor="#array/progress_colors"
app:indeterminateAnimationType="contiguous"/>
with array/progress_colors:
<integer-array name="progress_colors">
<item>#color/...</item>
<item>#color/...</item>
<item>#color/...</item>
</integer-array>

actually all u need to do (for both cirle and bar) is create an xml file in drawable as shown below......
progress_spinner_001 points to whatever image u want to animate and duration ... how long u want to display each frame for.......
and set ur android:indeterminateDrawable= filename_in_drawable....
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="#drawable/progress_spinner_001" android:duration="300" />
<item android:drawable="#drawable/progress_spinner_002" android:duration="300" />
<item android:drawable="#drawable/progress_spinner_003" android:duration="300" />
<item android:drawable="#drawable/progress_spinner_004" android:duration="300" />
<item android:drawable="#drawable/progress_spinner_005" android:duration="300" />
<item android:drawable="#drawable/progress_spinner_006" android:duration="300" />
<item android:drawable="#drawable/progress_spinner_007" android:duration="300" />
<item android:drawable="#drawable/progress_spinner_008" android:duration="300" />
</animation-list>
ps u may need to resize the progressbar to show as desired

Related

Listview highlight on press and ripple different in app and on widget... how do i change widget values?

So I have a listview in my app and it seems that the AppTheme value "colorControlHighlight" changes the colors for ripple and onclick row in that listview to (blue). However I made a listview with identical xml attributes that relate to color on a widget and for some reason the ripple and on press colors are still the default color (white). If I change the android:listselector attribute for the listview on my widget, it works but it doesn't act the same and the ripple effect is gone. How it acts; after I change listselector it stays blue unless I press a different row on the list. This is not what I want (is this because i haven't set onclick actions yet? or is this how that attribute is designed to work?). Any help would be appreciated.
This is my app activity listview
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/AppUrlList"
android:background="#color/dark_black"
android:layout_gravity="center_horizontal"
android:dividerHeight="1px"
android:divider="#color/faded_blue"
android:clickable="true"
/>
now this is my widget listview
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:dividerHeight="2px"
android:divider="#color/faded_blue"
android:background="#color/black"
android:id="#+id/WidgetListView"
android:layout_gravity="center_horizontal" />
now this is my apptheme
<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="colorControlNormal">#color/faded_blue</item>
<item name="colorControlActivated">#color/bright_blue</item>
<item name="colorControlHighlight">#color/bright_blue</item>
thanks for the suggestions to improve my question i'm new here
I figured it out i had to do this:
app_list_item.xml
<TextView
android:id="#+id/AppListTextViewGreen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/List_Hint"
android:gravity="center"
android:background="#drawable/list_background_green"
style="#style/liststyleGreen"
/>
then in list_background_green.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#color/faded_green" />
<item android:state_pressed="true" android:drawable="#color/faded_green"/>
<item android:drawable="#color/black" />
</selector>
the state_pressed is what defines the color when the list item is pressed (obviously)
and the default background is black.
this works on my widget whereas setting the default colors in the app theme did not change the colors in my widget.
EDIT: forgot to put the ripple selector here too, here it is, and i'll also include how to make it work on versions above api 21.
you have to name the ripple selector the same name as the list selector. so you would make another xml named "list_background_green" but put it in drawable-v21 folder. This folder probably won't exist so right click on res folder in android studio and click show in explorer, then create drawable-v21 folder and put the same named xml there. here is an example.
drawable-v21/list_background_green.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/bright_green">
<item>
<selector>
<item android:state_selected="true">
<color android:color="#color/transp_green" />
</item>
<item android:state_activated="true">
<color android:color="#color/transp_green" />
</item>
<item android:state_pressed="true">
<color android:color="#color/transp_green" />
</item>
<item>
<color android:color="#color/semi_trans_black" />
</item>
</selector>
</item>
you want to be careful how transparent you make this value.
<color android:color="#color/semi_trans_black" />
i couldn't figure out why the selector wasn't showing up for a while and then realized that if this value is too transparent it makes the ripple not show up. I can't explain why if someone can please do, just warning people because it took me a while to figure out that was the cause. That value is the default background for each textview in the list, so maybe the selectors inherit it's transparency although that doesn't make alot of sense. I only had to do this on my appwidget, for some reason beyond my current understanding. In my regular app listview i didn't need to do any of this. My suspicion is that widgets inherit their theme from the system but i'm not sure of this.
cheers, hope this helps someone.

How to change the color of a button?

I'm new to android programming. How do I change the color of a button?
<Button
android:id="#+id/btn"
android:layout_width="55dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:text="Button Text"
android:paddingBottom="20dp"/>
The RIGHT way...
The following methods actually work.
if you wish - using a theme
By default a buttons color is android:colorAccent. So, if you create a style like this...
<style name="Button.White" parent="ThemeOverlay.AppCompat">
<item name="colorAccent">#android:color/white</item>
</style>
You can use it like this...
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/Button.White"
/>
alternatively - using a tint
You can simply add android:backgroundTint for API Level 21 and higher, or app:backgroundTint for API Level 7 and higher.
For more information, see this blog.
The problem with the accepted answer...
If you replace the background with a color you will loose the effect of the button, and the color will be applied to the entire area of the button. It will not respect the padding, shadow, and corner radius.
You can change the colour two ways; through XML or through coding. I would recommend XML since it's easier to follow for beginners.
XML:
<Button
android:background="#android:color/white"
android:textColor="#android:color/black"
/>
You can also use hex values ex.
android:background="#FFFFFF"
Coding:
//btn represents your button object
btn.setBackgroundColor(Color.WHITE);
btn.setTextColor(Color.BLACK);
For the text color add:
android:textColor="<hex color>"
For the background color add:
android:background="<hex color>"
From API 21 you can use:
android:backgroundTint="<hex color>"
android:backgroundTintMode="<mode>"
How to customize different buttons in Android
If the first solution doesn't work try this:
android:backgroundTint="#android:color/white"
I hope this work.
Happy coding.
Many great methods presented above - One newer note
It appears that there was a bug in earlier versions of Material that prevented certain types of overriding the button color.
See: [Button] android:background not working #889
I am using today Material 1.3.0. I just followed the direction of KavinduDissanayake in the linked post and used this format:
app:backgroundTint="#color/purple_700"
(I changed the chosen color to my own theme of course.) This solution worked very simply for me.
Here is my code, to make different colors on button, and Linear, Constraint and Scroll Layout
First, you need to make a custom_button.xml on your drawable
Go to res
Expand it, right click on drawable
New -> Drawable Resource File
File Name : custom_button, Click OK
Custom_Button.xml Code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/red"/> <!-- pressed -->
<item android:state_focused="true" android:drawable="#color/blue"/> <!-- focused -->
<item android:drawable="#color/black"/> <!-- default -->
</selector>
Second, go to res
Expand values
Double click on colors.xml
Copy the code below
Colors.xml Code
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="black">#000</color>
<color name="violet">#9400D3</color>
<color name="indigo">#4B0082</color>
<color name="blue">#0000FF</color>
<color name="green">#00FF00</color>
<color name="yellow">#FFFF00</color>
<color name="orange">#FF7F00</color>
<color name="red">#FF0000</color>
</resources>
Screenshots below
XML Coding
Design Preview
Starting with API 23, you can do:
btn.setBackgroundColor(getResources().getColor(R.color.colorOffWhite));
and your colors.xml must contain:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorOffWhite">#80ffffff</color>
</resources>
Best way to change button color without losing button ghosting and other features.
Try it and you will see it is the best
app:backgroundTint="#color/color_name"
in theme change this:
parent="Theme.MaterialComponents.DayNight.DarkActionBar"
to that:
parent="Theme.AppCompat.Light.NoActionBar"
It worked for me after many search
usually with API 21 and above :
just PUT this attribute : android:backgroundTint=" your color "
You can change the value in the XML like this:
<Button
android:background="#FFFFFF"
../>
Here, you can add any other color, from the resources or hex.
Similarly, you can also change these values form the code like this:
demoButton.setBackgroundColor(Color.WHITE);
Another easy way is to make a drawable, customize the corners and shape according to your preference and set the background color and stroke of the drawable.
For eg.
button_background.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2dp" android:color="#ff207d94" />
<corners android:radius="5dp" />
<solid android:color="#FFFFFF" />
</shape>
And then set this shape as the background of your button.
<Button
android:background="#drawable/button_background.xml"
../>
Hope this helps, good luck!
see the image and easy understand
in new update of android studio you have to change the
button -> androidx.appcompat.widget.AppCompatButton
then only the button color will changed
res/drawable/button_color_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FFDA8200" />
<stroke
android:width="3dp"
android:color="#FFFF4917" />
</shape>
And add button to your XML activity layout and set background
android:background="#drawable/button_color_border".
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_border"
android:text="Button" />
Use below code for button background color
btn.setBackgroundResource(R.drawable.btn_rounded);
here is drawable xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/gray_scale_color"/>
<corners android:radius="#dimen/_12sdp"/>
<stroke android:width="0.5dp"
android:color="?attr/appbackgroundColor"/>
</shape>
</item>
</layer-list>
i'm using api 32 and I had to do it this way in the xml code:
android:backgroundTint="#android:color/white"
If you are trying to set the background as some other resource file in your drawable folder, say, a custom-button.xml, then try this:
button_name.setBackgroundResource(R.drawable.custom_button_file_name);
eg. Say, you have a custom-button.xml file. Then,
button_name.setBackgroundResource(R.drawable.custom_button);
Will set the button background as the custom-button.xml file.
I have the same problem
the solution for me was the background color was colorPrimary of my theme
you can use custom theme as one answer say above and set the colorPrimary to what you want
1- add this to your "value/themes/themes.xml" inside resources
<resources>
<style name="Button.color" parent="ThemeOverlay.AppCompat">
<item name="colorPrimary">#color/purple_500</item>
</style>
</resources>
2- add this line to the button you want to have the color
<Button
android:theme="#style/Button.color"/>
Go to res > values > themes > theme.xml/themes.xml. Then change:
<style name="Theme.BirthdayGreet" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
To:
<style name="Theme.MemeShare" parent="Theme.AppCompat.Light.NoActionBar">)>
Watch this video for more information.
backgroundTint above API21 background has no effect it takes colorPrimary of the theme by default
Button background color in xml
<Button
android:id="#+id/button"
android:background="#0000FF"
android:textColor="#FFFFFF"/>
Change button background color programmatically
Button button = findViewById(R.id.button);
button.setBackgroundColor(Color.BLUE);
Custom button background
shape.xml [res --> drawble]
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp" />
<gradient
android:angle="0"
android:endColor="#0000FF"
android:startColor="#00FFFF" />
<stroke
android:width="1dp"
android:color="#000000" />
</shape>
Add this line
android:background="#drawable/shape"
full code
<Button
android:id="#+id/button"
android:background="#drawable/shape"
android:textColor="#FFFFFF"/>
Material Design Button
Change button background color
app:backgroundTint="#0000FF"
Button Stroke color
app:strokeColor="#000000"
Button Stroke width
app:strokeWidth="2dp"
Full code
<Button
android:id="#+id/button"
android:textColor="#FFFFFF"
app:backgroundTint="#0000FF"
app:strokeColor="#000000"
app:strokeWidth="2dp"/>
Hope you helpful!
Go to themes.xml file under res -> values -> themes
Go to the line -
<style name="Theme.YourProjectName" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
and change it to -
<style name="Theme.YourProjectName" parent="Theme.AppCompat.DayNight.DarkActionBar">
Then proceed with changing
android:backgroundTint to desired color
Buttons use colorPrimary attribute by default.
If you are styling using Theme:
<item name="colorPrimary">#color/yourColor</item>
Note that other widgets also use this attribute.
Drop any other widgets, that you know use this attribute, in the comments.
To change the color of button programmatically
Here it is :
Button b1;
//colorAccent is the resource made in the color.xml file , you can change it.
b1.setBackgroundResource(R.color.colorAccent);

How to change line color in EditText

I am creating an EditText in my layout xml file
But I want to change color line in EditText from Holo to (for example) red.
How that can be done?
This is the best tool that you can use for all views and its FREE many thanks to #Jérôme Van Der Linden.
The Android Holo Colors Generator allows you to easily create Android components such as EditText or spinner with your own colours for your Android application. It will generate all necessary nine patch assets plus associated XML drawable and styles which you can copy straight into your project.
http://android-holo-colors.com/
UPDATE 1
This domain seems expired but the project is an open source you can find here
https://github.com/jeromevdl/android-holo-colors
try it
this image put in the background of EditText
android:background="#drawable/textfield_activated"
UPDATE 2
For API 21 or higher, you can use android:backgroundTint
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Underline color change"
android:backgroundTint="#android:color/holo_red_light" />
Update 3
Now We have with back support AppCompatEditText
Note: We need to use app:backgroundTint instead of android:backgroundTint
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Underline color change"
app:backgroundTint="#color/blue_gray_light" />
Update 4
AndroidX version
<androidx.appcompat.widget.AppCompatEditText
app:backgroundTint="#color/blue_gray_light" />
I don't like previous answers. The best solution is to use:
<android.support.v7.widget.AppCompatEditText
app:backgroundTint="#color/blue_gray_light" />
android:backgroundTint for EditText works only on API21+ . Because of it, we have to use the support library and AppCompatEditText.
Note: we have to use app:backgroundTint instead of android:backgroundTint
AndroidX version
<androidx.appcompat.widget.AppCompatEditText
app:backgroundTint="#color/blue_gray_light" />
You can also quickly change the EditText's underline color by tinting the background of the EditText like so:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Something or Other"
android:backgroundTint="#android:color/holo_green_light" />
for API below 21, you can use theme attribute in EditText
put below code into style file
<style name="MyEditTextTheme">
<item name="colorControlNormal">#FFFFFF</item>
<item name="colorControlActivated">#FFFFFF</item>
<item name="colorControlHighlight">#FFFFFF</item>
</style>
use this style in EditText as
<EditText
android:id="#+id/etPassword"
android:layout_width="match_parent"
android:layout_height="#dimen/user_input_field_height"
android:layout_marginTop="40dp"
android:hint="#string/password_hint"
android:theme="#style/MyEditTextTheme"
android:singleLine="true" />
Programmatically, you can try:
editText.getBackground().mutate().setColorFilter(getResources().getColor(android.R.color.holo_red_light), PorterDuff.Mode.SRC_ATOP);
Its pretty simple (Required: Minimum API 21)...
Go to your xml and select the EditText field
On the right side, you can see the 'Attributes' window. Select 'View All Attributes'
Just search for 'tint'
And add/change the 'backgroundTint' to a desired color hex (say #FF0000)
Keep Coding........ :)
You can change the color of EditText programmatically just using this line of code:edittext.setBackgroundTintList(ColorStateList.valueOf(yourcolor));
i think the best way is by theme:
<style name="MyEditTextTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">#color/black</item>
<item name="colorControlActivated">#color/action_blue</item>
<item name="colorControlHighlight">#color/action_blue</item>
</style>
<style name="AddressBookStyle" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">13sp</item>
<item name="android:theme">#style/MyEditTextTheme</item>
</style>
<android.support.v7.widget.AppCompatEditText
style="#style/AddressBookStyle"/>
To change Edittext’s underline color:
If you want the entire app to share this style, then you can do the following way.
(1) go to styles.xml file. Your AppTheme that inherits the parent of Theme.AppCompat.Light.DarkActionBar (in my case) will be the base parent of all they style files in your app. Change the name of it to “AppBaseTheme’. Make another style right under it that has the name of AppTheme and inherits from AppBaseTheme that you just edited. It will look like following:
<!-- Base application theme. -->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<!--see http://www.google.com/design/spec/style/color.html#color-color-palette-->
<item name="colorPrimary">#color/material_brown_500</item>
<item name="colorPrimaryDark">#color/material_brown_700</item>
<item name="colorAccent">#color/flamingo</item>
<style name="AppTheme" parent="AppBaseTheme">
<!-- Customize your theme here. -->
</style>
Then change the “colorAccent” to whatever the color you want your EditText line color to be.
(2) If you have other values folders with style.xml, this step is very important. Because that file will inherit from your previous parent xml file. For example, I have values-19/styles.xml. This is specifically for Kitkat and above. Change its parent to AppBaseTheme and make sure to get rid of “colorAccent” so that it doesn’t override the parent’s color. Also you need to keep the items that are specific to version 19. Then it will look like this.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
The line's color is defined by EditText's background property. To change it you should change the android:background in the layout file.
I should note that this style is achieved by using a 9-patch drawable. If you look in the SDK, you can see that the background of the EditText is this image:
To change it you could open it in an image manipulation program and color it in desired color. Save it as bg_edit_text.9.png and then put it in you drawable folder. Now you can apply it as a background for your EditText like so:
android:background="#drawable/bg_edit_text"
drawable/bg_edittext.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#android:color/transparent" />
</shape>
</item>
<item
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#color/colorDivider" />
</shape>
</item>
</layer-list>
Set to EditText
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_edittext"/>
The background of widgets are API level dependent.
ALTERNATIVE 1
You can provide a custom image to your EditText background by
android:background="#drawable/custom_editText"
Your image should look something like this. It will give you the desired effect.
ALTERNATIVE 2
Set this xml to your EditText background attribute.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#4C000000"/>
<corners android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
This will have the same look and feel of your EditText on every API.
You can change the color with tinting the background
<EditText
android:backgroundTint="#color/red"/>
in xml layout use:
android:backgroundTint="#color/colorPrimary"
or in java code copy this method:
public void changeLineColorInEditText(EditText editText, int color) {
editText.setBackgroundTintList(ColorStateList.valueOf(color));
}
and use it like this:
changeLineColorInEditText(editText, getResources().getColor(R.color.colorPrimary));
Use this method.. and modify it according to ur view names. This code works great.
private boolean validateMobilenumber() {
if (mobilenumber.getText().toString().trim().isEmpty() || mobilenumber.getText().toString().length() < 10) {
input_layout_mobilenumber.setErrorEnabled(true);
input_layout_mobilenumber.setError(getString(R.string.err_msg_mobilenumber));
// requestFocus(mobilenumber);
return false;
} else {
input_layout_mobilenumber.setError(null);
input_layout_mobilenumber.setErrorEnabled(false);
mobilenumber.setBackground(mobilenumber.getBackground().getConstantState().newDrawable());
}
If you want a flat line, you can do this easily with xml. Here is the xml example:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:top="-1dp"
android:left="-1dp"
android:right="-1dp"
android:bottom="1dp"
>
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="#6A9A3A"/>
</shape>
</item>
</layer-list>
Replace the shape with a selector if you want to provide different width and color for focused edittext.
The best approach is to use an AppCompatEditText with backgroundTint attribute of app namespace. i.e.
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
app:backgroundTint="YOUR COLOR"
android:layout_height="wrap_content" />
when we use android:backgroundTint it will only work in API21 or more but app:backgroundTint works on all API levels your app does.
You can do it dynamically if you have custom class for edittext.
First of all you have declare state and color of edittext given below.
int[][] states = new int[][]{
new int[]{-android.R.attr.state_focused}, // enabled
new int[]{android.R.attr.state_focused}, // disabled
};
int[] colors = new int[]{
secondaryColor,
primaryColor,
};
Then Create ColorStateList variable with that
ColorStateList myList = new ColorStateList(states, colors);
Then last step is to assign it to edittext.
editText.setBackgroundTintList(myList);
After this you have to written on focused change event.
this.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
setUnderlineColor(selectionColor,deselectionColor);
}
});
And you can make above code inside setUnderlineClor() Method,
private void setUnderlineColor(int primaryColor, int secondaryColor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int[][] states = new int[][]{
new int[]{-android.R.attr.state_focused}, // enabled
new int[]{android.R.attr.state_focused}, // disabled
};
int[] colors = new int[]{
secondaryColor,
primaryColor,
};
ColorStateList myList = new ColorStateList(states, colors);
setBackgroundTintList(myList);
}
}
Use android:background property for that edittext. Pass your drawable folder image to it.
For example,
android:background="#drawable/abc.png"
<EditText
android:id="#+id/et_password_tlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:textColorHint="#9e9e9e"
android:backgroundTint="#000"
android:singleLine="true"
android:drawableTint="#FF4081"
android:paddingTop="25dp"
android:textColor="#000"
android:paddingBottom="5dp"
android:inputType="textPassword"/>
<View
android:id="#+id/UnderLine"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/et_password_tlay"
android:layout_centerHorizontal="true"
android:background="#03f94e" />
**it one of doing with view **
Try the following way, it will convert the bottom line color of EditText, when used as a background property.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="#dimen/spacing_neg"
android:right="#dimen/spacing_neg"
android:top="#dimen/spacing_neg">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="#dimen/spacing_1"
android:color="#android:color/black" />
</shape>
</item>
</layer-list>
This can simply be done by including this android:theme="#style/AppTheme.AppBarOverlay as attribute in for your editText
and add this <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> to your styles

Translucent seekbar

I prefer if I could my the seekbar in my app looks Translucent, is this possible?
<SeekBar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/topBar"
android:layout_marginTop="26dip"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="10dip"
android:max="100"
android:background="#drawable/roundedcorner" >
</SeekBar>
Set the android:progressDrawable attribute to a custom layer-list drawable with translucent components. You can set the android:thumb property as well.
Here's a sample drawable xml from HoloEverywhere, you can reference that project for sample image assets, too:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background"
android:drawable="#drawable/progress_bg_holo_dark" />
<item android:id="#android:id/secondaryProgress">
<scale android:scaleWidth="100%"
android:drawable="#drawable/progress_secondary_holo_dark" />
</item>
<item android:id="#android:id/progress">
<scale android:scaleWidth="100%"
android:drawable="#drawable/progress_primary_holo_dark" />
</item>
</layer-list>
Also - related question:
Custom Drawable for ProgressBar/ProgressDialog
In your XML, add this attribute :
android:alpha = "floatValue"
or, in the java code use
setAlpha(floatValue)
It enables you to change the alpha property of the view.
0 = completely transparent
1 = completely opaque
So, for translucent, you can set to 0.5 or any value according to the transparency you desire.
You can set opacity value using hex colors. You just need to add the right prefix.
I hid the seekBar using this code:
android:progressBackgroundTint="#00555555"
android:progressTint="#00555555"
Where the first two ciphers (i.e. "00") set opaqueness (alpha percentage) and the other six (i.e. "555555") set the color.
Check this post for more information and a list of hex opacity values:
Understanding colors on Android (six characters)

Button background as transparent

I have a button. When I press the button I have to make text as bold otherwise normal. So I wrote styles for bold & normal.
<style name="textbold" parent="#android:style/TextAppearance">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textStyle">bold</item>
</style>
<style name="textregular" parent="#android:style/TextAppearance">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textStyle">normal</item>
</style>
Now I have a button_states.xml as:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true"
style="#style/textbold" />
<item android:state_focused="false" android:state_pressed="true"
style="#style/textregular" />
<item style="#style/textregular" />
</selector>
In my layout for this button, I have to make the background as transparent too...How will I do it? My layout code is :
<Button android:id="#+id/Btn" android:background="#drawable/button_states" />
How will I include background as transparent in my style?
To make a background transparent, just do android:background="#android:color/transparent".
However, your problem seems to be a bit deeper, as you're using selectors in a really weird way. The way you're using it seems wrong, although if it actually works, you should be putting the background image in the style as an <item/>.
Take a closer look at how styles are used in the Android source. While they don't change the text styling upon clicking buttons, there are a lot of good ideas on how to accomplish your goals there.
Try new way to set background transparent
android:background="?android:attr/selectableItemBackground"
You may also use:
in your xml:
android:background="#null"
or in code:
buttonVariable.setBackgroundColor(Color.TRANSPARENT);
use #0000 (only four zeros otherwise it will be considered as black) this is the color code for transparent. You can use it directly but I recommend you to define a color in color.xml so you can enjoy re-usefullness of the code.
Add this in your Xml - android:background="#android:color/transparent"
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button"
android:background="#android:color/transparent"
android:textStyle="bold"/>
I achieved this with in XML with
android:background="#android:color/transparent"
I used
btn.setBackgroundColor(Color.TRANSPARENT);
and
android:background="#android:color/transparent"
Selectors work only for drawables, not styles. Reference
First, to make the button background transparent use the following attribute as this will not affect the material design animations:
style="?attr/buttonBarButtonStyle"
There are many ways to style your button. Check out this tutorial.
Second, to make the text bold on pressed, use this java code:
btn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
// When the user clicks the Button
case MotionEvent.ACTION_DOWN:
btn.setTypeface(Typeface.DEFAULT_BOLD);
break;
// When the user releases the Button
case MotionEvent.ACTION_UP:
btn.setTypeface(Typeface.DEFAULT);
break;
}
return false;
}
});
We can use attribute android:background in Button xml like below.
android:background="?android:attr/selectableItemBackground"
Or we can use style
style="?android:attr/borderlessButtonStyle" for transparent and shadow less background.
You can achieve that by setting the colors alpha channel.
The color scheme is like this #AARRGGBB there A stands for alpha channel(transparency), R stands for red, G for green and B for blue.
Step 1:
Create a new resource file in drawable and copy paste
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:color="#fff" android:width="2dp"/>
<corners android:radius="25dp"/>
<padding android:right="15dp" android:top="15dp" android:bottom="15dp" android:left="15dp"/>
</shape>
save it as ButtonUI(let's say)
Step 2: Apply the UI to the button xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="join the crew"
android:background="#drawable/ButtonUI"
android:textColor="#fff"/>
I'd say extend Borderless style.
<style name="Button" parent="Widget.AppCompat.Button.Borderless">
<item name="android:textColor">#color/blue</item>
<item name="android:textAllCaps">true</item>
</style>
Code:
button.setVisibility(View.INVISIBLE);
Xml:
android:background="#android:color/transparent"
You can do it easily by adding below attribute in xml file. This code was tested plenty of time.
android:background="#android:color/transparent"
You apply the background color as transparent(light gray) when you click the button.
ButtonName.setOnClickListener()
In the above method you set the background color of the button.

Categories

Resources