androidcomponentsstylinglayer-list

Android Development - Where to find "original" obscure component documentation


I have been tasked with creating an Android app, and have done some studying about Android in the past. I have watched several video tutorials from developer.android.com, and am gaining somewhat of a grasp on how things in Android work and how the system of files is laid out and works together.

Lately I have been exploring making my own styling on various components, and right now I am trying to style a ProgressBar. I have learned that the progressDrawable attribute can point to a layer-list that contains items that style the background, the progress, and the secondary progress.

What I do not understand, is how do people know exactly which IDs to include in the layer list, and what exactly those IDs reference? I have searched for hours online through the documentation, through articles, through API Demos, through the source code (by Ctrl+Click in Android Studio), etc. But I cannot find a single place that mentions where this information originally came from. How do people know which three IDs to include in the layer-list? It would be really great if somebody could link to exactly where the official information is about where and how these id's from ids.xml are used in styling components.

Thanks!


Solution

  • how do people know exactly which IDs to include in the layer list, and what exactly those IDs reference?

    If it wasn't in the ProgressBar docs, I would find a framework-supplied ProgressBar drawable and see what they did. For example, look at $ANDROID_SDK/platforms/android-$API/data/res/drawable/progress_horizontal_material.xml, where:

    If you look in there, you will see that the stock Theme.Material background for ProgressBar is:

    <?xml version="1.0" encoding="utf-8"?>
    <!-- Copyright (C) 2014 The Android Open Source Project
    
         Licensed under the Apache License, Version 2.0 (the "License");
         you may not use this file except in compliance with the License.
         You may obtain a copy of the License at
    
              http://www.apache.org/licenses/LICENSE-2.0
    
         Unless required by applicable law or agreed to in writing, software
         distributed under the License is distributed on an "AS IS" BASIS,
         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         See the License for the specific language governing permissions and
         limitations under the License.
    -->
    
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@id/background"
              android:gravity="center_vertical|fill_horizontal">
            <shape android:shape="rectangle"
                   android:tint="?attr/colorProgressBackgroundNormal">
                <corners android:radius="?attr/progressBarCornerRadius" />
                <size android:height="@dimen/progress_bar_height_material" />
                <solid android:color="@color/white_disabled_material" />
            </shape>
        </item>
        <item android:id="@id/secondaryProgress"
              android:gravity="center_vertical|fill_horizontal">
            <scale android:scaleWidth="100%">
                <shape android:shape="rectangle"
                       android:tint="?attr/colorControlActivated">
                    <corners android:radius="?attr/progressBarCornerRadius" />
                    <size android:height="@dimen/progress_bar_height_material" />
                    <solid android:color="@color/white_disabled_material" />
                </shape>
            </scale>
        </item>
        <item android:id="@id/progress"
              android:gravity="center_vertical|fill_horizontal">
            <scale android:scaleWidth="100%">
                <shape android:shape="rectangle"
                       android:tint="?attr/colorControlActivated">
                    <corners android:radius="?attr/progressBarCornerRadius" />
                    <size android:height="@dimen/progress_bar_height_material" />
                    <solid android:color="@color/white" />
                </shape>
            </scale>
        </item>
    </layer-list>
    

    (based on API Level 29; other versions might differ, but probably not significantly)

    You can clone that file into your project's res/drawable/ directory, tweak it as needed, and then use the tweaked version.

    IOW, if it is not in the docs, the "official" information is the Android source code.

    Note that for Theme.Material, if all you need to do is change the colors, you can leave the drawable alone and use the various tint attributes on ProgressBar.