Search code examples
androiduser-interfacelayout

How to center view on border of another view?


I have looked around and haven't found an answer. I am building one of my apps for Android and I can't find a way to achieve this: iOS App Screenshot

I did it easily for iOS but I'm having trouble with Android. Any help would be appreciated. Thanks!

This is what I have at the moment on my Android app: Android App Screenshot

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="8dp"
        android:layout_weight="1"
        android:background="@color/chronoBgColor"
        android:orientation="horizontal"
        tools:layout_editor_absoluteX="41dp"
        tools:layout_editor_absoluteY="247dp">

        <TextView
            android:id="@+id/shockTimerTextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:fontFamily="sans-serif-heavy"
            android:gravity="center"
            android:text="00:00:00"
            android:textColor="@color/chronoFontColor"
            android:textSize="36sp"
            android:typeface="monospace" />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <Button
                android:id="@+id/shockTimerButton"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@color/shockColor"
                android:fontFamily="sans-serif-light"
                android:text="SHOCK"
                android:textColor="@android:color/white"
                android:textSize="24sp" />

            <TextView
                android:id="@+id/activityCountTextView"
                android:layout_width="16dp"
                android:layout_height="25dp"
                android:layout_gravity="center_vertical|right"
                android:background="@android:color/black"
                android:gravity="center"
                android:text="0"
                android:includeFontPadding="false"
                android:textColor="@android:color/white"
                android:textSize="24sp" />
        </FrameLayout>

    </LinearLayout>

Solution

  • You could try the following:

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="16dp"
            android:weightSum="2">
    
            <TextView
                android:id="@+id/shockTimerTextView"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:fontFamily="sans-serif-heavy"
                tools:text="00:00:00"
                android:textColor="@android:color/darker_gray"
                android:textSize="36sp"
                android:typeface="monospace" />
            
            <Button
                android:id="@+id/shockTimerButton"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@color/colorAccent"
                android:fontFamily="sans-serif-light"
                tools:text="SHOCK"
                android:layout_gravity="center"
                android:textColor="@android:color/white" />
    
        </LinearLayout>
    
        <TextView
            android:id="@+id/activityCountTextView"
            android:layout_width="24dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|end"
            android:background="@android:color/black"
            android:gravity="center"
            android:layout_marginEnd="4dp"
            android:textColor="@android:color/white"
            android:textSize="24sp"
            tools:text="0" />
        
    </FrameLayout>
    

    this produces:

    enter image description here