Okay now we will continue our last lesson .. in Today’s lesson the first thing that we gonna learn is how to display a dialog window .. ?!
First we will create a button in our UI (main.xml file) ..
- This provides a unique identifier for the view, which you can use to reference the object from your app code, such as to read and manipulate the object (you’ll see this in the next lesson).The at sign (@) is required when you’re referring to any resource object from XML. It is followed by the resource type (id in this case), a slash, then the resource name (btn_Show).The plus sign (+) before the resource type is needed only when you’re defining a resource ID for the first time. When you compile the app, the SDK tools use the ID name to create a new resource ID in your project’s gen/R.java file that refers to the Button element. Once the resource ID is declared once this way, other references to the ID do not need the plus sign. Using the plus sign is necessary only when specifying a new resource ID and not needed for concrete resources such as strings or layouts.
wrap_content value specifies that the view should be only as big as needed to fit the contents of the view.
fill_parent then the Button element would fill the screen, because it would match the size of the parent LinearLayout
The next step .. we will go to our .java class and write the code that respond to click event on our button .. Let’s first descripe the shape of our Dialog that we will create .. our dialog will contain a small picture, a word beside it, some check boxes and two buttons.
First we will create an array to contain check boxes’ names, and another array to contain a boolean that indicate if this item is checked or not .. then we will do as we do before .. we will connect our button in .xml file with our button in .java code .. and then implement the function that when button pressed is fired .. and inside it we will call showDialog(int id) method and give the dialog a unique id .. 🙂
Because we will show a dialog on our screen .. so we need to override onCreateDialog() method that will be called when we call showDialog(int id) method. So now we will write this code in our onCreateDialog() method
The preceding code sets the Dialog Icon, Title and two buttons: OK and Cancel, using the setPositiveButton() and setNegativeButton() methods, respectively. We also set a list of checkboxes for users to choose via the setMultiChoiceItems() method. For the setMultiChoiceItems() method, you passed in two arrays: one for the list of items to display and another to contain the value of each item to indicate if they are checked. When each item is checked, you use the Toast class to display a message. A toast is a view containing a quick little message for the user. When the view is shown to the user, appears as a floating view over the application. And now when i run the App … Jerry Icon appears (i love tom & jerry :D)
The last thing i want to say the meaning of MainActivity.this that we use it as a parameter in Toast.makeText … On Android, a
Context is used for many operations but mostly to load and access resources .. and MainActivity.this returns the current context of the activity, if activity is destroyed then it will be destroyed also.
That’s all for now … wait the other parts of Activities and Intents series 🙂