i have made a simple timetable with GridLayout and it looks like this
Now the idea is to insert required subject into specific row and column. In order to achieve that i have created a class that extends CardView in which i need to insert TextView. code: TimeTable.xml
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="8"
android:columnOrderPreserved="true"
android:rowCount="6"
tools:context="com.digiart.schoolapp.fragments.TimetableFragment">
<android.support.v7.widget.CardView
android:id="@+id/timetable_card_space"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_row="0"
app:cardElevation="2dp"
app:contentPadding="5dp">
<TextView
android:id="@+id/timetable_dummy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="time"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:visibility="invisible" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/timetable_day1_card"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_row="0"
app:cardElevation="2dp"
app:contentPadding="5dp">
<TextView
android:id="@+id/timetable_day1_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Monday"
android:textAppearance="@style/TextAppearance.AppCompat.Body2" />
</android.support.v7.widget.CardView>
.....
........
<android.support.v7.widget.CardView
android:id="@+id/timetable_time1_card"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_row="1"
app:cardElevation="2dp"
app:contentPadding="5dp">
<TextView
android:id="@+id/timetable_time1_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="09.00"
android:textAppearance="@style/TextAppearance.AppCompat.Body2" />
</android.support.v7.widget.CardView>
.....
......
</GridLayout>
TimetableSubject:
public class TimetableSubject extends CardView {
TextView subjectText;
public TimetableSubject(Context context,int column,int row,int columnSpan,String subjectName) {
super(context);
GridLayout.LayoutParams subjectParam =new GridLayout.LayoutParams();
subjectParam.columnSpec = GridLayout.spec(column,columnSpan);
subjectParam.rowSpec = GridLayout.spec(row);
subjectText = new TextView(context);
CardView.LayoutParams textParams = new CardView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
subjectText.setText(subjectName);
subjectText.setLayoutParams(textParams);
setLayoutParams(subjectParam);
}
}
Now i get what i need to do, i need to pass row and column to the custom view, and set those as layout params. the issue i think is with the Layout parameters code, i must be messing something up there. Could anyone explain how to set layout params for this situation properly? Thanks.
There was an issue with GridLayout weight parameter in Spec. Managed to solve the problem by inserting weight as a float parameter in Spec Code:
public class TimetableSubjectView extends CardView {
TextView subjectText;
public TimetableSubjectView(Context context, int column, int row, float columnWeight, String subjectName, int color) {
super(context);
GridLayout.Spec rowI = GridLayout.spec(row,1,columnWeight);
GridLayout.Spec colI = GridLayout.spec(column,1,columnWeight);
GridLayout.LayoutParams subjectParam =new GridLayout.LayoutParams(rowI,colI);
subjectParam.setMargins(5,5,5,5);
setBackgroundColor(color);
subjectText = new TextView(context);
CardView.LayoutParams textParams = new CardView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
subjectText.setText(subjectName);
subjectText.setGravity(Gravity.CENTER);
subjectText.setLayoutParams(textParams);
this.addView(subjectText);
setLayoutParams(subjectParam);
}
}