34

I am programmatically creating view in Android. Parent layout is RelativLlayout and I'm adding two views in it. First is ListView and second is custom view. I want my second custom view to come on top of the list, but somehow it is getting hidden behind the ListView. How do I make sure that my custom view is on top.

Onik
  • 19,396
  • 14
  • 68
  • 91
Chimu
  • 758
  • 1
  • 6
  • 16

12 Answers12

34

Views get placed in the order you add them to their parent views. The view added last will be on top. You might also want to try View.bringToFront() http://developer.android.com/reference/android/view/View.html#bringToFront()

Megan Scott
  • 459
  • 4
  • 6
13

Try to use: View.bringToFront();

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
onur taskin
  • 929
  • 1
  • 14
  • 33
8

Solution 1 - View#bringToFront() View.bringToFront() should work if used properly. Maybe you forgot this (source: http://developer.android.com/reference/android/view/View.html#bringToFront()):

Prior to KITKAT this method should be followed by calls to requestLayout() and invalidate()

Solution 2 - reattach view

If View.bringToFront() will not work try to remove and add the view again. The advantage is that it has the same code on pre-KITKAT versions too.

I am doing it this way in my code:

ViewGroup parentView = (ViewGroup) (listView.getParent());
parentView.removeView(addFab);
parentView.addView(addFab);
Amio.io
  • 20,677
  • 15
  • 82
  • 117
5

Use FrameLayout, frame layout shows last added view on top..

MohsinSyd
  • 175
  • 2
  • 13
5

Try this :

   <?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" >

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

   <YourCustomView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_centerInParent="true" />

</RelativeLayout>
nikhil.thakkar
  • 1,093
  • 7
  • 12
4

Please try adding android:elevation attribute to your view in the XML. It will always place your custom view over the ListView.

Akanshi Srivastava
  • 1,160
  • 13
  • 24
2

Again: Views get placed to the layout in the order you add it. Question is: is it realy hidden or does it not render?. Check LogCat and Console for errors. If you only add this single Custom View to your layout, does it get rendered without any problems? If so, ensure you realy add your custom to the same parent view (group), as you add your ListView. If you don't get any further, provide de respective code sample.

Megan Scott
  • 459
  • 4
  • 6
1

Give an elevation to the view which you want to bring up.

android:elevation="2dp"

Eldhopj
  • 2,703
  • 3
  • 20
  • 39
0

This works for me :

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="220dp"
android:gravity="bottom"
android:orientation="vertical"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

<ImageView
    android:id="@+id/details_wrapper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingBottom="16dip"
    android:scaleType="centerCrop"
    android:src="@drawable/temp_nav_image">
</ImageView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="Kapil Rajput"
        android:textStyle="bold"
        android:textColor="@android:color/black"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        android:text="kapil@gnxtsystems.com"/>
 </LinearLayout>
 </RelativeLayout>
Kapil Rajput
  • 11,429
  • 9
  • 50
  • 65
0

I used relative layout and the order of the views somehow determines which one is on top.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">
    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:splitTrack="false"
        android:layout_alignParentTop="true"
    />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"/>

In this layout, the textview will be on top of the seekbar

code
  • 1
0

Make parent view as relative layout and add your both view in xml.It wont work for Linear Layout. And then add FirstView.bringToFront() programtically.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bg"
android:orientation="vertical"
android:padding="15dp">


<ImageView
    android:id="@+id/imgIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center"
    android:layout_marginBottom="-30dp"
    android:layout_marginTop="30dp"
    android:src="@mipmap/login_logo" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/imgIcon"
    android:layout_gravity="bottom"

    android:background="#B3FFFFFF"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

<View 1/>
<View 2/>
</LinearLayout>

Java file
imgIcon.brigtToFront();
0

what you should do is using android:layout_below or android:layout_above programmatically. so you should do this:

RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); 
params.addRule(RelativeLayout.BELOW, R.id.below_id);
viewToLayout.setLayoutParams(params);

and i suggest you take a look at this answer

hope this helps

Pouya Danesh
  • 1,557
  • 2
  • 19
  • 36