Change Border Color of Edittext - android

I tried making a 9 patch image and setting that as background as many solutions suggest but that didn't change the border color when the edittext is selected. In fact, there is no border now, just the image of the edittext. How can I simply change the border color of it? To be for example blue instead of yellow.

Try this :
According to Vikram's answer
Create a xml file with the following in drawable (say
backwithborder.xml):
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#00000000" />
<stroke android:width="1dip" android:color="#ffffff" />
</shape>
and for the EditText user attribute
android:background="#drawable/backwithborder"
Other answers that may help you :
Change edittext border color
How to set border color for EditText

You need to make a selector when the Edittext is focused or selected the image changes of the Edittext.
sample:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:state_pressed="true"
android:drawable="#android:drawable/edittext_pressed"/> //9patch for pressed
<item
android:state_focused="true"
android:drawable="#drawable/edittext_focused"/> //9patch for focused
<item
android:drawable="#android:drawable/edittext_normal"/> //9patch for normal
</selector>
This selector is then added in the background of the Edittext.

Use the below step to change the border color.
editText.Background.SetColorFilter(Color.Red);

Related

Android: change color of spinner's popup scrollbar

How can I change the color of the spinner's popup scrollbar?
For some reason the color of the scrollbar is currently white on a white background, it is not visible.
add android:scrollbarThumbVertical="#drawable/yourdawable //in your spinner
yourdrawable.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="your color" android:endColor="your color"
android:angle="45"/>
<corners android:radius="6dp" />
</shape>
I found this solution for changing the scrollbar color globally:
How can I change the color of the scrollbar in Android?
You can change the style of spinner to change the spinner color.
Make a style in style.xml like below:
<style name="App_SpinnerStyle" >
<item name="android:scrollbarThumbVertical">#color/colorAccent</item>
</style>
then in xml of spinner :
android:popupTheme="#style/App_SpinnerStyle"
Tried and tested!!

Android: programmatically changing to a solid xml shape makes text disappear

I have a Button in my layout whose background and textcolor are defined as selectors. When unpressed the button has a background color and a textcolor and when pressed another color and another text color - e.g. white background with black text -> black background with white text.
At a certain point in my code I need to replace the background/text color with a third set of colors and then go back to the original selector defined in the xml. However, after going back the text no longer appears and instead I get just a solid colored button.
<Button
android:id="#+id/somebutton"
android:layout_width="80"
android:layout_height="80"
android:textColor="#color/sometext_selector"
android:background="#drawable/somebackground_selector"
android:gravity="center"
android:text="#string/sometext"
android:textSize="15sp"
/>
At this point everything is fine, but when I do this:
someButton.setBackgroundDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.backgroundRed, null));
someButton.setTextColor(getResources().getColor(android.R.color.holo_red_light));
and then afterwards this:
someButton.setBackgroundDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.originalBackgroundSelectorDefinedInXml, null));
someButton.setTextColor(getResources().getColor(R.color.originalTextSelectorDefinedInXml));
is when the problems begin.
Here are the xmls for my selectors - apologies for pseudocode, just imagine appropriate hex values:
First - the background:
<item android:drawable="#drawable/color_purple_full" android:state_selected="true"/>
<item android:drawable="#drawable/color_purple_full" android:state_pressed="true"/>
<item android:drawable="#drawable/color_purple_full" android:state_focused="true"/>
<item android:drawable="#drawable/color_purple_outline"/>
color_purple_outline:
android:shape="oval">
<stroke android:width="2dp" android:color="#somepurplehexa" />
<size android:width="80dp" android:height="80dp"/> </shape>
color_purple_full:
android:shape="oval">
<solid android:color="#somepurplehexa"/>
<size android:width="80dp" android:height="80dp" /> </shape>
for text:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#android:color/black" android:state_selected="true"/>
<item android:color="#android:color/black" android:state_pressed="true"/>
<item android:color="#android:color/black" android:state_focused="true"/>
<item android:color="#color/green"/>
What I'm seeing is the correct background in the unpressed state but in the pressed state I'm just getting a solid color with no text on it at all. I'd be grateful for any ideas as to why this is happening and why in the original xml it works fine but only stops working after changing it programmatically?
SOLVED:
turns out the problem was here:
someButton.setTextColor(getResources().getColor(R.color.originalTextSelectorDefinedInXml));
should have been
someButton.setTextColor(getResources().getColorStateList(R.color.originalTextSelectorDefinedInXml));

setting edittext border when is on focus

its something you may think is duplicated question but i could not found simple solution .
i want edittext to be with border when focus .
this is what i try'd , if you have simple or better solution i will be happy to read .
define shape xml :
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#80000000" />
<stroke android:width="2dp" android:color="#5480F7" />
<padding android:left="1dp" android:top="1dp" android:right="1dp"
android:bottom="1dp" />
</shape>
this simple <shape> create border with transparent background .
define xml for edittext :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/edittext_border" android:state_focused="true" />
<item android:drawable="#drawable/notfocus"/> <!-- default -->
</selector>
the problem here is that the xml that create the border is insert to my edittext as drawable meaning that i cant change the background image for the edittext .
i want my edittext to be with spcefic background image and when focus i want the image to stay but will be with border .
thanks .
You can achieve it by two ways:
By creating two different image for the EditText.
By creating two different shape color for the EditText.
By two different image:
Just follow Siddhesh comment to achieve this.
By Different shape for EditText:
you have done right with this way. but i think you have enter wrong background color code to solid attribute.
add:
<solid android:color="#00000000" />
instead of:
<solid android:color="#80000000" />
Hope this help you. If not then let me know. Will like to help you.
Enjoy Coding... :)
make One background image with border and other will be just background of focus set with border else without border.

How to change textColor of a button from XML in Android?

I am using a selector to change the background drawable of the button in different states (focused, pressed, normal). Is there any way to change the text color too? I want to provide various text colors for various button states, but I want to do it from the xml. Is this possible?
Yes it can be done. You just do the same way you do for the button drawable. Then assign it to android:textColor="#drawable/yourselector"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#000000" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#000000" /> <!-- focused -->
<item android:color="#FFFFFF" /> <!-- default -->
</selector>
Try combining the above with the android:drawable attribute.
If it's programmatically, you could use:
Button button = findViewById(R.id.yourbutton);
button.setTextColor(Color.yourcolor);
In xml it'll be the same thing.
Use android:color="#ff0000" in selector for focused and another color for default state.Then in xml of button android:textColor="#drawable/yourselector"

Make TextView turn orange when focused?

I have a textview, I set it as clickable and focusable - how do I get it to highlight to orange (like a button) when the user focuses it with the trackwheel etc?:
TextView tv = ...;
tv.setClickable(true);
tv.setFocusable(true);
Thanks
This is quite easy. Here is the solution.
You have an TextView element with its background attribute set to #drawable/tbselector like this.
<TextView android:text="My text"
android:id="#+id/tv01"
android:layout_width="300dip"
android:layout_height="150dip"
android:layout_gravity="center_horizontal"
android:background="#drawable/tbselector"/>
The last attribute android:background is essential the other stuff is up to you.
Now you create a tbselector.xml in your drawable subdirectory. Which looks like this.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/bgdefault"
android:state_focused="false"
android:state_selected="false"/>
<item
android:drawable="#drawable/bgselected"
android:state_focused="true"
android:state_selected="false"/>
</selector>
Now you create a bgdefault.xml in your drawable subdirectory which looks like this.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="200dip"
android:height="150dip"
android:color="#00FF00"/>
<solid
android:color="#00FF00"/>
</shape>
Finally create a bgselected.xml in your drawable subdirectory which looks like the other one with other color values like this for example.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="200dip"
android:height="150dip"
android:color="#FFFF00"/>
<solid
android:color="#FFFF00"/>
</shape>
And thats it you now have a state dependent TextView background. You can however decide to set your drawables in your selector XML it's totally up to you. My values are just random values to show you the difference.
Hope it helps.
I have no chance to try it at the moment, but what about adding a OnFocusChangeListener to your TextView and then use the setBackgroundColor(int color) method to change the background to orange

Categories

Resources