Add label inside a TextView on android

35,435

Solution 1

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

Solution 2

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.

Share:
35,435
Paulo Ferreira
Author by

Paulo Ferreira

Updated on July 16, 2020

Comments

  • Paulo Ferreira
    Paulo Ferreira almost 4 years

    Is it possible to add a TextView inside a EditText?