AlvaroSantisteban
AlvaroSantisteban

Reputation: 5336

EditText's cursor horizontally

Is it possible to have the cursor of an EditText horizontally disposed and not vertically? I would like to have it like in the following image:

horizontal cursor

I have read some topics were people play with the android:textCursorDrawable, but I need it to style it for Android < 3.0. Any ideas?

EDIT: I am adding the xml of the EditText:

<EditText
    android:id="@+id/edittext1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:cursorVisible="true"
    android:ems="10"
    android:hint="@string/id"
    android:inputType="text"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:textCursorDrawable="@drawable/custom_bottom_cursor" />

Upvotes: 3

Views: 914

Answers (3)

Jorge Casariego
Jorge Casariego

Reputation: 22212

Here is another example on how I implemented this screen:

enter image description here

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:layout_width="53dp"
        android:layout_height="67dp"
        android:inputType="numberDecimal"
        android:background="@drawable/background"
        android:textCursorDrawable="@drawable/line"
        android:cursorVisible="true"
        android:gravity="center"
        android:maxLength="1"
        android:textSize="40sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Background

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF" />
    <stroke
        android:width="2dp"
        android:color="#777777" />
    <corners
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp" />
</shape>

Horizontal Line

<shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="rectangle" >
    <size android:width="20dp"/>
    <solid android:color="#9C27B0"/>
    <padding
        android:top="-47sp"
        android:bottom="-5sp"
        android:left="10dp"/>
</shape>

enter image description here

It's necessary to update the padding of the Horizontal Line if you change the height & width of the EditText

Upvotes: 0

jyoonPro
jyoonPro

Reputation: 1681

1) Create a Cursor drawable.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <size 
        android:width="20dp"
        android:height="1dp" />
    <solid
        android:color="#000000" />
    <padding 
        android:top="-20sp"
        android:bottom="1sp" />


</shape>

or however you like.
2) android:textCursorDrawable="@drawable/cursor_drawable"
But this only works with Honeycomb or higher...
Success

Upvotes: 2

Matej Špil&#225;r
Matej Špil&#225;r

Reputation: 2687

I was looking for something similar , but I did not found a solution :( only one idea I had found was to set cursorVisible to false

editText.setCursorVisible(false);

then dynamically append char _ to end of edittext, a set blinking animation to end of

Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(50); //You can manage the time of the blink with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
lastChar.startAnimation(anim);

Upvotes: 2

Related Questions