I'm trying to make a Scheduling app, and in doing so have made a Schedule screen with TextViews and Views that represent the time slots that an activity can correspond to.
However I'm having a hard time getting a generated TextView (which will represent the activity on the Schedule) to line up correctly with the View associated with the Activity's start time.
For example in this case I make a TextView with text = "CSI2120"
, and attempt to line it up with line (which is a View) above the "13:00" TextView here. However I am missing something as the advice from these links as I can't get them to work.
Can I set “android:layout_below” at runtime, programmatically?
Android How to change layout_below to TextView programmatically (See Second Way in the Answer)
The TextView is in the default location on the top-right, not where I want it to be. What should be doing instead of what the links advise?
Here is my full method. timeSlots
is an array of R.id.view[####] ints and Schedule
is the Activity the method is in:
public void displayDailyActivities(int[] timeSlots){
String[] todaysActivities = {"CSI2120", "01", "14", "2017", "0300", "0400"};
// Make the associated TextView(s)
for(int i=0; i < todaysActivities.length; i=i+6){
int startTime = Integer.valueOf(todaysActivities[i+4]);
int startTimeHour = startTime / 100;
int startTimeMin = startTime % 100;
// Make the TextView and add it to the Schedule
// Code I got from links
TextView newActivity = new TextView(Schedule.this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// Make the activity, grabbing the corresponding timeslot (View) from the timeSlots array
newActivity.setText(todaysActivities[i]);
// In this case ( timeSlots[startTimeHour + 1] ) returns ( R.id.view0300 )
// which is the View (line) on the Schedule directly above to the "03:00" TextView
relativeParams.addRule(RelativeLayout.BELOW, timeSlots[startTimeHour + 1]);
newActivity.setLayoutParams(relativeParams);
linearParams.setMargins(0, startTimeMin-3, 0, 0);
newActivity.setLayoutParams(linearParams);
// Add to the screen
RelativeLayout schedule = (RelativeLayout) findViewById(R.id.scheduleView);
schedule.addView(newActivity);
// Make sure we're not going out of bounds
if(i + 6 > todaysActivities.length){
i = todaysActivities.length;
}
else{
}
}
}
EDIT: The code that I've found from other similar questions, specifically, that doesn't work for me are the lines:
TextView newActivity = new TextView(Schedule.this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
...
relativeParams.addRule(RelativeLayout.BELOW, timeSlots[startTimeHour + 1]);
newActivity.setLayoutParams(relativeParams);
linearParams.setMargins(0, startTimeMin-3, 0, 0);
newActivity.setLayoutParams(linearParams);