android ui

16

Click here to load reader

Upload: jetti-chowdary

Post on 19-May-2015

4.640 views

Category:

Technology


0 download

DESCRIPTION

This presentation have- How to design the User-Interfce. How to use different layouts............

TRANSCRIPT

Page 1: Android Ui

Developing Efficient Android UI

Page 2: 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.

Page 3: Android Ui
Page 4: Android Ui

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.

Page 5: Android Ui

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.

Page 6: Android Ui
Page 7: Android Ui

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

Page 8: Android Ui

Pre-scaling a bitmap

Bitmap.createScaledBitmap(

originalImage, //bitmap to resize

view.getwidth() //new width

view.getHeight() //new Height

true); //bi-linear filterring

Page 9: Android Ui

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)

Page 10: Android Ui

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.

Page 11: Android Ui

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

Page 12: Android Ui

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();

Page 13: Android Ui

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>

Page 14: Android Ui

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.

Page 15: Android Ui

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.

Page 16: Android Ui

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.