EditText - highlight text inside with specified color - android

All I want to do is to highlight text inside an EditText like this
Is there any good way for it?

i think here and here can help you
you need selector and aandroid:selectAllOnFocus="true"
<EditText
android:id="#+id/tvOrdinanceTitle"
android:layout_width="wrap_content"
android:cursorVisible="false"
android:layout_height="wrap_content"
android:background="#color/etxt_color" >
</EditText>
and :
<?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>

You need to call the "selectAllOnFocus" method either in XML or programatically.
To do so in the XML, use the following example:
<EditText
android:id="#+id/group_name"
android:text="Group Name"
android:selectAllOnFocus="true"/>
You could also do it programmatically, reference the edit text in your code and call the selectAllOnFocus() method.
EditText edittext = ...;
edittext.setText("Group Name ");
edittext.selectAllOnFocus(); //You can also use "edittext.selectAll();"
Make sure you are using "android:text = " rather than "android:hint = ". Hint cannot use selectAllOnFocus and is not supposed to.
android:text will automatically disappear if you start typing while focused, but will stay if you click it again.

Related

Change drawable when a field gains focus

I have severalEditTextfields in my layout xml. All of them follow this pattern:
<EditText
style="#style/EditText"
android:id="#+id/email"
android:hint="Email Address"
android:inputType="textEmailAddress"
android:drawableEnd="#drawable/icon1" />
If this field gains focus, I want to set the drawableIcon to icon1 and if it loses focus, I want to set it to icon2.
I know I can do it using setOnFocusChangeListener in my activity.
But I want to ask is it possible to do it using XML only?
It can be easily done using simple selectors once you create a custom drawable.
Check this tutorial where it applies what you want to a Button.
http://www.mkyong.com/android/android-imagebutton-selector-example/
Something like this should work for you:
<EditText
style="#style/EditText"
android:id="#+id/email"
android:hint="Email Address"
android:inputType="textEmailAddress"
android:background ="#drawable/customDrawableIcon" />
res/drawable/customDrawableIcon.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/icon1"
android:state_focused="true" />
<item android:drawable="#drawable/icon2"
android:state_focused="false" />
</selector>
I think it is possible, you need to create a selector xml for Edittext in your drawable folder, check this post he gave a good answer for this.
https://stackoverflow.com/a/14543476/1007087
You can use selector and change the state_focused drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="#drawable/focusedIcon"/>
<item android:drawable="#drawable/normalicon"/>
</selector>
so now the android:drawableEnd should refer to the previous selector.
Hope this will help.

Show Effect on focus/click on Android Buttons

I would like to adding some effects like color change to my android button when user focus/click the button
My android button layout is here :
<Button
android:id="#+id/btnUpload"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#color/btn_bg"
android:text="தகவல் அனுப்புக"
android:textColor="#color/white"
android:padding="20dp"
android:margin="20dp"
android:layout_alignParentBottom="true" />
You should check out the documentation regarding color state lists.
Create an XML file in your drawable folder with the name of btn_background and place the following code in the file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
After you have created this file, change the background attribute of your Button to point to the new background:
android:background="#color/btn_background"
Good luck and happy coding!

Android checkbox is not visible well in black background prob

I want make a list of Checkbox with white text in black background. We are using following code:
CheckBox chkAdditionalPack = new CheckBox(MainActivity.this);
chkAdditionalPack.setTag(j);
chkAdditionalPack.setText(offerPackageListForAddl.get(j).getOfferPackageName().toString());
chkAdditionalPack.setTextColor(Color.WHITE);
It gives a view like below:
The problem is now boxes of checkboxes is not clearly visible. How can I make it clearly visible keeping intact other parts?
If you are using android-studio, select your checkbox, and in the properties, set 'buttonTint' property value to whatever color you want using the editor. Or, if you prefer the XML solution, use
android:buttonTint="#color/white"
Note: This assumes that the color white is defined.
try the below xml code ref from solution
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="#drawable/cbchk_blue"
android:state_focused="false" >
</item>
<item
android:state_checked="true"
android:drawable="#drawable/cbchk_blue"
android:state_focused="true" >
</item>
<item
android:state_checked="false"
android:drawable="#drawable/cbunchk_blue"
android:state_focused="false" >
</item>
<item
android:state_checked="false"
android:drawable="#drawable/cbunchk_blue"
android:state_focused="true" >
</item>
</selector>
One really simple way to do it, within the xml for the checkbox, simply set the android:background variable to the colour you wish.
Instead of having the text and check box created together like this, simply create a custom list item layout xml file.
e.g custom_list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
/>
</LinearLayout>
Simply use this :
android:buttonTint="#FFFFFF"
** replace #FFFFFF with any color

disabling editText but with a different color

<EditText
android:id="#+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:enabled="false" >
</EditText>
I have the above edit text which is disabled. I only take values from somewhere else and insert it there, the user can not type inside of it. However, the color of the edit text when in disabled form is too gray and not visible to the user sometimes. I prefer it to appear black. Is there any basic functionality I am not aware than can change the color of edit text when in disabled mode ?
Any other work around works fine too.
Your layout:
<EditText
android:id="#+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:enabled="false"
android:background = "#drawable/edittextdrawable.xml">
</EditText>
edittextdrawable.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="#drawable/PUT DRAWABLE HERE" />
<item android:state_enabled="true"
android:drawable="#drawable/PUT DRAWABLE HERE" />
</selector>
Try to use statelist xml drawable, and set this to your EditText's background:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"
android:drawable="#drawable/bankszamlak_pressed" /> <!-- pressed --> <item android:state_focused="true"
android:drawable="#drawable/bankszamlak" /> <!-- focused --> <item android:drawable="#drawable/bankszamlak" /> <!-- default --> </selector>
for further information check this link
Just set text color to black will make it better:
android:textColor="#android:color/black"
You should use TextView if you don't want the user to type into. Then you don't need to use disable to stop user from typing into it.

Remove focus border of EditText

How can I remove the border with appears when focusing an EditText View?
I need it cause this view has little space in the screen, but without the border it's enough. When running on Emulator an orange border appears, on Device a blue one.
Have you tried setting the background of the EditText to a transparent colour?
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/hello"
android:background="#00000000"
/>
<EditText
android:id="#+id/edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background_normal"
/>
It is possible. However I would not recommend it because users are used to certain metaphors and you should not change the general UX.
You can apply different styles to your views. In your case it sounds like you want an EditText View element which looks like a TextView element. In this case you would have to specify other backgrounds for the EditText depending on the state of the View element.
In your desired layout.xml you assign a background to your EditText:
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/hello" android:background="#drawable/custom"
/>
Then you create the custom.xml in your drawable folder and add the following:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/textfield_default" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/textfield_disabled" />
<item android:state_pressed="true" android:drawable="#drawable/textfield_default" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="#drawable/textfield_default" />
<item android:state_enabled="true" android:drawable="#drawable/textfield_default" />
<item android:state_focused="true" android:drawable="#drawable/textfield_disabled" />
<item android:drawable="#drawable/textfield_disabled" />
</selector>
Those are the possible states of your EditText View element. Normally you can access Android platform drawables directly by using #android:drawable/textfield_default, but in this case the textfield drawables are private so you have to copy them into your own drawable folder. The original resources can be found in your SDK installation folder at ANDROID_HOME\platforms\android-(API LEVEL)\data\res\drawable-(*dpi)\.
Once you are done you end up with an EditText which looks like a TextView but completely without those borders. Those orange borders you saw in the emulator are the default Android drawables. The blue ones are vendor specific (possibly Samsung).
Hope that helped and didn't confuse that much.
This is the fastest solution:
<EditText
android:id="#+id/edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
/>
You can keep the background color as transparent to remove the EditText border on focus.
Method 1
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00000000"
/>

Categories

Resources