Search code examples
javaandroidorientationlandscapeportrait

Android : Change color of drawable rectangle and ActionBar depending on layout file


I've been stuck for a while on something that sounds basic. I want to change the ActionBar and two drawable rectangles colors according to the orientation of the device using two layout files. I also have two activities.

Right now, I've coded this into a controller (calling the function in OnResume) :

public void changeColor(int orientation) {
    String className = context.getClass().getSimpleName();

    if (orientation == Configuration.ORIENTATION_PORTRAIT) {
        if (className.equals("MainActivity")) {
            // Change actionbar, big square and small square colors
        }
        if (className.equals("WelcomeActivity")) {
            // Change actionbar color
        }
    } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
        if (className.equals("MainActivity")) {
            // change actionbar, big square and small square colors
        } else if (className.equals("WelcomeActivity")) {
            // change actionbar color
        }
    }
}

It sounds like something that could be fixed using a layout for portrait and a layout for landscape to have different colors for both of them. Is this possible? Right now with this function, it sounds like I'm reinventing the wheel.

EDIT : My drawable shape (rectangle) :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
    <padding android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
    <corners android:radius="25dp" />
    <solid android:color="#17D9C5" />
</shape>

Solution

  • You can provide different resources depending on the orientation of the device. In your case, add a color resource file res/values/colors.xml for portrait as well as a file res/values-land/colors.xml specifically for landscape.

    Then, define colors in both files like so

    <resources>
        <color name="colorPrimary">#008577</color>
        <color name="colorRectangle1">#00574B</color>
        <color name="colorRectangle2">#D81B60</color>
    </resources>
    

    and use different rgb values as desired.

    Next, use the color resources in your Activity theme. You can set the theme for an Activity in the Manifest.xml and declare it in res/values/styles.xml. The color for the ActionBar is determined by

    <item name="colorPrimary">@color/colorPrimary</item>
    

    And, last not least, use the color resources in the drawable resource files:

    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
        <padding android:left="2dp"
            android:top="2dp"
            android:right="2dp"
            android:bottom="2dp" />
        <corners android:radius="25dp" />
        <solid android:color="@color/colorRectangle1" />
    </shape>