What I thought would be a fairly straightforward layout is not turning out to be.
I have several rows in a linear layout.
Each row has a “title”, a “tag” and a “description”.
The description appears below the title and tag and is no problem.
The title is of indeterminate length and should be aligned to top left. The tag should appear appear aligned to the right of the title (not necessarily the right edge of the parent, if the title is short). If there’s not enough room for both on a single line, the title should wrap and leave space for the tag.
The tag has it’s own style and behavior, and must be a separate element from the title.
In my limited experience, I’ve had the most luck with RelativeLayouts, so that was my first try:
import android.content.Context;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ExampleRow extends RelativeLayout {
private TextView title;
private TextView description;
private TextView tag;
public ExampleRow(Context context) {
super(context);
LayoutParams lp;
lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
title = new TextView(context);
title.setId(1);
addView(title, lp);
lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, title.getId());
description = new TextView(context);
description.setId(2);
addView(description, lp);
lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.RIGHT_OF, title.getId());
lp.addRule(RelativeLayout.ABOVE, description.getId());
lp.setMargins(20, 0, 0, 0);
tag = new TextView(context);
addView(tag, lp);
}
}
Long titles would force the tag off-screen.
I then tried nested LinearLayouts – the main container as a LinearLayout with vertical orientation, that had a “top row” that was a horizontal LinearLayout containing the title and the tag, and the bottom “row” being the description. That had the same issue described above.
Next I tried TableLayout, with 2 TableRows, but this not only failed but also showed strange quirks like not rendering certain rows at all, for reasons I don’t understand. The documentation on TableLayout and TableRow seems a little sparse, and I wasn’t able to find the Java equivalents of XML fields dealing with columns.
The layout needs to be in Java, not XML, for reasons too long and uninteresting to mention.
Any insight is appreciated.
TYIA.
/EDIT
to clarify the requirement, the best example I can think of would be in HTML.
<table>
<tr>
<td>title</td>
<td>tag</td>
</tr>
<tr>
<td colspan="2">description...</td>
</tr>
</table>
the tag is always to the right of the title, but only as far right as it needs to be (depending on title width). the title would wrap once it forced the tag to the most extreme right.
I think this is the layout you’re after:
If you change the text on the title to something shorter you will see that the
tag“follows” thetitle.