Skip to content

Instantly share code, notes, and snippets.

@ckarthickit2
Last active March 11, 2021 18:36
Show Gist options
  • Select an option

  • Save ckarthickit2/e19bfb5b84a6fcbe3b77fe8c89648a91 to your computer and use it in GitHub Desktop.

Select an option

Save ckarthickit2/e19bfb5b84a6fcbe3b77fe8c89648a91 to your computer and use it in GitHub Desktop.
AndroidTheming

Theming

Declare an Attribute that could be part of any Styleable

<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 a 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);
  }
}

Declaring a Style for Styleable

<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>

Referencing an Attribute

<!-- 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**.
 -->

Theme vs ThemeOverlay vs Style vs TextAppearence

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.

Styling Precedence Hierarchy

Hierarchy ↑
View
Style
Default Style
Theme (or overlayed Theme)
Text Appearence

References


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment