How do I create an edit text with currency on left and value on right.
this is the image
Currency can also be part of edit text? or currency is a different edit text?
I hope it can only be 1 edit text.
How do I create an edit text with currency on left and value on right.
this is the image
Currency can also be part of edit text? or currency is a different edit text?
I hope it can only be 1 edit text.
Why not create a LinearLayout that is horizontally oriented and align a TextView to the left and an Edit Text on the right?
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);
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.
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.
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.
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" />
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