void
void

Reputation: 207

Aligning TextViews in same row

I have two TextViews aligned in the same row. If text in both TextViews are small, they fit in same row in the screen. But if the text in the left TextView (employee name) increases, the text in the right TextView (employee type) is shifted to the second line, but to the right side of the screen. How do I make the second TextView aligned to the left and in the second line (below employee name) if the employee name in left TextView is increased and both TextViews do not fit in same row?

Please check the below screenshot : current behaviour

Please find my code below:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="#fff"
  android:orientation="horizontal"
  android:padding="10dp" >

<TextView
    android:id="@+id/employee_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="22.0sp" />

<TextView android:id="@+id/employee_type"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="8dp"
    android:layout_toRightOf="@id/employee_name"
    android:layout_alignBaseline="@id/employee_name"
    android:textSize="15.0sp" />

 </RelativeLayout>

Upvotes: 0

Views: 238

Answers (3)

Kevin Coppock
Kevin Coppock

Reputation: 134664

The answer is to use a single TextView, and modify the characteristics using Spans.

Assuming you only had the TextView with @+id/employee_name:

TextView employeeInfo = (TextView) findViewById(R.id.employee_name);

String employeeName = // get your employee name
String employeeType = // get your employee type

// Assuming you move your 15sp into a dimens.xml resource file
int employeeTypeSize = getResources().getDimensionPixelSize(R.dimen.employee_type_text_size);

SpannableStringBuilder builder = new SpannableStringBuilder(employeeName);
builder.append(employeeType);

// Set the smaller text size from the end of the name to the end
// of the string.
builder.setSpan(new AbsoluteSizeSpan(employeeTypeSize), 
                employeeName.length(), builder.length(), 
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

// Then set this spannable to the TextView
employeeInfo.setText(builder);

Upvotes: 0

Try this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="#fff"
  android:weightSum="2"
  android:orientation="horizontal"
  android:orientation="horizontal"
  android:padding="10dp" >

<TextView
    android:id="@+id/employee_name"
    android:layout_width="match_parent"
    android:weightSum="2"
    android:weight="1"
    android:layout_height="wrap_content"

    android:textSize="22.0sp" />

<TextView android:id="@+id/employee_type"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weight="1"
    android:paddingLeft="8dp"
    android:textSize="15.0sp" />

 </LinearLayout>

Upvotes: 0

Karakuri
Karakuri

Reputation: 38595

I suggest one of the following:

  1. Always place the employee type on the second line, to avoid problems like this.
  2. Find some other way to indicate the employee that doesn't involve spelling out the type. For example, perhaps use some kind of symbol that doesn't take up so much space. That way even if the name gets very long, you can still leave room for a small symbol.

Upvotes: 1

Related Questions