Ads Top

Set TextColor of TextView in LinearLayout in android studio using java

If You have multiple Linear Layouts in a Layout then you can use this code
like the Xml code below

--------------------------------------------------------------------------------------------------------------------------
 <LinearLayout
            android:id="@+id/linear_layour_main_dashboard"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#64B5F6"
            android:orientation="horizontal"
            android:padding="10dp"
            android:weightSum="5">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:orientation="vertical">

               <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>
</LinearLayout>
--------------------------------------------------------------------------------------------------------------------------

Then use the below java code


--------------------------------------------------------------------------------------------------------------------------
private void setLinearLayoutColor() {
        TextView textView;
        for (int i = 0; i < mLinearLayourMainDashboard.getChildCount(); i++) {
            View view = mLinearLayourMainDashboard.getChildAt(i);
            if (view instanceof LinearLayout) {
                LinearLayout linearView = (LinearLayout) view;
                for (int t = 0; t < linearView.getChildCount(); t++) {
                    View textViewView = linearView.getChildAt(t);
                    if (textViewView instanceof TextView) {
                        textView = (TextView) textViewView;
                        textView.setTextColor(getResources().getColor(R.color.colorWhite));
                        //textView.setTextSize(17);
                    }
                }
            } else {
                if (view instanceof TextView) {
                    textView = (TextView) view;
                    textView.setTextColor(getResources().getColor(R.color.colorWhite));
                }
            }

        }
    }

--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------

If you have multiple TextView in single LinearLayout mean you have no other object in Layout except TextView or any other object like the code below
--------------------------------------------------------------------------------------------------------------------------
<LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:orientation="vertical">

               <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
  </LinearLayout>
--------------------------------------------------------------------------------------------------------------------------

Then use the java code below
--------------------------------------------------------------------------------------------------------------------------

private void setLinearLayoutColor() {
        TextView textView;
        for (int i = 0; i < mLinearLayourMainDashboard.getChildCount(); i++) {
            View view = mLinearLayourMainDashboard.getChildAt(i);
                if (view instanceof TextView) {
                    textView = (TextView) view;
                    textView.setTextColor(getResources().getColor(R.color.colorWhite));
                }
        }
    }
--------------------------------------------------------------------------------------------------------------------------


If you want to set this property for other objects like EditText just change that line and you are good to go


 if (view instanceof EditText) {
Powered by Blogger.