Paulo Ferreira
Paulo Ferreira

Reputation: 421

Add label inside a TextView on android

Is it possible to add a TextView inside a EditText?

Upvotes: 3

Views: 36599

Answers (2)

Aleadam
Aleadam

Reputation: 40391

I'm guessing you want a TextView label and an edit box TextEdit...

LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView label = new TextView(this);
EditText edit = new EditText(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
     LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
label.setText("My Label");
edit.setText ("My Edit");
ll.addView(label, layoutParams);
ll.addView(edit, layoutParams);
setContentView(ll);

You can find the API here:

http://developer.android.com/reference/android/widget/EditText.html

http://developer.android.com/reference/android/widget/TextView.html

http://developer.android.com/reference/android/widget/LinearLayout.html

You can also add those as xml files. There's a very simple tutorial (that you could easily adapt to this) here: http://developer.android.com/guide/tutorials/views/hello-linearlayout.html

Upvotes: 3

Kevin Coppock
Kevin Coppock

Reputation: 134664

With your edit, I'm assuming what you're looking for is EditText's android:hint attribute. This will add a slightly transparent caption inside the EditText which will disappear upon pressing it.

Upvotes: 7

Related Questions