Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Label in a editbox in android

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 required design

like image 780
akashPatra Avatar asked Jun 18 '12 12:06

akashPatra


People also ask

What is label in android?

Editable items in an app allow users to enter text. Each editable item should have a descriptive label stating its purpose. Android offers several ways for developers to label Views in an app's user interface. For editable items in an interface, some of these ways of labeling can improve accessibility.

How do I fix this item may not have a label readable by screen readers?

Turn on TalkBack. Open the app. Use linear navigation gestures to move accessibility focus to each element on the screen. If TalkBack moves focus to some element, but doesn't speak a meaningful representation of that element, or speaks an "unlabeled" message, that element might be missing a content label.


2 Answers

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>
like image 71
user Avatar answered Nov 15 '22 14:11

user


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;

}
like image 36
XGouchet Avatar answered Nov 15 '22 16:11

XGouchet