2

I have a listview in my activity and Action bar for activity set to transparent and UP navigation. Results are showing up properly but, the first item is showing up below actionBAR. Like the image below:

Check Image Below

Below is the code I am using to make the bar transparent:

    getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    ActionBar actionBar = getActionBar();
    actionBar.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    actionBar.setStackedBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setDisplayShowHomeEnabled(false);
    setContentView(R.layout.invite_friends);

invite_friends.XML for listView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bluebackground"
android:orientation="vertical" >

<ListView
    android:id="@+id/person_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="#b5b5b5"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector" />

 </LinearLayout>

Surprising that the listview is thinking it is full screen? How to solve this issue?

Thanks!

Community
  • 1
  • 1
TheDevMan
  • 5,914
  • 12
  • 74
  • 144

1 Answers1

5

The problem is this:

getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

You are telling the ActionBar to be in overlay mode, that means it will overlay the content instead of being at a fixed position above it. This is useful if you want to dynamically hide and show the ActionBar in your app, but it is not required to make the ActionBar transparent. Just remove it and it should work as you expect.

If you however want or need the ActionBar in overlay mode than you can just apply a padding to the content in your Activity which is equal to the ActionBar height. Like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?android:attr/actionBarSize">
    ...
</RelativeLayout>

If you are using the support library you have to use ?attr/actionBarSize insted of ?android:attr/actionBarSize like this:

<!-- Support library compatibility -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">
    ...
</RelativeLayout>
Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
  • Yeah just spotted this, seems like the culprit – gavlaaaaaaaa Aug 01 '14 at 10:36
  • If I remove the ACTION_BAR_OVERLAY.. My actionbar gets white color? I want it to be transparent? – TheDevMan Aug 01 '14 at 10:44
  • That flag has nothing to do with the color. You must be doing something else wrong. – Xaver Kapeller Aug 01 '14 at 10:44
  • I added android:paddingTop="?android:attr/actionBarSize" in my xml it is showing up properly now... THanks! – TheDevMan Aug 01 '14 at 10:47
  • With this approach, in the case that you use the quick return pattern with the action bar (like the Google IO 2014 app) you will see an empty region in the ListView. The only way I have found for now is to use 2 different cell types in the ListView, being the cell in the first position an empty cell with the same height of the action bar. – Diego Palomar Oct 14 '14 at 22:52