<attr format="enum" name="fontName">
<enum name="gotham_ssm_bold" value="0"/>
<enum name="gotham_ssm_light" value="1"/>
<enum name="gotham_ssm_medium" value="2"/>
<enum name="gotham_ssm_book" value="3"/>
<enum name="gotham_ssm_black" value="4"/>
<enum name="gotham_ssm_italic" value="5"/>
</attr>
<declare-styleable name="customView">
<attr format="integer" name="textStyle"/>
<attr format="integer" name="selectedTextStyle"/>
<attr name="fontName"/> <!-- fontName referred in this styleable-->
</declare-styleable>
<declare-styleable name="appButtonView">
<attr name="textStyle"/>
<attr name="startColor"/>
<attr name="endColor"/>
<attr name="fontName" /> <!-- fontName referred in this styleable-->
</declare-styleable><declare-styleable name="appButtonView"> <!-- usage is demostrated below -->
<attr name="textStyle" format="integer" />
<attr name="startColor" format="reference|color"/>
<attr name="endColor" format="reference|color"/>
<attr name="textStartColor" format="color" />
<attr name="textEndColor" format="color" />
<attr name="icon" format="reference" />
<attr name="cornerRadius" format="dimension" />
<attr name="enableShadow" format="boolean" /> <!-- Access is demostrated below -->
</declare-styleable>and then use it at
<com.byjus.learnapputils.widgets.AppButton
android:id="@+id/resume_journey_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:gradientType="?attr/appButtonGradientStyle"
app:gradientShadow="?attr/appButtonGradientShadow"
app:shadowColor="@color/black_28"
app:roundedCorner="true"
style="@style/ButtonText"
/>and then access it at
public class AppButton extends AppCompatButton {
public AppButton(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.appButtonView, 0, 0);
boolean shadowEnabled = typedArray.getBoolean(R.styleable.appButtonView_enableShadow, false);
}
}
<style name="ButtonListText">
<item name="android:gravity">center_vertical</item>
<item name="android:textSize">@dimen/text_size_button_list_text</item>
<item name="android:textColor">#4a4a4a</item>
<item name="textStyle">@integer/regular</item>
<item name="android:textAllCaps">false</item>
</style><!-- A Styleable declares attributes related to a widget together -->
<declare-styleable name="RatingScreen">
<attr format="reference" name="ratingScreenHeaderImage"/>
</declare-styleable>
<!-- A style just gives values for a group of attributes across styleables-->
<style name="RatingScreenTheme" parent="PremiumNewAppTheme">
<item name="ratingScreenHeaderImage">@drawable/ic_premium_home_header</item>
</style>
<!-- Reference the attribute like below -->
<ImageView
android:id="@+id/ivHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitEnd"
app:srcCompat="?attr/ratingScreenHeaderImage"
/>
<!--
An attribute (ratingScreenHeaderImage) is referenced whose value will be given by theme
"ImageView" used in an Activity that has "RatingScreenTheme" applied, will get "@drawable/ic_premium_home_header" value
We can use attribute references (that get value from current theme) almost everywhere like **styles** , **drawables** , **color state list**.
-->| Style | Description |
|---|---|
| Style | A Key-Value Store where key is attribute. Specified the look of a single view. Applies to >1 logically related components |
| Theme Overlay | View level theme that overrides the one applied at the base context (parent view, activity (or) app |
| Theme | Type of Style applied to app or activity or view hierarchy |
| TextAppearence | Text specific styling that works at character level. |
| Hierarchy ↑ |
|---|
| View |
| Style |
| Default Style |
| Theme (or overlayed Theme) |
| Text Appearence |