Search code examples
androidandroid-layoutandroid-studioandroid-relativelayout

Color issue onFocus/onClick in relative layout


I'm having some issues with my layout style, actually on Android 5.1.1 it works fine and when i press on a RelativeLayout the Text on it change color so it's seems like it's selected and it's more nice to see.

While in Android 7.0 when a RelativeLayout is onClick or onFocus it's get a horrible orange color so i would that it's looks like as in Android 5.1.1... enter image description here

enter image description here

Here is my xml code from this view

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
    tools:context=".MainActivity"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:layout_marginTop="0dp">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:ignore="UselessLeaf,UselessParent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                tools:ignore="InefficientWeight,ObsoleteLayoutParam,UselessLeaf"
                android:baselineAligned="false">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/rellay_cassa"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_weight="0.65"
                    android:background="@color/blueLight"
                    android:clickable="true"
                    android:foreground="?attr/selectableItemBackground"
                    android:focusable="true"
                    tools:ignore="NestedWeights,ObsoleteLayoutParam">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        tools:ignore="UselessParent">

                        <ImageView
                            android:id="@+id/imgCassa"
                            android:layout_width="60dp"
                            android:layout_height="60dp"
                            android:layout_centerHorizontal="true"
                            android:adjustViewBounds="true"
                            android:padding="8dp"
                            android:scaleType="centerInside"
                            android:src="@drawable/ic_cassa"
                            android:tint="@color/colorWhite"
                            tools:ignore="ContentDescription" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/imgCassa"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="5dp"
                            android:text="CASSA"
                            android:textSize="14sp"
                            tools:ignore="HardcodedText" />

                    </RelativeLayout>

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/rellay_tasti"
                    android:layout_weight="0.55"
                    android:clickable="true"
                    android:background="@color/blueDark"
                    android:foreground="?attr/selectableItemBackground"
                    android:focusable="true">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        tools:ignore="UselessParent">

                        <ImageView
                            android:id="@+id/imgTasti"
                            android:layout_width="60dp"
                            android:layout_height="60dp"
                            android:layout_centerHorizontal="true"
                            android:adjustViewBounds="true"
                            android:padding="8dp"
                            android:scaleType="centerInside"
                            android:src="@drawable/ic_tasti"
                            android:tint="@color/colorWhite"
                            tools:ignore="ContentDescription" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/imgTasti"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="5dp"
                            android:text="AGGIUNGI TASTI"
                            android:textSize="14sp"
                            tools:ignore="HardcodedText" />

                    </RelativeLayout>

                </RelativeLayout>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="0dp"
                android:layout_weight="1"
                android:baselineAligned="false"
                tools:ignore="ObsoleteLayoutParam">

                <RelativeLayout
                    android:id="@+id/rellay_articoli"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="0.55"
                    android:background="@color/blueDark"
                    android:clickable="true"
                    android:foreground="?attr/selectableItemBackground"
                    android:focusable="true"
                    tools:ignore="InefficientWeight,NestedWeights">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    tools:ignore="ObsoleteLayoutParam,UselessParent">

                    <ImageView
                        android:id="@+id/imgArticoli"
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:layout_centerHorizontal="true"
                        android:adjustViewBounds="true"
                        android:padding="8dp"
                        android:scaleType="centerInside"
                        android:src="@drawable/ic_articoli"
                        android:tint="@color/colorWhite"
                        tools:ignore="ContentDescription" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/imgArticoli"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="5dp"
                        android:text="AGGIUNGI ARTICOLI"
                        android:textSize="14sp"
                        tools:ignore="HardcodedText" />

                </RelativeLayout>

             </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/rellay_clienti"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@color/blueLight"
                    android:layout_weight="0.55"
                    android:clickable="true"
                    android:foreground="?attr/selectableItemBackground"
                    android:focusable="true">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        tools:ignore="UselessParent">

                        <ImageView
                            android:id="@+id/imgClienti"
                            android:layout_width="60dp"
                            android:layout_height="60dp"
                            android:layout_centerHorizontal="true"
                            android:adjustViewBounds="true"
                            android:padding="8dp"
                            android:scaleType="centerInside"
                            android:src="@drawable/ic_clienti"
                            android:tint="@color/colorWhite"
                            tools:ignore="ContentDescription" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/imgClienti"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="5dp"
                            android:text="AGGIUNGI CLIENTI"
                            android:textSize="14sp"
                            tools:ignore="HardcodedText" />

                    </RelativeLayout>

                </RelativeLayout>

          </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="0dp"
        android:layout_weight="1"
        tools:ignore="ObsoleteLayoutParam"
        android:baselineAligned="false">

        <RelativeLayout
            android:id="@+id/rellay_settings"
            android:layout_width="347dp"
            android:layout_height="match_parent"
            android:layout_weight="0.65"
            android:background="@color/blueLight"
            android:clickable="true"
            android:foreground="?attr/selectableItemBackground"
            tools:ignore="InefficientWeight,NestedWeights,UselessParent"
            android:focusable="true">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true">

                <ImageView
                    android:id="@+id/imgSettings"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:layout_centerHorizontal="true"
                    android:adjustViewBounds="true"
                    android:padding="8dp"
                    android:scaleType="centerInside"
                    android:src="@drawable/ic_settings"
                    android:tint="@color/colorWhite"
                    tools:ignore="ContentDescription" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/imgSettings"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="5dp"
                    android:text="IMPOSTAZIONI"
                    android:textSize="14sp"
                    tools:ignore="HardcodedText" />

            </RelativeLayout>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rellay_exit"
            android:layout_width="331dp"
            android:layout_height="match_parent"
            android:layout_weight="0.55"
            android:background="@color/red"
            android:clickable="true"
            android:foreground="?attr/selectableItemBackground"
            android:focusable="true">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                tools:ignore="UselessParent">

                <ImageView
                    android:id="@+id/imgExit"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:layout_centerHorizontal="true"
                    android:adjustViewBounds="true"
                    android:padding="8dp"
                    android:scaleType="centerInside"
                    android:src="@drawable/ic_exit"
                    android:tint="@color/colorWhite"
                    tools:ignore="ContentDescription" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/imgExit"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="5dp"
                    android:text="USCITA"
                    android:textSize="14sp"
                    tools:ignore="HardcodedText" />

            </RelativeLayout>

        </RelativeLayout>

     </LinearLayout>

  </LinearLayout>


</RelativeLayout>

Solution

  • Apply focus color via java

     int[][] states = new int[][] {
            new int[] { android.R.attr.state_pressed}, // pressed
            new int[] { android.R.attr.state_focused}, // focused
            new int[] { android.R.attr.state_enabled} // enabled
        };
    
    
    
         int[] colors = new int[] {
                Color.parseColor("#008000"), // green
                Color.parseColor("#0000FF"), // blue
                Color.parseColor("#FF0000")  // red
            };
    
            ColorStateList list = new ColorStateList(states, colors);
            textView.setTextColor(list);        
            textView.setClickable(true);
            textView.setFocusableInTouchMode(true);