I have a card declared in the file cardslib_item_card_view
:
<it.gmariotti.cardslib.library.view.CardViewNative
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardCornerRadius="4dp"
style="@style/native_recyclerview_card.base"
android:id="@+id/carddemo"
android:layout_width="match_parent" android:layout_height="wrap_content">
and set as content view within onCreate()
method:
public class CardMariotti extends ActionBarActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cardslib_item_card_view);
//Create a Card
Card card = new Card(this);
CardViewNative cardView = (CardViewNative) this.findViewById(R.id.carddemo);
cardView.setCard(card);
card.setOnClickListener(new Card.OnCardClickListener() {
@Override
public void onClick(Card card, View view) {
Toast.makeText(CardMariotti.this, "Clickable card", Toast.LENGTH_LONG).show();
}
});
}
Now, I'd like to customize it with my own layout, containing a narrow header and some information, as follows:
<RelativeLayout android:id="@+id/cardlayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="?android:selectableItemBackground"
android:clickable="true">
<!-- layout containing 3 TextView -->
</RelativeLayout>
What is the canonic procedure for such a process? I've tried a good deal of adjustments, i.e.:
- creating a second xml file called
cardslib_item_layout.xml
and referencing it with theCard
's constructor this way:Card card = new Card(this, R.layout.cardslib_item_layout);
and then setting thesetContentView(R.layout.cardslib_item_card_view)
- Appending the layout inside the card and then setting the
setContentView(R.layout.cardslib_item_card_view)
.
This way; cardslib_item_card_view
:
<it.gmariotti.cardslib.library.view.CardViewNative
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardCornerRadius="4dp"
android:id="@+id/carddemo"
android:layout_width="match_parent" android:layout_height="wrap_content">
<RelativeLayout>
<!-- my layout containing a header and some TextViews -->
</RelativeLayout>
</it.gmariotti.cardslib.library.view.CardViewNative>
In both tests I experience the following issues:
- The overall result is completely distorced
- most importantly, the RelativeLayout is placed ON TOP of the card, making any operation on the card impossible (for example, setting the
Card.OnCardClickListener
on the card itself won't work since the user will be clicking the RelativeLayout and not the card itself)
attempt 1:
attempt 2:
What is the canonic procedure?
EDIT2: ANSWER
The contribution given by @Msk worked fine for me, although I discovered later that with some minor changes it is also possible to obtain the same results by using the original cardslib's
Card
class, without resorting to the creation of a new classDeviceCard
extending theCard
class.I was able to adjust my layout (header and the rest of the card's layout overlapping with each other, as shown in the screenshots) with just some minor and trivial changes in the
cardslib_item_layout.xml
file (which I had overlooked before); at the same time I was able to eliminate the phantom padding that is automatically attached to every card, by applying Mariotti's answer to this question.