I want to show the current day to coming 5 days date and week so I am using dynamic view and adding this view to parent programmatically. Now I want to change the colour of the textviews and background colour of the view in onclick operation. At first, I need to change the colours of the first index.
I am sharing my code can anyone tell me how to change the textcolor of textview when onclick.
maiin.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="8dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="15sp"
android:id="@+id/datenumber"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="15sp"
android:inputType="textCapSentences"
android:capitalize="sentences"
android:layout_marginTop="14dp"
android:layout_gravity="center"
android:id="@+id/weekname" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="15sp"
android:visibility="gone"
android:inputType="textCapSentences"
android:capitalize="sentences"
android:layout_marginTop="14dp"
android:layout_gravity="center"
android:id="@+id/date" />
</LinearLayout>
</LinearLayout>
activity: (calendar dates from the current day to the upcoming 5 days )
public class TableBookActivtiy extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maiin);
SimpleDateFormat sdf = new SimpleDateFormat("EEE dd");
for (int i = 0; i < 6; i++) {
Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.DATE, i);
String day = sdf.format(calendar.getTime());
String[] splited = day.split("\s+");
final View child = getLayoutInflater().inflate(R.layout.activitiy_tabledate, null);
final TextView datenumber = (TextView) child.findViewById(R.id.datenumber);
final TextView weekname = (TextView) child.findViewById(R.id.weekname);
final TextView date = (TextView) child.findViewById(R.id.date);
child.setTag(date.getText().toString());
datenumber.setText(splited[0]);
weekname.setText(splited[1]);
dates_lay.addView(child);
child.setOnClickListener(new OnClickLitener());
}
((View) dates_lay.getChildAt(0)).setBackgroundColor(Color.RED); //for index 0
}
class OnClickLitener implements View.OnClickListener {
@Override
public void onClick(View v) {
// date number week name date// want to change text and background colours
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…