Friday, September 20, 2013

Android: Set Custom Attributes Via Programmatically and XML



Resources.Theme holds the current attribute values for a particular theme. In other words, a Theme is a set of values for resource attributes; these are used in conjunction with TypedArray to resolve the final value for an attribute.
The Theme's attributes come into play in two ways:
1.      A styled attribute can explicit reference a value in the theme through the "?themeAttribute" syntax;
2.      If no value has been defined for a particular styled attribute, as a last resort we will try to find that attribute's value in the Theme.
Let us take first point, where we need to get the value of a custom attribute programmatically.
We can access custom attr programmatically with the help of Resources.Theme class.

TypedValue, Container for a dynamically typed data value.
resolveAttribute, Retrieve the value of an attribute in the Theme.

Example:
Create two TextView in main.xml file, where we are changing background of TextView using custom attribute via xml and programmatically.

Define your own custom attribute: Create attr.xml in your layout folder
xml version="1.0" encoding="UTF-8"?>
<resources>

    <attr name="CustomYellowColor" format="reference|color" />
    <attr name="CustomBlueColor" format="reference|color" />

</resources>

Change Style.xml file
<style name="AppTheme" parent="AppBaseTheme">
    <item name="CustomYellowColor">@color/yellow</item>
    <item name="CustomBlueColor">@color/blue</item>
</style>

<color name="yellow">#FFFF00</color>
<color name="blue">#0040FF</color>

Go to your main.xml and change background color of a TextView’s

XML:
<TextView
   android:id="@+id/textView2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="?attr/CustomBlueColor"
   android:text="@string/xml" />

Programmatically:

TextView prgmTV = (TextView) findViewById(R.id.textView1);
Resources.Theme themes = getTheme();
TypedValue storedValueInTheme = new TypedValue();
if (themes.resolveAttribute(R.attr.CustomYellowColor, storedValueInTheme, true)) {
            prgmTV.setBackgroundColor(storedValueInTheme.data);
}


Output:

Android: ExpandableListView using CursorTreeAdapter with in fragment, Content Provider, LoaderCallbacks and CursorLoader



Sample application using Single-pane and Multi-pane Layouts with ExpandableListView using CursorTreeAdapter with in fragment, Content Provider, LoaderCallbacks & CursorLoader.

MULTI-PANE LAYOUTS
On a tablet, you want to take advantage of all the space. Get rid of line lengths that stretch the whole screen. Multi-pane layouts allow you to resolve than. Panes should represent content in an ordered manner; from left-to-right... content becomes more detailed.

Rule is to maintain functional parity in all screen orientations. We have a few strategies to solve this. You can use all or some. You can either: stretch, stack, expand/collapse, or show/ hide different UI panes, depending on the screen orientation. Some figures demonstrate these designs.

Implementation should be done with Fragments. A fragment is "a fragment of an activity". It's a unit that can be reused across activities. It's something smaller than an activity that shares lifecycle patterns, but is focused on a sub-layout that's separated in your codebase.


Ref: AndroidUIs





Figure: Implementing Android UI's for Handset and Tablets
 


On a tablet-sized screen, the Activity A layout contains both Fragment A and Fragment B.
On a handset-sized screen, the Activity A layout contains only Fragment A (the list view). In order to show the details in Fragment B, Activity B must open.

In Expandable List, Child Query is fetched from CursorLoader, We may get doubt, Why we should go for Cursor Loader and what are their adv.?

A CursorLoader automatically re-runs the query when data associated with the query changes. It runs an asynchronous query in the background against a ContentProvider, and returns the results to the Activity or FragmentActivity from which it was called. This allows the Activity or FragmentActivity to continue to interact with the user while the query is ongoing.

Loading Data in the Background

Querying a ContentProvider for data you want to display takes time. If you run the query directly from an Activity, it may get blocked and cause the system to issue an "Application Not Responding" message. Even if it doesn't, users will see an annoying delay in the UI. To avoid these problems, you should initiate a query on a separate thread, wait for it to finish, and then display the results.
You can do this in a straightforward way by using an object that runs a query asynchronously in the background and reconnects to your Activity when it's finished. This object is aCursorLoader. Besides doing the initial background query, aCursorLoader automatically re-runs the query when data associated with the query changes.
This class describes how to use a CursorLoader to run a background query. Examples in this class use the v4 Support Library versions of classes, which support platforms starting with Android 1.6.

Running a Query with a CursorLoader

A CursorLoader runs an asynchronous query in the background against a ContentProvider, and returns the results to the Activity or FragmentActivity from which it was called.

Source Code: Here

Thanks for reading :)
If you have any other quick thoughts/hints that you think people will find useful, feel free to leave a comment.