3

Because I would like to make the same, like Whatsapp, it puts message and hour in the same line

enter image description here

or in another line.

Sometimes there is space and Whatsapp puts the hour in same line. However sometimes there is not space and Whatsapp puts the hour in other line.

Inside or outside...

Any idea?

Cabezas
  • 9,329
  • 7
  • 67
  • 69
  • 1
    I haven't tried this, so not putting it in an answer. I would expect wrapping the time in a right-aligned span to work. If there is no space it is right aligned on a new line, else it is right aligned in-line. http://stackoverflow.com/questions/6654022/multiple-alignment-in-textview – Kevin Feb 08 '16 at 15:02

2 Answers2

1
public static final String TAG = "MainActivity";
private TextView mText;
private RelativeLayout relativeLayout;
private Boolean mFirstTime = true;
private static final int WIDH_HOUR = 382;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final int width = getScreensWidh();

    mText = (TextView) findViewById(R.id.activity_main_text);
    relativeLayout = (RelativeLayout) findViewById(R.id.activity_main_relative);

    mText.setText("aaaaa dfsafsa afdsfa fdsafas adfas fdasf adfsa dsa aaaa dfsafsa afdsfa fdsafas adfas fdasf adfsa");

    ViewTreeObserver vto = mText.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (mFirstTime) {
                Layout layout = mText.getLayout();
                int lines = layout.getLineCount();

                int offset = layout.layout.getLineWidth(lines - 1);
                int freeSpace = width - offset;

                TextView hour = new TextView(MainActivity.this);
                hour.setText("12:20");
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                if (freeSpace > WIDH_HOUR) {
                    params.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.activity_main_text);
                } else {
                    params.addRule(RelativeLayout.BELOW, R.id.activity_main_text);
                }
                hour.setLayoutParams(params);
                relativeLayout.addView(hour);
                Log.d(TAG, String.valueOf(freeSpace));
                mFirstTime = false;
            }

        }
    });


}

public int getScreensWidh() {
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    return size.x;

}

Two Public Methods

Return the number of lines of text in this layout.

Gets the unsigned horizontal extent of the specified line, including leading margin indent and trailing whitespace.

Community
  • 1
  • 1
Cabezas
  • 9,329
  • 7
  • 67
  • 69
0

I think you can give textView.getLayout().getLineWidth(lineNum) a try (lineNum is obviously the last line). Then compare it to the parent layout width, and if the difference is bigger than timestamp.getWidth() plus some extra padding, then you display it on the same line.

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71