That is, if userName is empty, then messagetext has shifted in its place

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="13dp" android:id="@+id/userName" android:textStyle="bold" android:gravity="left"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Large Text" android:textStyle="bold" android:textSize="11.5dp" android:id="@+id/messagetext" android:gravity="top|left|center"/> 

  • Does the 2nd textbox need only to display an inscription instead of an empty 1st textbox? - Igor Viskov
  • 1) the case we have a userName and there is a messagetext - everything is fine, everyone takes up his line. 2) the case the userName field is empty and there is a messagetext Right here, so that there is no empty space from the top of userName, you must shift the messagetext - qwend

2 answers 2

It is necessary to set these fields id, then in the code, when setting text into these fields, determine whether any text is set in the userName field or not, if not, set the visibility property for this GONE field. The code will look something like this.

 TextView userName = (TextView)findViewById(R.id.userName); TextView message = (TextView)findViewById(R.id.message); String userNameStr = null; String messageStr = null; //код по получению данных для полей if(username == null){ userName.setVisibility(View.GONE); } else { username.setText(userNameStr); } //остальной код 

Well, you can either initially set the visibility for the GONE field in the markup, and then set the visibility of the visible field when setting the value in this field.

  • And with the help of markup it can not be done for example using the width? - qwend
  • one
    I do not know, I do as described above, if you want, you can experiment. In my opinion, this is a fairly simple and intuitive option, it’s all the same that you install data there, set the visibility flag during data installation - temq

There is a special attribute android:hint , in which the text is written will be displayed if the textbox is empty

 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="13dp" android:id="@+id/userName" android:textStyle="bold" android:hint="@string/myText" android:gravity="left"/> 

Update

If you really just want to write the value of the 2nd textbox, you can simply apply:

 TextView userName = (TextView)findViewById(R.id.userName); TextView message = (TextView)findViewById(R.id.message); userName.setHint(message.getText());