How To Create A Edit Text With A Permanent Hint Inside It
I have a edit text in which the user enters amount. What I want to do is set a textview value before it that is not editable by the user like 'INR' and then the user will enter the
Solution 1:
Try to use RelativeLayout which contains a TextView and an EditText. I did something below, try it.
<RelativeLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:background="@android:drawable/editbox_background" ><TextViewandroid:id="@+id/constant_text"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_alignParentLeft="true"android:layout_centerVertical="true"android:gravity="center_vertical|center_horizontal"android:textStyle="bold"android:textColor="#C0C0C0"android:text="INR"android:background="@android:color/transparent" /><EditTextandroid:id="@+id/amount"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_toRightOf="@+id/constant_text"android:textColor="#000000"android:textStyle="bold"android:background="@android:color/transparent"android:paddingLeft="5dp"android:text="100 000" /></RelativeLayout>
Solution 2:
You can use a RelativeLayout contaiing a TextView(for the hint) and an EditText for text input. This code shows the example:
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/ll_parent"android:layout_width="fill_parent"android:layout_height="fill_parent" ><TextViewandroid:id="@+id/textView1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:text="PasswordHint"android:textColor="#aaaaaaaa"android:textSize="30dip" /><EditTextandroid:id="@+id/editText1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignBaseline="@+id/textView1"android:layout_alignBottom="@+id/textView1"android:layout_alignParentLeft="true"android:background="#00000000"android:textSize="30dip" ></EditText></RelativeLayout>
Solution 3:
My suggestion is just use a background picture, which draws the text you want. Use the 9-patch to limit where user can input. This is not hint, but it can sloved your problem.
Solution 4:
MyEditText.java
package com.example.myapplication24.UI;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import com.example.myapplication24.R;
publicclassMyEditTextextendsandroidx.appcompat.widget.AppCompatEditText {
privateStringmhint="";
private Paint mPaint;
private Context mContext;
privatefloatpaddingLeft=0 ;
publicMyEditText(Context context) {
super(context);
this.mContext = context;
init();
}
publicMyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
init();
TypedArraya= mContext.obtainStyledAttributes(attrs,R.styleable.MyEditText);
mhint = a.getString(R.styleable.MyEditText_mhint);
setPadding((int)(mPaint.measureText(mhint)),getPaddingTop(),getPaddingRight(),getPaddingBottom());
a.recycle();
}
publicMyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext = context;
init();
TypedArraya= context.obtainStyledAttributes(attrs,R.styleable.MyEditText,defStyleAttr,0);
mhint = a.getString(R.styleable.MyEditText_mhint);
setPadding((int)(mPaint.measureText(mhint)),getPaddingTop(),getPaddingRight(),getPaddingBottom());
a.recycle();
}
privatevoidinit() {
mPaint = newPaint();
mPaint.setTextSize(getTextSize());
mPaint.setColor(Color.GRAY);
paddingLeft = getPaddingLeft();
}
privateintdp2px(float dp) {
return (int) (mContext.getResources().getDisplayMetrics().density * dp + 0.5f);
}
@OverrideprotectedvoidonDraw(Canvas canvas) {
//Drawing mhint
canvas.drawText(mhint,dp2px(5),getHeight()*0.65f,mPaint);
super.onDraw(canvas);
}
}
layout.xml
<com.example.myapplication24.UI.MyEditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"app:mhint="Username: "android:text="asd"
></com.example.myapplication24.UI.MyEditText>
attrs.xml
<!--my hint--><attrname="mhint"format="string"/><declare-styleablename="MyEditText"><attrname="mhint"/></declare-styleable></resources>
Hope it can be helpful for latecomers...
Post a Comment for "How To Create A Edit Text With A Permanent Hint Inside It"