My question is, how to put a label in a editBox in android ?
Like for example, i want to put "To:" in editbox of a contact picker which will not get deleted even if I press backspace on the onscreen keyboard.
I tried with android:hint, but it gets deleted when the editBox is focus or clicked.
I tried with image but it's not looking good. So, I need a method by which i can implement this label thing.
See the visual diagram
I give you two ideas to do this :
If you only need this in a couple of places, you can use a FrameLayout / merge to have a TextView over your EditText. Then using a padding on the edit text, you can make it seem like the TextView is "inside" the EditText. :
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="40dp" >
<requestFocus />
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="10dp"
android:text="To : " />
</FrameLayout>
Else you can inplement your own version of EditText, by writing your own Class. Here's a basic example, you'd need to tweak it a little :
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.EditText;
public class LabelledEditText extends EditText {
public LabelledEditText(Context context) {
super(context);
mPaddingLeft = getPaddingLeft();
}
public LabelledEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mPaddingLeft = getPaddingLeft();
}
protected void onDraw(Canvas canvas) {
TextPaint textPaint = getPaint();
Rect size = new Rect();
textPaint.getTextBounds(mLabel, 0, mLabel.length(), size);
setPadding(mPaddingLeft + size.width(), getPaddingTop(), getPaddingRight(), getPaddingBottom());
super.onDraw(canvas);
canvas.drawText(mLabel, mPaddingLeft + size.left, size.bottom + getPaddingTop(), textPaint);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
private String mLabel = "To : ";
private int mPaddingLeft;
}
You could always have a TextView + EditText in a LinearLayout that looks like an EditText like below:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/edit_text" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="To:" />
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="3dp"
android:background="@null" />
</LinearLayout>
Try this
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" >
<RelativeLayout
android:id="@+id/linearlayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="To:" />
<EditText
android:id="@+id/editTextTo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:background="@android:color/transparent"
android:hint="Type your text..."
android:singleLine="true"
android:textColor="#000000" />
</RelativeLayout>
</LinearLayout>
In android the label is known as TextView:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To:"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
EditText has no label so you need both. Thankfully ViewGroups make this relatively painless for the developer. The EditText uses fill_parent attribute so is presented tight to the "To: " label.
Then use this...
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TO:"
/>
</LinearLayout>
You can add like this
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen);
EditText edittext = (EditText)getViewById(R.id.edittextid);
edittext.setText("To:")
}
Then you will see the text when ever you starting the activity.
I hope it helps....
You can use Here FrameLayout, Within which you can use 3 widgets :
1.TextView with set Text "TO"
Next to TextView You can have Edittext where you can enter the Data
Last is ImageButton Showing Cross Sign...
Set Background image of the FrameLayout with rounded rectangle,,,,
Rounded Text View
<LinearLayout
android:id="@+id/linearLayoutSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:padding="5dp" >
<RelativeLayout
android:id="@+id/linearlayout2"
android:layout_width="fill_parent"
android:layout_height="36dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/rounded_textview"
android:paddingLeft="5dp" >
<TextView
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_centerVertical="true"
android:alignParentLeft="true"
android:text="To:" />
<EditText
android:id="@+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:background="@android:color/transparent"
android:hint="Enter your text..."
android:imeOptions="actionDone"
android:singleLine="true"
android:textColor="#000000" />
<ImageView
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_centerVertical="true"
android:alignParentRight="true"
android:src="@drawable/stop" />
</RelativeLayout>
</LinearLayout>
rounded_textview.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<corners
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp" />
</shape>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TO:"
/>
来源:https://stackoverflow.com/questions/11082462/label-in-a-editbox-in-android