Reputation: 1172
I have an EditText in my main layout. I also have an app theme, where the default edittext is styled with a custom curdordrawable.
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/introEditText"
android:gravity="top"
android:singleLine="false"
android:text=""
android:inputType="textMultiLine"
android:textSize="24sp" >
<requestFocus/>
</EditText>
<style name="AppTheme" parent="android:Theme.Holo.NoActionBar">
<item name="android:textColor">#ffffff</item>
...
<item name="android:editTextStyle">@style/EditTextStyle</item>
</style>
<style name="EditTextStyle" parent="@android:style/Widget.EditText">
<item name="android:background">@android:color/transparent</item>
<item name="android:textCursorDrawable">@drawable/cursor</item>
</style>
And my custom cursor is:
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<size android:width="2dp" />
<solid android:color="#363636" />
</shape>
The cursor color is correct, although its width isn't. On the first row it's 1 px wide instead of the 2dp. As soon as the text breaks into two lines, the cursor becomes 2dp wide. Also, it is not shown when the edittext has no text. I tested this on 4.4 (HTC One and a Nexus 7) Is this an Android bug?
Upvotes: 3
Views: 1145
Reputation: 156
Actually as the width of the EditText increases, the cursor's size is set masked by some space(can't figure out what it was). So the solution is to fix the EditText width.
<EditText
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/introEditText"
android:gravity="top"
android:singleLine="false"
android:text=""
android:inputType="textMultiLine"
android:textSize="24" />
If you want exact width, use ems like:
<EditText android:ems="10" ...
/>
android:ems sets the width of a TextView to fit a text of n 'M' letters when the text size 12sp
I don't think there's a way around this.
Upvotes: 4
Reputation: 4660
This worked for me in emulator. create an xml file in drawable like you did already:
edit_text_cursor.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="2dip"/>
<solid android:color="#00AAEE"/>
</shape>
then in your layout create it like this:
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:singleLine="false"
android:textCursorDrawable="@drawable/edit_text_cursor"
android:ems="10"
android:inputType="textMultiLine" />
this fixes one pixel cursor in the first.(I tested it):
android:ems="10"
this should be enough, but if you have problem showing the cursor while it's empty. you can add this to EditText to force it:
android:cursorVisible="true"
Upvotes: 1