Проблемы с изменением цвета кнопки в viewPager2 в Android
Элемент, который выполняется при запуске activity
setContentView(R.layout.activity_test); //запускаем фронтенд
setTitle(group);
ViewPager2 pager=(ViewPager2)findViewById(R.id.pager);
FragmentStateAdapter pageAdapter = new MyAdapter(this);
pager.setAdapter(pageAdapter);
LayoutInflater factory = this.getLayoutInflater();
pager_view = factory.inflate(R.layout.fragment_page, null); //Получаем элементы во fragment_page
switch (commonStuff.getNowCalendar()
.get(Calendar.DAY_OF_WEEK)){ //запускаем расписание на текущий день
case Calendar.MONDAY:
loadDay(pager_view.findViewById(R.id.mondayButton));
break;
case Calendar.TUESDAY:
loadDay(pager_view.findViewById(R.id.tuesdayButton));
break;
case Calendar.WEDNESDAY:
loadDay(pager_view.findViewById(R.id.wednesdayButton));
break;
case Calendar.THURSDAY:
loadDay(pager_view.findViewById(R.id.thursdayButton));
break;
case Calendar.FRIDAY:
loadDay(pager_view.findViewById(R.id.fridayButton));
break;
case Calendar.SATURDAY:
loadDay(pager_view.findViewById(R.id.saturdayButton));
break;
case Calendar.SUNDAY:
loadDay(pager_view.findViewById(R.id.sundayButton));
break;
}
Сам метод, который должен менять цвет:
public void loadDay(View view){
LinearLayout parent = pager_view.findViewById(R.id.DAYS);
for(int i=0; i<parent.getChildCount();i++){
Button child = (Button) parent.getChildAt(i);
//Время костылей
ViewGroup.LayoutParams lp = child.getLayoutParams();
lp.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 46, getResources().getDisplayMetrics()); //установка ширины и высоты в dp
lp.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 38, getResources().getDisplayMetrics());
child.setTextSize((float) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 15, getResources().getDisplayMetrics()));
child.setBackgroundColor(Color.LTGRAY);
child.setLayoutParams(lp);
}
view.setBackgroundColor(Color.DKGRAY);
Сам layout с кнопками
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:id="@+id/DAYS"
android:layout_marginTop="5dp"
app:layout_constraintTop_toBottomOf="@+id/header_group"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
>
<Button
android:id="@+id/mondayButton"
android:onClick="loadDay"
android:text="@string/monday"
android:textSize="15sp"
style="@style/DaysButtonStyle"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/tuesdayButton"
app:layout_constraintTop_toBottomOf="@+id/header_group"
/>
<Button
android:id="@+id/tuesdayButton"
android:text="@string/tuesday"
android:textSize="15sp"
android:onClick="loadDay"
style="@style/DaysButtonStyle"
app:layout_constraintLeft_toRightOf="@+id/mondayButton"
app:layout_constraintRight_toLeftOf="@+id/wednesdayButton"
app:layout_constraintTop_toBottomOf="@+id/header_group"/>
<Button
android:id="@+id/wednesdayButton"
android:text="@string/wednesday"
android:onClick="loadDay"
android:textSize="15sp"
style="@style/DaysButtonStyle"
app:layout_constraintLeft_toRightOf="@+id/tuesdayButton"
app:layout_constraintRight_toLeftOf="@+id/thursdayButton"
app:layout_constraintTop_toBottomOf="@+id/header_group"
/>
<Button
android:id="@+id/thursdayButton"
android:text="@string/thursday"
android:onClick="loadDay"
android:textSize="15sp"
style="@style/DaysButtonStyle"
app:layout_constraintLeft_toRightOf="@+id/wednesdayButton"
app:layout_constraintRight_toLeftOf="@+id/fridayButton"
app:layout_constraintTop_toBottomOf="@+id/header_group"
/>
<Button
android:id="@+id/fridayButton"
android:text="@string/friday"
android:onClick="loadDay"
android:textSize="15sp"
style="@style/DaysButtonStyle"
app:layout_constraintLeft_toRightOf="@+id/thursdayButton"
app:layout_constraintRight_toLeftOf="@+id/saturdayButton"
app:layout_constraintTop_toBottomOf="@+id/header_group"/>
<Button
android:id="@+id/saturdayButton"
style="@style/DaysButtonStyle"
android:text="@string/sunday"
android:onClick="loadDay"
android:textSize="15sp"
app:layout_constraintLeft_toRightOf="@+id/fridayButton"
app:layout_constraintRight_toLeftOf="@+id/sundayButton"
app:layout_constraintTop_toBottomOf="@+id/header_group"
/>
<Button android:id="@+id/sundayButton"
android:layout_width="46dp"
android:layout_height="38dp"
android:text="ВС"
android:onClick="loadDay"
android:textSize="15sp"
app:layout_constraintLeft_toRightOf="@+id/saturdayButton"
app:layout_constraintTop_toBottomOf="@+id/headergroup"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
После запуска программы:
Цвет не поменялся.
Как решить эту проблему?
При нажатии непосредственно на кнопку - все работает.
Необходимо, чтобы при запуске программы без нажатия на саму кнопку менялся цвет.