setHintTextColor() in EditText - android

I have View in which there are two text boxes, and the user can select text color from another view on the same screen (through dialog box).
So when the user changes color via dialog box, I am changing color of EditText text and its hint. But when there is some text is available in EditText after that user selects other color, then that text is coming in that color. But if I remove all that text then the color of HintText is that of the previous color.
For example, currently if I have red color in text box and the user selects green color so text is there in green color. But if I remove that text then hint text are coming in red even if I change hint color in code. This problem only comes when there is some text there. if it is blank and hint text is there then problem is not coming.

Simply add this in your layout for the EditText :
android:textColorHint="#FFFFFF"

Use this to change the hint color. -
editText.setHintTextColor(getResources().getColor(R.color.white));
Solution for your problem -
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,int arg3){
//do something
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
//do something
}
#Override
public void afterTextChanged(Editable arg0) {
if(arg0.toString().length() <= 0) //check if length is equal to zero
tv.setHintTextColor(getResources().getColor(R.color.white));
}
});

Default Colors:
android:textColorHint="#android:color/holo_blue_dark"
For Color code:
android:textColorHint="#33b5e5"

Inside Layout Xml File We can Change Color of Hint.....
android:textColorHint="#android:color/*****"
you can replace * with color or color code.

Seems that EditText apply the hintTextColor only if the text is empty. So simple solution will be like this
Editable text = mEditText.getText();
mEditText.setText(null);
mEditText.setHintTextColor(color);
mEditText.setText(text);
If you have multiple fields, you can extend the EditText and write a method which executes this logic and use that method instead.

Programmatically in Java - At least API v14+
exampleEditText.setHintTextColor(getResources().getColor(R.color.your_color));

This is like default hint color, worked for me:
editText.setHintTextColor(Color.GRAY);

You could call editText.invalidate() after you reset the hint color. That could resolve your issue. Actually the SDK update the color in the same way.

Related

dynamically android button style

I want to give some styles to a Android button dynamically , based on what integer value entered. for example if user enter 25 my button shape will change to "quarter of a circle". like below picture.
quarter of a circle image
I try to use
GradientDrawable shape=new GradientDrawable();
shape.setShape(GradientDrawable.OVAL);
but i can't find useful method for make quarter of Oval
In other words i want to make a clickable pieChart
don't offer me github.io libraries.
Please read this notes.
1. I know how to used <selector> element and change button style based on that's state.
2. I know that how to using <shape> drawable resource. but I need change shape based on what value, user enter.
I am assuming you mean Edit Text View as you can not enter text on it. You can do this programmatically. You need to use instance of button as a reference and add a TextWatcher to your edit text view and add the if statement in there.
Use the following as an example:
Button button = findViewById(R.id.<id of button>);
EditText editText = findViewById(R.id.<id of edit text view>);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i,int i1, int i2) {}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(charSequence.equals("25")){
button.setBackgroundResource(R.drawable.<id of custom button>);
}
}
#Override
public void afterTextChanged(Editable editable) {}
});

How to change hint text size in TextInputLayout

Suppose I want to change text size. I'm doing it in code and it looks like this:
_textInputLayout.EditText.SetTextSize(Android.Util.ComplexUnitType.Dip, 40);
When I write text in the entry it looks like 40dip text. But when entry is empty hint text looks like 16-18dip.
Is there any way to change hint text size?
Changing the final hint size / floating label size is possible via a style and calling SetHintTextAppearance using something like the following:-
_nativeView.SetHintTextAppearance(App6.Droid.Resource.Style.MyTextInputLayout);
Where MyTextInputLayout is something along the lines of:-
<style name="MyTextInputLayout" parent="#android:style/TextAppearance">
<item name="android:textColor">#color/blue</item>
<item name="android:textSize">44sp</item>
</style>
However the textSize from this style is only applied to the final destination, when you start to enter some text in.
From what I can see, and including the properties on the object, it doesn't appear to be possible to change the starting font size of the hint unfortunately at the moment?
Where as EditText is exposed, and you can alter things there. The Hint portion is not handled at all by it, and instead by the TextInputLayout. There appears no object exposed to get access to customize this specifically for the Hint.
You can do it by setting a size in the string recource.
For example:
<string name="edittext_hint"><font size="15">Hint here!</font></string>
then in your XML just write
android:hint="#string/edittext_hint"
This will resault in a smaller text for the hint but the original size for the input text.
Or like this:
MYEditText.addTextChangedListener(new TextWatcher(){
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int start, int before,
int count) {
if (arg0.length() == 0) {
// No entered text so will show hint
editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mHintTextSize);
} else {
editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mRealTextSize);
}
}
});

How can I change color state of an ImageView based on an empty test of an EditText line?

When the EditText line is blank (the default), I'd like the ImageView icon color to be white.
When the icon is pressed, I'd like the color to be black and then revert back to white because the press clears the EditText line, so it would then again be empty.
With the ImageView, I first set src equal to a white drawable. Any thoughts on how to switch the color state based on the length test of the EditText line?
I'm not too exactly sure what the question is asking for, I got confused when you brought in ImageView and hover because I don't believe phones support the onHoverListener as well.. Do you mean changing the EditText accent color when you click on it to type something? If so it will be under the colors.xml-->colorAccent
Also if you mean to change an images background color
public void onClick(View v) {
imageView.setBackgroundColor(Color.parseColor(EditText.getText().toString()));
}
Hopefully I provided you with some help to continue your project.
To update icon if textview empty, check Android: How can I validate EditText input? with addTextChangedListener() then
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Log.i("Text", charSequence + "");
if(charSequence.length()==0)
ib.setImageResource(R.drawable.button_unpressed);
else
ib.setImageResource(R.drawable.button_pressed);
}
and to change imagebutton background follow its state How to set image button backgroundimage for different state? with "create an xml in your drawable"
If you want to keep color of icon remain black after pressed, just override onClick() by
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ib.setImageResource(R.drawable.button_pressed);
}
});

Change edittext text color android?

Can you change the edittext text color AFTER it is entered in android? (Take an input, and on the press of a button, change the color of the contents of the edittext!)
Thanks
public void onButtonClick(View v){
EditText et = (EditText)findViewById(R.id.editText);
et.setTextColor(Color.rgb(red, green, blue));
}
On Button Click You can use one of the following :
editText.setTextColor(Color.RED);
editText.setTextColor(Color.parseColor("#FFFFFF"));
editText.setTextColor(Color.rgb(200,0,0));
editText.setTextColor(Color.argb(0,200,0,0));
editText.setTextColor(getResources().getColor(R.color.editTextColor));
editText.setTextColor(0xAARRGGBB);
If you are looking to change the color of all of the text, you can call setTextColor() on the EditText.
If you are looking to change the color of some of the text, you can apply a ForegroundColorSpan to that text. getText() on an EditText should return a SpannableStringBuilder, on which you can call setSpan() as needed.

EditText - have default text that cannot be deleted, and cover text with background after leaving EditText

I am creating an email form and would like to have text in an EditText that cannot be deleted. On the screenshot below, the To could not be deleted.
If anyone has suggestions on how to achieve the above, it would be great - Thanks.
My current code for the To EditText box:
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="0dp"
android:hint="#string/email_to" >
</EditText>
The problem is android:hint text dissappears when the user starts to text, and android:text can be deleted by the user.
How do we have text that cannot be deleted? Thanks.
Note:
Also, I would like to note that I have a method that clears text using a clear button - this works fine - but I am hoping that it would not delete the fixed text (If I got that implemented!).. Here`s the code for that:
private void clearForm(ViewGroup group)
{
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText)view).setText("");
}
if(view instanceof ViewGroup && (((ViewGroup)view).getChildCount() > 0))
clearForm((ViewGroup)view);
}
}
SOLUTION:
Managed a roundabout way of doing this.. I created a TextView and EditText within a nested Linear Layout. I turned off the border in the EditText using android:background="#00000000".
I created an xml file in the drawable folder, and refered to this in the relevant linear layout like this: android:background="#drawable/customxml"
You can do it by using Text Watcher listener.
You can keep text in edittext by checking length of edittext.
For example
editText.setText("To") // it mean have lenght 2
And Than in method afterTextChanged, check if text in editText is has been deleted (with check length of text in editText)
This is for complete example code:
editText.setText("To");
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if(s.length < 2){
editText.setText("To")//set editext with "To" again like has been initialized
editText.setSelection(editText.getText().length)// to make cursor in end of text
}
}
});
Hope this help!
To get the visual appearance you want, include a horizontal LinearLayout containing a text view and an EditView. Turn off the border around the EditView (there's an attribute that does that (I think it's android:shadowColor) ) Play around with margins and padding to get them to be adjacent to each other. Set the background color on the linear layout to put a border around the combined pair.
I wouldn't worry much about efficiency. You aren't nesting very deeply. The biggest challenge is going to be getting it to look like a single view.
Edit: Another thought. If that doesn't work, you could make the "To" a drawable, and set it using the android:drawableLeft attribute.
Add TextView to the right side of EditText in LinearLayout with horizontal orientation or RelativeLayout with android:layout_toRightOf="#id/YourTextView"

Categories

Resources