rockhammer
rockhammer

Reputation: 967

Android: how to align listView to bottom of custom actionBar?

I am following instructions here to make the actionBar "float" on top of an activity's contents so that my background image extends from top edge of screen instead of under the actionBar. However, now I need to align my listView to the bottom of the actionBar because match_parent now aligns the listView to the top edge.

enter image description here

Is there a way to achieve that in xml only? What would the id of the actionBar be so I can set layout_below?

I'm sure I can get/estimate the height of the actionBar in code to set the top margin of the listView accordingly but I'm hoping for a more elegant solution. Thanks

The listView activity xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_users"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srcCompat="@drawable/img_people_512"
        android:id="@+id/usersImageView"
        android:alpha="0.55"
        android:contentDescription="@string/backgroundImage"
        android:scaleType="centerCrop" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/userListView"
         />
</RelativeLayout>

the custom actionBar layout xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mainActionBar"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/mainActionBarSpacer"
        android:text="@string/mainActionBarSpacer"
        android:textColor="@color/colourTransparent"
        android:textSize="35sp"
        android:fontFamily="cursive"
        android:onClick="onClick" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toRightOf="@+id/mainActionBarSpacer"
        android:layout_toEndOf="@+id/mainActionBarSpacer"
        android:gravity="center"
        android:id="@+id/mainActionBarTitle"
        android:text="@string/app_name"
        android:textColor="@color/colourActionBarText"
        android:textSize="35sp"
        android:fontFamily="cursive"
        android:onClick="onClick" />

</RelativeLayout>

Upvotes: 0

Views: 336

Answers (1)

Haythem BEN AICHA
Haythem BEN AICHA

Reputation: 603

just add layout_marginTop to your list view

android:layout_marginTop="?actionBarSize"

Upvotes: 2

Related Questions