SDK
All view are defined in sdk/platforms/android-x/data/res:
layout: The layour of all view
values: string items, styles (styles.xml) , attrs (attrs.xml)
Summary
"Widgets" are views that provide a visual (and interactive) elements for the screen, such as a button, text field, checkbox, or just an image.
Android provides a number of ready-made views that you can use to design and organize your
layout. You can also subclass the View and ViewGroup classes (or existing subclasses) to create your own widgets and layouts and apply them to your activity layout.
There are many types of views and view groups, each of which is a descendant of the View class. View can be divided into Widget and Layout.
A View object is a data structure whose properties store the layout parameters and content for a specific
rectangular area of the screen. A View object handles its own measurement,and key/gesture events.
UI design tips
ANR (Android No Responding dialog)
In Android, application responsiveness is monitored by the Activity Manager
and Window Manager system services. Android will display the ANR dialog
for a particular application when it detects one of the following
conditions:
No response to an input event (e.g. key press, screen touch)
within 5 seconds
BroadcastReceiver hasn't finished executing within 10 seconds
xxx
View
The base class of all widgets and ViewGroup(Layout)
setTag
Bind private data to the view.
setVisibility
Visible: view.setVisibility(View.VISIBLE);
Invisible: view.setVisibility(View.INVISIBLE);
Invisible and discard the space: view.setVisibility(View.GONE);
id
The syntax for an ID, inside an XML tag is: "@[+][package:]type:name"
The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource.
The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file). There are a number of other ID resources that are offered by the Android framework. When referencing an Android resource ID, you do not need the plus-symbol, but must add the android package namespace, like so: android:id="@android:id/empty".
An ID need not be unique throughout the entire tree, but it should be
unique within the part of the tree you are searching (which may often be the entire tree, so it's best
to be completely unique when possible).
padding & margin
Padding can be used to offset the content of the view. The padding space is belong of the View, while the space of margin is
not. Padding means set extra space from contensts, while margin means
add extra space from UI component.
size
The first pair is known as measured width and measured height. These dimensions define how big a view wants to be within its parent.
The second pair is simply known as width and height, or
sometimes drawing width and drawing height. These
dimensions define the actual size of the view on screen, at drawing time and
after layout. These values may, but do not have to, be different from the
measured width and height.
requestFocus
Make the control get focus.
xxx
SurfaceView
Provides a dedicated drawing surface embedded inside of a view hierarchy.
One of the purposes of this class is to provide a surface in which a
secondary thread can render into the screen.
GLSurfaceView
RemoteViews
A class that describes a view hierarchy that can be displayed in
another process. The hierarchy is inflated from a layout resource
file, and this class provides some basic operations for modifying
the content of the inflated hierarchy.
Button
Button
Set style
buttonStyleXXX defined in android.R.attr, you can apply button style as
style="?android:attr/buttonStyleXXX", such as small button:
style="?android:attr/buttonStyleSmall"
SeekBar
Like ProgressBar but with a draggable thumb, user can touch the thumb and drag left or right to set the progress.
RatingBar
Is extension of SeekBar that shows rating in stars.
Issue:
Refresh
When Activity is in background, the progress can be increased but the ProgressBar won't be refreshed. Later if you switch the Activity to foreground, the ProgressBar will not be refreshed if you don't call setProgress to set a new value, even if you call ProgressBar.invalidate(). i.e. When progress reaches 100 when the Activity is in background, then switch the Activity to foreground, you will find the ProgressBar is not freshed to 100.
Solution: call progressBar.setSaveEnable(false), or set android:saveEnabled="false" in layout xml file.
xxx
xxx
ImageView
ListView
When you add/remove line(s) to/from Adapter, you can call Adapter.notifyDataSetChanged() to notify AdapterView to refresh, but if you only change the contents of existing row(s), that API doesn't take effective.
Common ListView
Single Choice ListView
Multiple Choice ListView
Expandable Choice ListView (
Customized ListView containing complex item tow
Text View
TextView
Set preference group style with 'listSeparatorTextViewStyle'
...
style="?android:attr/listSeparatorTextViewStyle"
....
/>
set color
TextView.setTextColor(ColorStateList.valueOf(Color.WHITE));
rather than TextView.setTextColor(Color.WHITE);
xxx
EditText
Attributes
singleLine
Make sure only one line can be shown, and user can only input one line' contents.
lines
Make sure only the specified lines' contents can be shown at most, but user can input more lines' contents.
For the following layout, EditText will be stretched, and Button is aligned out of screen.
android:layout_height="wrap_content"
android:stretchColumns="0"
android:singleLine="true"
android:ellipsize="middle"
android:text="aaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbccccccccc" />
android:layout_height="wrap_content"
android:text="OK" />
To
resolve this issue, just add attribute android:maxWidth="10px" to
TextEdit, the value of maxWidth is arbitrary but must be less than
screen width.
if scroll bar overwrap contents within the scrollview, you can add android:scrollbarStype="insideInset" to ScrollView, i.e. android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbarStype="insideInset" >
or set dynamic in source code as:
findViewById(R.id.scroll_view_id).setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
xxx
CheckBox
Display as a star and can be checked
....
style="?android:attr/starStyle"
... />
ViewFlipper
Animate between two or more views
that have been added to it. Only one child is shown at a time. If
requested, can automatically flip between each child at a regular interval.
ViewSwitcher
Switches between two views, and has a factory from which these views are created. A ViewSwitcher can only have two child views, of which only one is shown at a time.
TextSwitcher
ViewSwitcher contains only children of type TextView. A TextSwitcher is useful to animate a label on screen. Whenever setText(CharSequence) is called, TextSwitcher animates the current text out and animates the new text in.
Sample: Search 'TextSwitcher1.java' in Google, or sample ApiDemo in Android sdk: Api Demos > Views > TextSwitcher
xxx
Customize View
Implement class MyView that inherits from View
onDraw
onKeyXXX
onTouchXXX
xxx
Create an object of MyView, you can add the object to ViewGroup
call Activity.setContentView to display the View/ViewGroup
You can define yourown attributes (styleable properties)
You can define new attributes of yourown, just as attrubites 'android:text', 'android:textColor' of widget TextView.
(Sample: ApiDemo. Related files: LabelView.java, attrs.xml, custom_view_1.xml, LabelView.java)
Enable alert() & console.log() in JavaScript
Invoke the following interface before loadurl.
webview.setWebChromeClient(new WebChromeClient()
{
Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result)
{
return super.onJsAlert(view, url, message, result);
}
});
Call method in Javascript
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/demo.html"); mWebView.loadUrl("javascript:fillContent()");
Others
AccessibilityService
Runs in the background and receives callbacks by the system
when AccessibilityEvents are fired. for example, the focus has changed, a button has been clicked,
etc.