0

I am working on my android project to input the text in two textviews and display the Inbox text next to the email_subject textview. I have got a problem with the textview because when the text is short and large it will not move the Inbox next to the textview.

Here is what it show:

enter image description here

Here is what I want to achieve:

enter image description here

Here is the content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:orientation="horizontal">

    <FrameLayout
        android:id="@+id/flString"
        android:layout_width="295dp"
        android:layout_height="135dp"
        tools:layout_editor_absoluteX="10dp"
        tools:ignore="MissingConstraints">

        <TextView
            android:id="@+id/email_subject"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="4dp"
            android:layout_marginTop="19dp"
            android:text="Mail delivery failed: returning message to sender"
            android:textColor="#757575"
            android:textSize="20sp"
            android:textStyle="bold" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TextView
                android:id="@+id/mailbox_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/rounded_mailbox"
                android:text="Inbox"
                android:textColor="#757575"
                android:textSize="12sp"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true" />
        </RelativeLayout>
    </FrameLayout>

    <ImageView
        android:id="@+id/ivFavorite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="284dp"
        android:layout_marginTop="16dp"
        android:padding="5dp"
        android:src="@drawable/ic_star_black_24dp"
        app:layout_constraintHorizontal_bias="0.723"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />



    <ImageView
        android:id="@+id/more_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="284dp"
        android:layout_marginTop="54dp"
        android:padding="5dp"
        android:src="@drawable/ic_baseline_3_dots_24"
        app:layout_constraintHorizontal_bias="0.723"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/from_me"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="72dp"
        android:layout_marginTop="8dp"
        android:text="to me"
        android:textColor="#757575"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/from_sender" />


    <ImageView
        android:id="@+id/dropdown_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="114dp"
        android:layout_marginTop="8dp"
        android:src="@drawable/ic_arrow_down"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/from_sender" />
</androidx.constraintlayout.widget.ConstraintLayout>

Can you please show me an example how I can get the Inbox text to display at the end of the text when the text is short and large??

EDIT: When I try this:

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/flString1"
    android:layout_width="295dp"
    android:layout_height="wrap_content"
    tools:ignore="MissingConstraints">

    <TextView
        android:id="@+id/email_subject"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RE: SRX1517391641ID - [EXTERNAL] Re: Reported deliverability problem to Outlook.com SRX1517391641ID "
        android:textColor="#757575"
        android:textSize="20sp"
        android:textStyle="bold"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/mailbox_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="false"
        android:layout_alignParentBottom="false"
        android:layout_marginBottom="83dp"
        android:background="@drawable/rounded_mailbox"
        android:text="Inbox"
        android:textColor="#757575"
        android:textSize="12sp"
        android:gravity="bottom|end" />

</androidx.constraintlayout.widget.ConstraintLayout>

It will not move the inbox text to the end of the text. Any idea??

chris oojer
  • 301
  • 1
  • 10
  • Try to keep a plain hierarchy while using `constraint layout`, removing the `framelayout` and constraining the inbox to the `bottom-end` should solve your case. – Santanu Sur May 16 '21 at 19:54
  • @SantanuSur Thank you for your help. I have replaced `androidx.constraintlayout.widget.ConstraintLayout` with `framelayout`. How I can add `bottom-end` on the inbox textview? – chris oojer May 16 '21 at 20:04
  • @Santanu Sur I have added `android:gravity="bottom|end"` and it doesn't move the inbox textview to the end of the textview. Any idea? – chris oojer May 16 '21 at 20:13

1 Answers1

0

One idea is to play with Spannable like something below

    TextView textView = rootView.findViewById(R.id.email_subject);
    String subject = "RE: SRX1517391641ID - [EXTERNAL] Re: Reported deliverability problem to Outlook.com SRX1517391641ID ";
    subject = subject + "Inbox";
    Spannable wordtoSpan = new SpannableString(subject);
    wordtoSpan.setSpan(new BackgroundColorSpan(Color.LTGRAY), subject.length() - 5,  subject.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    wordtoSpan.setSpan(new StyleSpan(Typeface.NORMAL), subject.length() - 5, subject.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    wordtoSpan.setSpan(new RelativeSizeSpan(0.7f), subject.length() - 5, subject.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    textView.setText(wordtoSpan);

Note: This is just a basic sample to give you some idea to explore on this side if you think it worth exploring.

enter image description here

Note: you can explore https://github.com/googlearchive/android-text/tree/master/RoundedBackground-Kotlin

and can also look into a solution regarding making a span round here https://stackoverflow.com/a/19297086/4828650

Dinkar Kumar
  • 2,175
  • 2
  • 12
  • 22
  • Thank you, it look like if it is the only way forward. I have the drawable code but how I can use your code to add the drawable? Thew name of the drawable I have is called `rounded_mailbox` – chris oojer May 16 '21 at 20:20
  • please see the updated part of my answer at the bottom. – Dinkar Kumar May 16 '21 at 20:36