skip to Main Content

I was wondering if there is a chance to align the play button as from screenshot at the center of the screen. Unfortunately, I have only succeeded in doing such with the white space that I wish to remove. The whole code sample has been attached to the question upon request. This now illustrates the area, where two blocks must be separated with a constraint layout with a required button in the center

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ROOT"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:background="#f5907c"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:orientation="vertical"
        android:clickable="true"
        android:focusable="true">

        <View
            android:id="@+id/dummyViewTop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

        <TextView
            android:id="@+id/top_clock"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="false"
            android:gravity="center"
            android:rotation="180"
            android:text="@string/DEFAULT_CLOCK_VALUE"
            android:textSize="90sp"
            android:textColor="@color/white"
            android:textStyle="bold"
            />

    </RelativeLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <ImageButton
            android:id="@+id/CONTROL_BUTTON_RESET"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@android:color/transparent"
            android:scaleType="center"
            android:src="@drawable/ic_baseline_reset_24"
            android:contentDescription="@string/reset_button_desc"
            />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <RelativeLayout
        android:background="#2e3a52"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:orientation="vertical">

        <View
            android:id="@+id/dummyViewBottom"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

        <TextView
            android:id="@+id/bottom_clock"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/DEFAULT_CLOCK_VALUE"
            android:textSize="90sp"
            android:textColor="@color/white"
            android:textStyle="bold"
            />

    </RelativeLayout>

</LinearLayout>

enter image description here

enter image description here

3

Answers


  1. Try this

    <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >
            <ImageView
                android:id="@+id/CONTROL_BUTTON_RESET"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_baseline_reset_24"
                android:scaleType="center"
    
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    this should make your button center in the middle of the screen, provided your ConstratintLayout occupies the whole screen.

    Also, don’t use ImageButton, use an ImageView and apply a click listener to it.

    Login or Signup to reply.
  2. I will solve this in this manner by making constraint layout as the root layout. It will flatten our view hierarchy and reduces the code complexity

    <?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"
      android:id="@+id/ROOT"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    
        
        <TextView
            android:background="#f5907c"
            android:id="@+id/top_clock"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:clickable="false"
            android:gravity="center"
            android:rotation="180"
            android:text="10:00"
            android:textSize="90sp"
            android:textColor="@color/white"
            android:textStyle="bold"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/bottom_clock"/>
    
        <TextView
            android:id="@+id/bottom_clock"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:gravity="center"
            android:text="10:00"
            android:background="#2e3a52"
            android:textSize="90sp"
            android:textColor="@color/white"
            android:textStyle="bold"
            app:layout_constraintTop_toBottomOf="@id/top_clock"
            app:layout_constraintBottom_toBottomOf="parent"/>
        <ImageView
           android:id="@+id/btn_setting"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/add_btn"
           app:layout_constraintTop_toTopOf="@id/bottom_clock"
           app:layout_constraintBottom_toTopOf="@id/bottom_clock"
           app:layout_constraintStart_toStartOf="parent"
           app:layout_constraintEnd_toStartOf="@id/btn_refresh"/>
        <ImageView
           android:id="@+id/btn_refresh"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/add_btn"
           app:layout_constraintTop_toTopOf="@id/bottom_clock"
           app:layout_constraintBottom_toTopOf="@id/bottom_clock"
           app:layout_constraintEnd_toEndOf="parent"
           app:layout_constraintStart_toEndOf="@id/btn_setting"/>
        
    
     </androidx.constraintlayout.widget.ConstraintLayout>
    

    enter image description here

    I suggest you to explore Constraint layout some time

    Login or Signup to reply.
  3. you have to give a constraint properly to your button like this:

    <androidx.constraintlayout.widget.ConstraintLayout
         android:layout_width="match_parent"
         android:layout_height="mate_parent">
    
         <TextView
             android:id = "@+id/textView"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             app:layout_constraintTop_toTopOf="parent"
             app:layout_constraintBottom_toBottomOf="parent"
             app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintEnd_toEndOf="parent"/>
        
    </androidx.constraintlayout.widget.ConstraintLayout>
          
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search