Styles inline, create theme example code

Styles

android_styles

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.trankyphat.app.controlstyle.MainActivity">


    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/et"
        android:width="200dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="98dp"
        android:layout_marginStart="98dp"
        android:layout_marginTop="123dp" />

    <TextView android:id="@+id/errorText"
        style="@style/ErrorText"
        android:text="No errors at this time"
        />

    <!--
    <style name="CustomTextAppearance" parent="@android:style/TextAppearance">
    </style>-->

    <!-- ?android: Looking for android theme -->
    <EditText android:id="@+id/et2"
        android:layout_width="fill_parent" android:layout_height="wrap_content"

        android:textColor="?android:textColorSecondary"
        android:text="@string/hello_world" />
</RelativeLayout>

MainActivity.java

package com.trankyphat.app.controlstyle;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Spannable;
import android.text.style.BackgroundColorSpan;
import android.text.style.StyleSpan;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText et =(EditText)this.findViewById(R.id.et);
        et.setText("Styling the content of an EditText dynamically");
        Spannable spn = (Spannable) et.getText();
        spn.setSpan(new BackgroundColorSpan(Color.RED), 0, 7,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spn.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),
                0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}

/res/values/strings.xml

<resources>
    <string name="app_name">ControlStyle</string>
    <string name="hello_world">Hello World</string>
</resources>

/res/values/styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="ErrorText">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#FF0000</item>
        <item name="android:typeface">monospace</item>
    </style>
</resources>

Using Themes

One problem with styles is that you need to add an attribute specification of style=”@style/…” to every view definition that you want it to apply to. If you have some style elements you want applied across an entire activity, or across the whole application, you should use a theme instead. A theme is really just a style applied broadly; but in terms of defining a theme, it’s exactly like a style. In fact, themes and styles are fairly interchangeable: you can extend a theme into a style or refer to a style as a theme. Typically, only the names give a hint as to whether a style is intended to be used as a style or a theme.
To specify a theme for an activity or an application, add an attribute to the <activity> or <application> tag in the AndroidManifest.xml file for your project. The code might look like this:

<activity android:theme="@style/MyActivityTheme">
<application android:theme="@style/MyApplicationTheme">
<application android:theme="@android:style/Theme.NoTitleBar">

You can find the Android-provided themes in the same folder as the Android-provided styles, with the themes in a file called themes.xml. When you look inside the themes file, you will see a large set of styles defined, with names that start with Theme. You will also notice that within the Android-provided themes and styles, there is a lot of extending going on, which is why you end up with styles called Theme.Dialog.AppError, for example.

The Android guide to styles and themes: http://developer.android.com/guide/topics/ui/themes.html

Apress Pro Android 5

Android 6 – Styles and Themes

Category: Uncategorized
0
4041 views

Join the discussion

Your email address will not be published. Required fields are marked *