android ui
DESCRIPTION
This presentation have- How to design the User-Interfce. How to use different layouts............TRANSCRIPT
Developing Efficient Android UI
Adapters in Android
• An Adapter object acts as a bridge between an View and the underlying data for that view.
• One side of the adapter is a data structure like a Java object storing data. the other side of the adapter, there is
view that the data structure was transformed into. • The Adapter is also responsible for making a View for
each item in the data set.
Considering Adaptor for the List View
• Each List item in the List View would be calling its “getView()” method.
• This method would return a view for the List.
• But creating a new View for each list item would be complicated, and the UI would be slow down.
• Instead of creating new View for each item, Recycler save these view and passes it to the Adapter.
• Adapter uses these view sent by the recycler, when it has to produce any new views.
View Holders• View Holders hold some data that related to an item to the view.• View Holders minimizes the amount of work, while writing the same piece of code again and again.• These are used while creating the views, where we have to use more than one time.
(TextView)findViewById(R.Id.text);(ImageView)findViewById(R.Id.Icon);
• By using the View Holders, the views are saved into a class variable and can be called when needed.
holder.text = (TextView)findViewById(R.id.text);holder.icon = (ImageView)findViewById(R.id.icon);convertView.setTag(holder);
• A tag can hold any kind of object, that can be set on the view.
Backgrounds
• Backgrounds can be any bitmap images or Colors or any resource from drawable directory.
• The background will completely fills the view.• Scaling a bitmap at runtime would be difficult as it has to
fill the complete view.• Instead pre-scale the bitmap and then set it as
background.• Else use the solid colors as background to fill the view.• This would increase the frame rate 50 frames per sec.
(In normal we have 25 frames per sec).
Pre-scaling a bitmap
Bitmap.createScaledBitmap(
originalImage, //bitmap to resize
view.getwidth() //new width
view.getHeight() //new Height
true); //bi-linear filterring
Drawing and Invalidating• Invalidate is a method used to redraw
• Using invalidate method the entire screen is redrawn again.
• This may consume some time to return to the normal state.
• While invalidating two areas on the screen (say, top right and bottom left of the screen) the frame work would take the union of the area and redraws.
• Solution for these problems would be:
invalidate(Rect)
and
invalidate(left, top, right, bottom)
• Frames would be increased to 50 per sec. (normally 18 frames)
Views and Layouts• View is a basic building block of the user interface
component.• A view occupies a rectangle area on the screen and is
responsible for drawing and event handling.• UI containing many Views would make the application to
run slower as it has to load every view accordingly.• Drawing of objects on many views is a difficult task and
requires more time to start an activity.• This may cause stackOverflow Exception (as the stack
size in android is 8kb).• Deep hierarchies makes the UI very slow.
Compound Drawables<Linearlayout android:orientation = “horizantal”
android:layout_width = “fill_parent”android:layout_height = “wrap_content” />
<TextView android:layout_width = “wrap_content”android:layout_height = “wrap_contentandroid:text = “@string/hello” />
<ImageView android:layout_width = “wrap_content”android:layout_height = “wrap_content”android:src = “@drawable/icon” />
</Linearlayout>
The above code can be written as:
<TextView android:layout_width = “wrap_content”android:layout_height = “wrap_content”android:text = “@string/hello”android:drawableLeft = “@drawable/icon” />
View Stub in android
• View Stub is a dumb and lightweight view.
• It has no dimensions and does not participate in Layouts.
• View Stud is a feature that allows runtime inflation of views.
<ViewStub
android:id=“@+id/stub”
android:inflateId=“@+id/panel_import”
android:lalyout = “@layout/progress_overlay”
android:gravity = “bottom” />
Inflating a View Stub….
findViewById(R.id.stub).setVisibility(View.VISIBLE);
or
View importPannel = ((ViewStub)findViewById(R.id.stub)).inflate();
Merge
• The <merge /> can only be the root tag in the xml file.• This tag would reduces the levels of hierarchies in the
xml layout file.
<merge xlms:android=“”>
<!– content -->
</merge>
Relative Layout
• Relative layout are powerful layout in android.• Relative layout replaces the Linear layout as the widgets
are placed in either horizontal or vertical way.• Each child element is laid out in relation to other child
elements.• Relationships can be established so that children will
start themselves where a previous child ends.• Children can relate only to elements that are listed
before them.
Custom Views
• Custom Views are very simple to use.• Just need to override only two methods.
onDraw(Canvas canvas)
onMeasure(int Width, int height) • We can define the dimensions of the view.
Custom Layouts
• Custom Layouts are used to define user defined layouts instead of using the pre-defined layouts.
• Using custom layout, user can define:
height and width of the layout
Childs residing on the layout
Visibility of the children during runtime.• To define custom layout,
A class should extend ViewGroup
override onMeasure and onLayout methods.