Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

android - Changing a textview text color in a dynamic layout

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
mTextView.setTextColor(Color.parseColor("#bdbdbd"));  

Or if you have defined color code in resource's color.xml file than

(From API >= 23)

mTextView.setTextColor(ContextCompat.getColor(context, R.color.<name_of_color>));

(For API < 23)

mTextView.setTextColor(getResources().getColor(R.color.<name_of_color>));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...