Reputation: 130
I am trying to simply place a TextView
inside a CardView
, but to no avail. All I need to do is make two numbers appear inside the two CardViews
. At the moment, with code below, the two CardViews
appear, but do not show any text / content inside them.
I have tried many different combinations but none seem to be working! Placing the TextView
inside a LinearLayout
does not work, nor does making it a direct child of the CardView
.
Here's my current code:
calendar_view.xml
<?xml version="1.0" encoding="utf-8"?>
<hoo.box.facecalendar.SquareCardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardCornerRadius="4dp"
card_view:cardUseCompatPadding="true"
android:id="@+id/calendar_view_card"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/calendar_view_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:singleLine="true"
android:textColor="@color/primary_text"/>
</RelativeLayout>
</hoo.box.facecalendar.SquareCardView>
SquareCardView.java
public class SquareCardView extends CardView {
public SquareCardView(Context context) {
super(context);
}
public SquareCardView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareCardView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
setMeasuredDimension(width, width);
}
}
CalendarAdapter.java
public class CalendarAdapter extends RecyclerView.Adapter<CalendarAdapter.CalendarViewHolder> {
private LayoutInflater inflater;
private Date curDate;
private List<Date> dateList;
public CalendarAdapter(Context context) {
inflater = LayoutInflater.from(context);
curDate = new Date();
dateList = new ArrayList<Date>();
}
@Override
public CalendarViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = inflater.inflate(R.layout.calendar_view, viewGroup, false);
CalendarViewHolder viewHolder = new CalendarViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(CalendarViewHolder calendarViewHolder, int i) {
if (dateList.size() <= i) {
Date tempDate = new Date(curDate);
tempDate.addDays(i + 1);
dateList.add(i, tempDate);
}
Date iDate = dateList.get(i);
calendarViewHolder.TEXTVIEW.setText(Integer.toString(iDate.day));
}
@Override
public int getItemCount() { return dateList.size() + 1; }
class CalendarViewHolder extends RecyclerView.ViewHolder {
public final TextView TEXTVIEW;
public final SquareCardView CARDVIEW;
public CalendarViewHolder(View itemView) {
super(itemView);
TEXTVIEW = (TextView) itemView.findViewById(R.id.calendar_view_day);
CARDVIEW = (SquareCardView) itemView.findViewById(R.id.calendar_view_card);
}
}
}
Just a prior thanks to anyone who can help me! This has been eating away at me for days!
Upvotes: 2
Views: 3601
Reputation: 1043
There is problem with your support library which you are using for cardview. The compiled sdk version and support lib version should be same.
for example: In build.gradle
if compileSdkVersion 23 then
in dependencies you should use
compile 'com.android.support:cardview-v7:23.0.0'
Check both the versions in build.gradle.
ps: if you use support lib version 24 studio suggest you the following
"this support library should not use a different version (24) than the compilesdkversion (23)"
best luck !
Upvotes: 0
Reputation: 130
After about 3 hours of playing with it, I found the solution! It was a problem with SquareCardView
's onMeasure()
not resizing the inner layout bounds. Below is what I altered it to:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
Upvotes: 1