2

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?

enter image description here

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);
gsamerica
  • 153
  • 1
  • 3
  • 18

2 Answers2

2

you are overriding the relativeParams with the linearParams. set margins to relativeParms variable iteself and then setLayoutParams to newActivity like below:

    relativeParams.addRule(RelativeLayout.BELOW, timeSlots[startTimeHour + 1]);
    relativeParams.setMargins(0, startTimeMin-3, 0, 0);
    newActivity.setLayoutParams(relativeParams);
    // Add to the screen
    RelativeLayout schedule = (RelativeLayout) findViewById(R.id.scheduleView);
    schedule.addView(newActivity);
sravs
  • 330
  • 2
  • 14
0

This can solve your problem

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(10,10,10,10);
TextView textView = (TextView) findViewById(R.id.textView);
textView.setLayoutParams(params);
Vinod Pattanshetti
  • 2,465
  • 3
  • 22
  • 36
  • Unfortunately I've seen this answer on other questions here on SO that are similar to this one, but it does not work for me. Also, should it instead be `ViewGroup.LayoutParams`? instead of just `LayoutParams`? And what about `relativeParams.addRule(RelativeLayout.BELOW, timeSlots[startTimeHour + 1]);` `newActivity.setLayoutParams(relativeParams);` from my code? While this code compiles, it doesn't set the TextView to be where I want it to be. What should I be doing instead? – gsamerica Apr 04 '17 at 04:18