0

How do I create an edit text with currency on left and value on right.

this is the image

enter image description here

Currency can also be part of edit text? or currency is a different edit text?

I hope it can only be 1 edit text.

david
  • 2,900
  • 5
  • 28
  • 48

5 Answers5

2

Why not create a LinearLayout that is horizontally oriented and align a TextView to the left and an Edit Text on the right?

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
  • I actually have done this just moments ago, but problem is if its possible to merge this as one then I would do it. – david May 19 '16 at 05:17
2

Use EditText and set this property android:drawableLeft="@mipmap/ic_launcher"

code

   <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@mipmap/ic_launcher"
        android:hint="Add money"
        android:gravity="center"
        android:padding="3dp"/>

If you need to dynamically change this image then use this code inside your java file

params :- left, top, right, bottom

editText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.drawableLeft, 0, 0, 0);
Arpit Patel
  • 7,212
  • 5
  • 56
  • 67
1

Use of SpannableString you can Achieve this.

In activity_main.xml file.

<EditText
        android:id="@+id/edtCurrency"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="Test"
        android:textColor="#000"
        android:textSize="26sp" />

Add this in you MainActivity.java.

 EditText edtCurrency = (EditText) findViewById(R.id.edtCurrency);
 SpannableString spannableString = new SpannableString("USD  123.456");
 spannableString.setSpan(new UsdSpannableSuperScript((float) 1.0), 2, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
 edtCurrency.setText(spannableString);

Here is UsdSpannableSuperScript.java.

public class UsdSpannableSuperScript extends SuperscriptSpan {
    //divide superscript by this number
    protected int fontScale = 2;

    //shift value, 0 to 1.0
    protected float shiftPercentage = 0;

    //doesn't shift
    UsdSpannableSuperScript() {
    }

    //sets the shift percentage
    UsdSpannableSuperScript(float shiftPercentage) {
        if (shiftPercentage > 0.0 && shiftPercentage < 1.0)
            this.shiftPercentage = shiftPercentage;
    }

    @Override
    public void updateDrawState(TextPaint tp) {
        //original ascent
        float ascent = tp.ascent();

        //scale down the font
        tp.setTextSize(tp.getTextSize() / fontScale);

        //get the new font ascent
        float newAscent = tp.getFontMetrics().ascent;

        //move baseline to top of old font, then move down size of new font
        //adjust for errors with shift percentage
        tp.baselineShift += (ascent - ascent * shiftPercentage)
                - (newAscent - newAscent * shiftPercentage);
    }

    @Override
    public void updateMeasureState(TextPaint tp) {
        updateDrawState(tp);
    }
}

Here is Screen.

enter image description here

Community
  • 1
  • 1
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
  • 1
    actually this is the only one that works to my needs, i thought i could separate the image file and add it to the left drawable , but seems like it isnt working for me. thanks for the code i really appreciate it. – david May 19 '16 at 06:57
  • btw if i getText() this , is the dollar sign going to appear? – david May 19 '16 at 06:58
0
  1. With only single EditText can also work. For that you will have to set prefix and fetch value from edittext with respect to the prefixCount.

  2. You can try this way. This is a bit better approach.

           <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/grey"
                android:orientation="horizontal"
                android:padding="10dp">
    
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="USD" />
    
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:background="@null"
                    android:singleLine="true" />
    
            </LinearLayout>
    

Output : Same as image above you mentioned.

  1. With single EditText another approach. You can have a image of currency "USD" and set drawableLeft to edittext.

              <EditText
                    android:id="@+id/edittext"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_margin="10dp"
                    android:drawableLeft="@drawable/dollar_sign"
                    android:drawablePadding="5dp"
                    android:inputType="number"
                    android:padding="10dp"
                    android:singleLine="true" />
    
Ashwini
  • 245
  • 1
  • 7
0
Currency currency = new Currency(Locale.US)
final String usd = currency.getCurrencyCode(); //returns USD

To use, either extend a TextView and manually add this logic, or do so through code

whompum
  • 69
  • 2
  • 12