Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java for android Development Nasrullah Khan. Using instanceof in Android Development the classes such as Button, TextView, and CheckBox, which represent.

Similar presentations


Presentation on theme: "Java for android Development Nasrullah Khan. Using instanceof in Android Development the classes such as Button, TextView, and CheckBox, which represent."— Presentation transcript:

1 Java for android Development Nasrullah Khan

2 Using instanceof in Android Development the classes such as Button, TextView, and CheckBox, which represent different types of user interface controls, are all derived from the same parent class: View. Therefore, if you wanted to create a method that took a View parameter, but had different behavior depending upon the specific type of control, you could use the instanceof mechanism to check the incoming parameter and determine exactly what kind of view control had been passed in.

3 void checkforTextView(View v) { if(v instanceof TextView) { // This is a TextView control } else { // This is not a TextView control } }

4 Inner classes Nested classes are called inner classes You can also create nested classes in order to clearly encapsulate and define subordinate objects that only matter in the context of the outer class, Inner classes can have all the features of a regular class, but their scope is limited. Inner classes have another benefit: they have full access to the class in which they are nested—this feature makes inner classes perfect for implementing adapter functionality like iterators. inner classes really only exist to help the developer organize code

5 Using static Nested Classes A static inner class defines behavior that is not tied to a specific object instance, but applies across all instances User.ServerInfo sInfo = new User.ServerInfo(); a static nested class, does not have access to members of the outer class–

6 Power of annoymous inner class Anonymous inner classes are basically developer shorthand, allowing the developer to create, define, and use a custom object all in one “line.” To create an anonymous inner class, you only provide the right-hand side of the definition. Begin with the new keyword, followed by the class or interface you wish to extend or implement, followed by the class definition. This will create the class and return it as a value which you can then use to call a method

7 Using an Anonymous Inner Class to Define a Listener Android developers often use anonymous inner classes to define specialized listeners, which register callbacks for specific behavior when an event occurs. For example, to listen for clicks on a View control, the developer must call the setOnClickListener() method, which takes a single parameter: a View.OnClickListener object.

8 Android developers often use anonymous inner classes to define specialized listeners, which register callbacks for specific behavior when an event occurs. For example, to listen for clicks on a View control, the developer must call the setOnClickListener() method, which takes a single parameter: a View.OnClickListener object. Developers routinely use the anonymous inner class technique to create, define and use their custom View.OnClickListener,

9 Using a named inner class Let’s say you have a screen that has 100 buttons on it (we did say hypothetical, right?). Now, let’s say each button, when pressed, does the exact same thing. In this case, we’ll just listen for clicks and Toast the text from the View object passed in (the text shown on the Button that was clicked):

10 How to show a Dialog in Android

11 1.Create the Dialog (for example, using an AlertDialog.Builder), and then call the newly created Dialog's show() method. 2.Call the Activity's showDialog() method, passing in an int that uniquely defines what sort of Dialog you want to build. Then override onCreateDialog() to actually build the Dialog, and Android will display it for you.

12 Reasons to use Activity.showDialog The API docs for Activity.showDialog say that the Dialog is "managed" by the Activity which I suppose provides some benefit? But this is also true if you use the AlertDialog.Builder, I would think, because you pass in this as an argument to the Builder's constructor. If your Activity is going to show the same (or a very similar) Dialog several times, this option creates it only once, instead of creating a new one each time, thus putting less strain on the system as far as allocating space for new objects, garbage collection, etc.

13 Context class in Android A Context has a lot of functions, but as a developer, you primarily use it to load and access application resources. You can think of Context, like the end-user-interface that will use that code. When you are in a class you can know based on Context if you have visual screen(Activity), or a running service(Service). To compare against some other programming example, you can think of Context is equal to Console App, GUI App, or even Applet.

14 Context object also provides access to: API for working with database files (not DB functionality!) API for working with files and directories API for working with permissions API for binding to services and starting/stopping them API for working with cache API for working with OBBs API for getting strings and formatted text from package resources API for getting various data relevant to the current package API for working with the current Theme (see Resources.Theme)Resources.Theme Method for retrieving system services (all …Managers are retrieved through it) API for handling broadcasts API for starting and stopping activities Method for starting Instrumentation objectInstrumentation ApplicationInfo object for the application ApplicationInfo Context object associated with the current application process ClassLoader object associated with the package ClassLoader ContentResolver object associated with the package ContentResolver Looper object associated with the main thread of the current process Looper Resources object associated with the package Resources


Download ppt "Java for android Development Nasrullah Khan. Using instanceof in Android Development the classes such as Button, TextView, and CheckBox, which represent."

Similar presentations


Ads by Google