1

I have a RelativeLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    android:paddingBottom="0dp"
    tools:context="com.pinder.moffel.pinder.DeleteActivity"
    android:id="@+id/deleteBackground">

    <com.andtinder.view.CardContainer
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tinderview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="0"/>


</RelativeLayout>

I'm using the library AndTinder: https://github.com/kikoso/Swipeable-Cards

I already know how to add a new card, but the problem is, because a new layout is being added, the new card get shown on top of the other views.

I'd like to add 3 cards when the activity loads and then add a new card behind all other cards when a card is swiped away.

How do I inflate a new view behind all other views in a RelativeLayout?

Some additional code:

JAVA code

SimpleCardStackAdapter adapter = null;
CardContainer mCardContainer = (CardContainer) findViewById(R.id.tinderview);
adapter = new SimpleCardStackAdapter(this);
CardModel newCard = new CardModel(null, null, image); //image is retrieved before
adapter.add(newCard); //the new card will now be on top of the others, i want it to be behind them

mCardContainer.setAdapter(adapter);

XML view that is being added

<RelativeLayout
    android:id="@+id/global_container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="300dp"
        android:layout_alignWithParentIfMissing="true"
        android:layout_centerHorizontal="true"
        android:scaleType="fitCenter"
        tools:src="@drawable/picture1"/>

    <View
        android:id="@+id/divider_bottom"
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:layout_below="@id/image"
        android:background="@color/card_outline" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="10dp"
        android:layout_alignBottom="@+id/layoutButton"
        android:layout_below="@+id/divider_bottom"
        android:background="@color/card_bg"
        />

    <!-- An invisible view aligned to the center of the parent. Allows other
        views to be arranged on either side -->
    <LinearLayout
        android:id="@+id/layoutButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/divider_bottom"
        android:gravity="center"
        >

        <ImageButton
            android:id="@+id/image_1"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="@drawable/denied"
            android:padding="10dp"
            android:layout_marginRight="80dp"/>

        <ImageButton
            android:id="@+id/image_2"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="@drawable/heart"
            android:padding="10dp"
            android:layout_alignParentRight="true" />

    </LinearLayout>



</RelativeLayout>

In another question (How to inflate a view behind another) the answer states that it is impossible to add views behind others, but I think there has to be a way to accomplish this.

Community
  • 1
  • 1
moffeltje
  • 4,521
  • 4
  • 33
  • 57

3 Answers3

6

When adding a new view it is automatically putting it on top.

You can use the addView(your_view, 0) this will add the view on the index 0 of the layout

NaviRamyle
  • 3,967
  • 1
  • 31
  • 49
5

You can use ViewAnimator

whose documentation is available on http://developer.android.com/reference/android/widget/ViewAnimator.html

In this you need to create two child layouts inside View Animator. you can define your own child layouts. and can use next and previous method to handle events. I have used it earlier in one of my project.

Mukesh Garg
  • 711
  • 1
  • 6
  • 20
  • I will post my code which might be helpful for you. you can also use viewflipper. you can check sample app https://play.google.com/store/apps/details?id=com.pmstudy.flashcard&hl=en where i have used this to display cards – Mukesh Garg Sep 23 '15 at 09:55
2

On each of your Views, you have view.bringToFront() which you can use to put that view in front of all others. If nothing else works, you could use this on all of your other views, after you have inflated yours.

Joakim
  • 3,224
  • 3
  • 29
  • 53
  • How do I pick the other views, since they have the same id? – moffeltje Sep 23 '15 at 08:55
  • You can iterate over the children of your RelativeLayout, but I'm not sure it's a nice solution.... You need to use relativeLayout.getChildCount() and relativeLayout.childAtIndex(index); – Joakim Sep 23 '15 at 09:21