-1

I am having a Relative layout that contains text views that list the days of a week along with the timings for that particular day. What I want is to change the order of the text views programmatically in a particular case. The idea is that the names of the days will appear in order such that it will display the days starting from the today's current day. For example, Suppose today is Sunday and the list will start from Sunday following the other days, similarly, when it is Monday, the list will start from Monday.

The layout with the text view is like,

  <RelativeLayout
        android:id="@+id/time_expandable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/time_area"
        android:animateLayoutChanges="true"
        android:clickable="true">

        <TextView
            android:id="@+id/company_day1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="0dp"
            android:layout_marginTop="15dp"
            android:text="Monday"
            android:textColor="#555555"
            android:textSize="15sp"
            android:textStyle="bold"
            android:clickable="false"/>

        <TextView
            android:id="@+id/company_time1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="37dp"
            android:layout_marginTop="15dp"
            android:text=""
            android:textColor="#555555"
            android:layout_toRightOf="@+id/company_day1"
            android:textSize="15sp"
            android:textStyle="bold"
            android:clickable="false"/>

        <TextView
            android:id="@+id/company_day2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="0dp"
            android:layout_marginTop="15dp"
            android:text="Tuesday"
            android:layout_below="@+id/company_day1"
            android:textColor="#555555"
            android:textSize="15sp"
            android:textStyle="bold"
            android:clickable="false"/>

        <TextView
            android:id="@+id/company_time2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="35dp"
            android:layout_marginTop="15dp"
            android:text=""
            android:textColor="#555555"
            android:layout_below="@+id/company_day1"
            android:layout_toRightOf="@+id/company_day2"
            android:textSize="15sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/company_day3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="0dp"
            android:layout_marginTop="15dp"
            android:text="Wednesday"
            android:layout_below="@+id/company_day2"
            android:textColor="#555555"
            android:textSize="15sp"
            android:textStyle="bold"
            android:clickable="false"/>

        <TextView
            android:id="@+id/company_time3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="15dp"
            android:text=""
            android:textColor="#555555"
            android:layout_toRightOf="@+id/company_day3"
            android:layout_below="@+id/company_day2"
            android:textSize="15sp"
            android:textStyle="bold"
            android:clickable="false"/>

        <TextView
            android:id="@+id/company_day4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="0dp"
            android:layout_marginTop="15dp"
            android:text="Thursday"
            android:layout_below="@+id/company_day3"
            android:textColor="#555555"
            android:textSize="15sp"
            android:textStyle="bold"
            android:clickable="false"/>

        <TextView
            android:id="@+id/company_time4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="29dp"
            android:layout_marginTop="15dp"
            android:text=""
            android:textColor="#555555"
            android:layout_toRightOf="@+id/company_day4"
            android:layout_below="@+id/company_day3"
            android:textSize="15sp"
            android:textStyle="bold"
            android:clickable="false"/>

        <TextView
            android:id="@+id/company_day5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="0dp"
            android:layout_marginTop="15dp"
            android:text="Friday"
            android:layout_below="@+id/company_day4"
            android:textColor="#555555"
            android:textSize="15sp"
            android:textStyle="bold"
            android:clickable="false"/>

        <TextView
            android:id="@+id/company_time5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="51dp"
            android:layout_marginTop="15dp"
            android:text=""
            android:textColor="#555555"
            android:layout_toRightOf="@+id/company_day5"
            android:layout_below="@+id/company_day4"
            android:textSize="15sp"
            android:textStyle="bold"
            android:clickable="false"/>

        <TextView
            android:id="@+id/company_day6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="0dp"
            android:layout_marginTop="15dp"
            android:text="Saturday"
            android:layout_below="@+id/company_day5"
            android:textColor="#555555"
            android:textSize="15sp"
            android:textStyle="bold"
            android:clickable="false"/>

        <TextView
            android:id="@+id/company_time6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="32dp"
            android:layout_marginTop="15dp"
            android:text=""
            android:layout_toRightOf="@+id/company_day6"
            android:layout_below="@+id/company_day5"
            android:textColor="#555555"
            android:textSize="15sp"
            android:textStyle="bold"
            android:clickable="false"/>

        <TextView
            android:id="@+id/company_day7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="0dp"
            android:layout_marginTop="15dp"
            android:text="Sunday"
            android:layout_below="@+id/company_day6"
            android:textColor="#555555"
            android:textSize="15sp"
            android:textStyle="bold"
            android:clickable="false"/>

        <TextView
            android:id="@+id/company_time7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="42dp"
            android:layout_marginTop="15dp"
            android:text=""
            android:layout_toRightOf="@+id/company_day7"
            android:layout_below="@+id/company_day6"
            android:textColor="#555555"
            android:textSize="15sp"
            android:textStyle="bold"
            android:clickable="false"/>
    </RelativeLayout>

I have no idea how can I impelent this. I have looked through some links like: How do I change the order of the textviews and buttons in a linerLayout or RelativeLayout?

change order of views in linear layout android

However, I am not sure how to go with this. Please can anyone help?The image shows the list of days starting from Sunday since today is sunday. Similarly I need the list to change with respect to the current day

devgeek
  • 418
  • 3
  • 13

2 Answers2

0

You don't need to. Instead of reordering the views, remap what info is put in each view. For example, if today is Sunday, do

((TextView)findViewById(R.id.company_day1)).setText("Sunday");     
((TextView)findViewById(R.id.company_day2)).setText("Monday");

etc. Moving the views would be unnecessarily hard. Remapping is fairly easy.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

Instead of hard coding the days in the layout file, I would leave it blank. Initialize the TextViews in a two-dimensional array. Enter the values of your day and time in another two-dimensional array. Then run a nested loop to populate the TextViews. This will make your code readable and flexible.

TextView[][] tvs;
String[][] values;

for(int i = 0 ; i < tvs.length ; i++) {
    for(int j = 0 ; j < tvs[i].length ; j++) {
        tvs[i][j].setText(values[i][j]);
    }
}
EdmDroid
  • 1,350
  • 1
  • 11
  • 25