android interview questions and answers paper 14 - skillgunNote: Paper virtual numbers may be different from actual paper numbers . In the page numbers section website displaying virtual numbers .
What is the difference between px, dp, dip, and dpi ?
px - pixels, dp - density pixels, dip - density independent pixels, sp - scale independent pixels
px - pixels, dp - density pics, dip - density independent pics, sp - scale independent pixels
px - pixels, dp - density independent pixels, dip - density independent pixels, sp - scale independent pixels
px - pixels, dp - density independent pics, dip - density independent pics, sp - scale independent pixels
px - is meant for absolute pixels. This is used if you want to give in terms of absolute pixels for width or height. Not recommended.
dp/ dip - both mean same, density independent pixels. This is alternative of px. Generally we never use px because it is absolute value. If you use px to set width or height, and if that application is being downloaded into different screen sized devices, then that view will not stretch as per the screen original size. dp is highly recommended to use in place of px.
Use dp if you want to mention width and height to grow & shrink dynamically based on screen sizes.
if we give dp/dip, android will automatically calculate the pixel size on the basis of 160 pixel sized screen.
sp - Scale independent pixels. when mentioning the font sizes to fit for various screen sizes, use sp. This is similar to dp.Use sp especially for font sizes to grow & shrink dynamically based on screen sizes.
dpi- dots per inch. It is the resolution factor. More the dpi better and higher the screen resolution.
Math logic behind it:
1dp = 1px in 160 dpi screen. Means 1 dp is equal to 1physical px of a device having 160 dots per inch.
so no of dynamic pixles for a given dp is = (dp's)*(density/160).
For example on 240dpi screen 1dp = 1.5px (physical pixels)
What is android:gravity attribute in the view tag?
it is to align the view either right/top/bottom/center in its parent layout
it is to align the view content either right/top/bottom/center with in that view.
it is to align the parent layout of the view either in right/top/bottom/center.
it is to weigh the view with respect to other views in that layout
android:gravity property is used to align the view content either right/top/bottom/center/.. with in that view.
What is the difference between linear layout and relative layout?
linear layout - arranges element in either vertical or horizontal fashion. Relative layout - arranges elements in relative to each other.
linear layout - arranges element in only vertical fashion. Relative layout - arranges elements in relative to each other.
linear layout - arranges element in either absolute fashion. Relative layout - arranges elements in relative to other layouts.
linear layout - arranges element in either vertical or horizontal fashion. Relative layout - arranges elements in stack base architecture (piled up).
linear layout - arranges child element in either vertical or horizontal fashion.
Relative layout - arranges child elements in relative to each other.
Important properties of Relative Layout:
How to kill an activity?
ii. finishActivity(int requestcode)
both i and ii
i, ii, & iii
Activity can be killed programatically in two ways. finish() or finishActivity(int requestcode).
finish() - can be used by an activity to kill itself.
finishActivity(int requestcode) - Force finish another activity that you had previously started with startActivityForResult(Intent intent, int requestcode); This function will let the parent activity to kill the child activity, which it has started previously.
what is the life cycle of an activity in case of configuration change or orientation change?
onPause() -> onSaveInstanceState() -> onCreate() -> onStart() -> onRestoreInstanceState() -> onResume()
onPause() -> onSaveInstanceState() -> onStop() -> onCreate() -> onStart() -> onRestoreInstanceState() -> onResume()
onPause() -> onSaveInstanceState() -> onStop() -> onDestroy()->onCreate() -> onStart() -> onRestoreInstanceState() -> onResume()
can be any of the above based on the situation.
Configuration change : is either orientation change(phone rotation), or language settings change.
In case of configuration change, android will forcefully kill your activity and recreates it, unless you are not handling configuration changes on your own programmatically.
When android kills your activity due to configuration changes, it makes sure that it will definitely call onPause() and onSaveInstanceState. But there is no guarantee about call onStop and onDestroy. But android documentation doesn't say that it will not call onStop and onDestroy. it depends.
Life cycle in the case of configuration changes can be any of the option 1, or 2, or 3, based on the situation. some times it might not kill activity at all if programmer is handling configuration changes programmatically, which is not a standard way to do.
How many kinds of linear layouts are there in android?
absolute linear layout, normal linear layout
horizontal & vertical linear layout
frame & absolute linear layout
linear layout & relative linear layout
two types of linear layouts are available. horizontal & vertical.
Linear Layout: Linear layout can have only two orientations either vertical or horizontal.It will align the child views in the linear fashion either vertically or horizontal.
By default linear layouts will use horizontal, if you don't specify any orientation.
Activity life cycle: Why you should not do heavy functionality in onPause() of your activity?
because android may kill your activity at any point of time from then on
because user is eagerly waiting for next activity to show up
because it has time limit of 5 seconds
because it has time limit of 10 seconds
onPause() will be called as the first indication that user is moving away from your activity, that means intentionally user is moving to next screen, so more the time you hold control in onPause() more it will irritate user. so don't hold control here for too much of time.
Activity life cycle: What to do in onRestart() of an activity?
load your xml layout file
if any UI changes happened while it is in invisible state, updated it in this function
if any UI changes happened while it is in running state, updated it in this function
Used to reload all UI resources in case of activity recreation which happens in case of configuration changes.
onRestart() will be called, after onStop(). when onStop is called it means your activity is in invisible state, during that time if any ui changes happens which is not visible to user, update all those changes to user in onRestart()
Is it possible to give cursor to an array adapter as a source?
option 2 is right, because array adapter takes only lists
option 3 is wrong because cursors is also one list.
array adapter takes - lists (arraylists);
cursor adapter takes - cursor which is returned from database tables upon querying.
what are the mandatory functions to be implemented in custom adapter?
onCreate(), onStart(), getView()
getCount(), getView(), getItem(), getItemId()
only getView() is mandatory to implement.
only getView() & getCount() is mandatory to implement.
If one is implementing custom adapter by extending BaseAdapter, then he or she has to give implementation of all 4 functions mentioned getView(), getItem(), getCount(), getItemId().
How to create a custom adapter in Android? What is the class that I need to extend to create my own adapter?
Extend Adapter class
Extend Either one of the conceret Adapters or BaseAdapter
To create a own custom adapter, one can extend BaseAdapter, or can extend any of the existing concrete adapters like ArrayAdapter, SimpleCursorAdatper etc..
Note: we can also extend or implement other adapter interfaces, but it is not so useful. Generally extending BaseAdapter is enough to create our own custom adapter.
What is an adapter in android?
bridge between source and layouts
bridge between source and adapter views
bridge between listview and view
bridge between view and activity
Adapter Design Patterns:
1. If destination is expecting one kind of input and source is giving one kind of input then we have to use adapters to convert source into destination.
2. Whenever you change the source we have to use a different kind of adapter.
adapter design pattern is used for bridging between source and adapter views.
Functionalities of Adapter:
It takes the input from source and gives to the destination.
Any view that is getting input from any adapter is called as Adapter View.
List view is an example of adapter view.
List view generally will contain vertically scrollable list of items.
NOTE: Source should not directly communicate with destination (Adapter view).
FUNCTIONALITIES OF ADAPTERS IN AANDROIDANDROIDNDROID:
1. Adapters will take inputs from source and gives to the destination.
2. If there is any change in the source and if we notify to the adapter then adapter will go and modify the destination.
3. Adapter will take each data item from source and prepare a view and dispatches that view to the destination.
Examples for adapters:
eg1 : If you want to display data from array list into listview, then we use arrayadapter in android.
eg2 : if you want to display data from database into a listview, then we use cursoradapter in android.
What is an adapterview? How many adapter views are available in android?
listview is an adapterview, eg of adapter views are : listview, tablelayout, spinner
linear layout is an adapterview, eg of adapter views are : linear layout, relative layout, and frame layout
any view that takes input from adapter is called as adapter view. eg:listview, imageview, spinner, gridview, etc..
any view that takes input from adapter is called as adapter view. eg:listview, gallery, spinner, gridview, etc..
When activity is destroyed, will it be in memory or moved out of it?
Yes, all destroyed apps will be removed from memory
Yes, all destroyed apps will be removed from memory only after some time.
A destroyed application will be removed from memory if it is not frequently visited by user.
option 3 is true because, if user is visiting an app frequently, then it has to be loaded many times.
Generally after calling onDestroy(), app will be removed from memory. But there is an exception for this rule. If user is visiting an app very frequently then it has to be loaded into memory very frequently. to avoid this over head for the system, android may choose to keep that app in memory even after onDestroy(). This is called "empty process". Process which is killed but still in memory.
Start messaging app –> composer activity -> gallery -> camera -> press home button. Now which state camera activity is in?
When we press home button any activity, that will be moved to back ground state (invisible state), so it calls onStop() on that activity
What is Bundle? What does it contain in onCreate() of your activity?
Bundle will be null always in onCreate()
Bundle contain previous savedInstantceState
Bundle contain previous restoredInstanceState
Bundle contains information passed from other activities when it was started using startActivity()
Bundle is a data holder, which holds data to be passed between activities.
In case of forceful closing of an activity, android will save all its UI states and transient states so that they can be used to restore the activity's states later. This saved states will be passed via Bundle in onCreate() function to restore its states once android recreates a killed activity.
EG: this will happen in case of low memory or configuration changes like rotating the phone.
This function will be called by aAndroidAndroidndroid before “ompause” or after “onpause” if aAndroidAndroidndroid is forcefully killing your activity. In this function we have to save all your activity states.
This function will be called after “onStart”.
How to get image from gallery, in android?
Intent in = new Intent();
Intent in = new Intent();
Intent in = new Intent();
Intent in = new Intent();
Name some list adapters?
all view groups are adapters only
array adapter, cursor adapter, expandable list adapter, etc..
both option 1 & 2
option 2 is right, and all layouts are also adapters.
adapter examples: arrayadapter , base adapter, cursor adapter, expandable list adapter,..
If I want to start some heavy weight functionalities that takes lot of battery power like starting animation or starting camera, should I do it in onCreate() or onStart() or onResume() of my activity? And where should I disable it?
can do it any where in above 3 functions, no harm
Since heavy weight functions take too much of battery power, better do it just before your activity is ready to take user events. so do it in onResume().
When an activity is in stopped state, is it still in memory or not?
when onStop() is called, then activity is removed from memory
when onStop() is called, then activity will be removed from memory in fraction of seconds
when onStop() is called, then activity is still in memory and all its states and variables are intact.
when onStop() is called, then activity is still in memory and all its states and variables are intact. But it will be removed from memory with in 5 seconds.
Back To Top