Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore CU-MCA-Mobile Applications Development Second draft-converted

CU-MCA-Mobile Applications Development Second draft-converted

Published by Teamlease Edtech Ltd (Amita Chitroda), 2021-05-04 06:33:44

Description: CU-MCA-Mobile Applications Development Second draft-converted

Search

Read the Text Version

3. For __________ , code used by Android is not an open source a. Device driver b. video driver c. WiFi driver d. Bluetooth driver 4. How to instantiate a Spinner control? a. Via XML layout b. Via code c. All of these d. None of these 5. Which of the following gwtwidget represents single line text box a. Text area b. GWT textbox c. suggest box d. suggest box Answers 1-b, 2-a, 3-c, 4-c, 5-a 4.14 REFERENCES Reference Books • Khoshafian, et al. (1990). Object orientation: Concepts, Languages, Databases. New York: John Wiley & Sons • Thornsby, J. (May 2016). Android UI Design. Packt Publishing. Morris, J. (Feb 2011). Android User Interface Development - Beginner’s Guide. UK. Packt Publishing Websites • https://enos.itcollege.ee/~jpoial/allalaadimised/reading/Android-UI-Design.pdf • https://www.techotopia.com/index.php/Understanding_Android_Views,_View_Group s_and_Layouts 101 CU IDOL SELF LEARNING MATERIAL (SLM)

• https://www.tutorialspoint.com/software_architecture_design/user_interface.htm • https://www.wideskills.com/android/building-user-interface/listening-to-ui- notifications-in-android • https://developer.android.com/guide/fragments/lifecycle 102 CU IDOL SELF LEARNING MATERIAL (SLM)

UNIT - 5: UI CONTROLS Structure 5.0 Learning Objectives 5.1 Introduction 5.2 Auto Complete Text View 5.3 Alert Dialog Box 5.4 Rating Bar 5.5 Date & Time Picker 5.6 Summary 5.7 Keywords 5.8 Learning Activity 5.9 Unit End Questions 5.10 References 5.0 LEARNING OBJECTIVES After studying this unit, you will be able to: • Define Auto Complete Text View • Explain Alert Dialog Box • Demonstrate working with Rating Bar • Illustrate Date & Time Picker 5.1 INTRODUCTION In android UI or input controls are the interactive or View components that are used to design the user interface of an application. In android we have a wide variety of UI or input controls available, those are TextView, EditText, Buttons, Checkbox, Progressbar, Spinners, etc. 5.2 AUTO COMPLETE TEXT VIEW Android autocompletetextview completes the phrase-based totally on the reserved words, so no 103 CU IDOL SELF LEARNING MATERIAL (SLM)

Need to write down all of the characters of the word. 104 Autocompletetextview example Let's examine the simple example of autocompletetextview in android. activity_main.xml <?Xml version=\"1.0\" encoding=\"utf-8\"?> <android.Support.Constraint.ConstraintLayout xmlns:android=\"http://schemas.Android.Com/apk/res/android\" xmlns:app=\"http://schemas.Android.Com/apk/res-auto\" xmlns:tools=\"http://schemas.Android.Com/tools\" android:layout_width=\"match_parent\" android:layout_height=\"match_parent\" tools:context=\"example.Jst.Com.Autocompletetextview.MainActivity\"> <TextView android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:text=\" favourite programming language?\" app:layout_constraintBottom_toBottomOf=\"parent\" app:layout_constraintLeft_toLeftOf=\"parent\" app:layout_constraintRight_toRightOf=\"parent\" app:layout_constraintTop_toTopOf=\"parent\" app:layout_constraintVertical_bias=\"0.032\" /> <AutoCompleteTextView android:id=\"@+id/autoCompleteTextView\" android:layout_width=\"200dp\" android:layout_height=\"wrap_content\" android:layout_marginLeft=\"92dp\" android:layout_marginTop=\"144dp\" CU IDOL SELF LEARNING MATERIAL (SLM)

android:text=\"\" app:layout_constraintStart_toStartOf=\"parent\" app:layout_constraintTop_toTopOf=\"parent\" /> </android.Support.Constraint.ConstraintLayout> Code for autocompletetextview. File: mainactivity.Java Import android.Portraits.Shade; Import android.Os.Package; Import android.Widget.Arrayadapter; Import android.Widget.Autocompletetextview; Public class mainactivity extends appcompatactivity { string[] language =\"c\",\"c++\",\"java\",\".Net\",\"ios\",\"android\",\"Internet\",\"Hypertext Preprocessor\"; @override covered void oncreate(package savedinstancestate) exquisite.Oncreate(savedinstancestate); setcontentview(r.Format.Activity_main); //developing the instance of arrayadapter containing listing of language names arrayadapter<String> adapter = new arrayadapter<String> (this,android.R.Format.Select_dialog_item,language); //getting the example of autocompletetextview autocompletetextview act = (autocompletetextview)findviewbyid(r.Identification.Autocompletetextview); act.Setthreshold(1); act.Setadapter(adapter); 105 CU IDOL SELF LEARNING MATERIAL (SLM)

act.Settextcolor(colour.Red); }}; Figure 5.1 Output of autocomplete textview 5.3 ALERT DIALOG BOX Android alert dialog can be used to display the conversation message with good enough and cancel buttons. It could be used to interrupt and ask the consumer approximately his/her desire to hold or discontinue. Android alert dialog consists of three regions: name, content area, and motion buttons. Android alert dialog is the subclass of the dialog class. Methods of Alert Dialog class Method Description public This method is used to set the title of Alert AlertDialog.BuildersetTitle(CharSequence) Dialog. 106 CU IDOL SELF LEARNING MATERIAL (SLM)

public This method is used to set the message for AlertDialog.BuildersetMessage(CharSeque Alert Dialog. nce) public AlertDialog.BuildersetIcon(int) This method is used to set the icon over Alert Dialog. Android Alert Dialog Example Android alert dialog example An instance of android alert dialog. Activity_main.Xml You could have more than one components, here we're having handiest a textview. Record: activity_main.Xml <?Xml version=\"1.0\" encoding=\"utf-8\"?> <android=\"http://schemas.Android.Com/apk/res/android\" xmlns:app=\"http://schemas.Android.Com/apk/res-auto\" xmlns:tools=\"http://schemas.Android.Com/tools\" android:layout_width=\"match_parent\" android:layout_height=\"match_parent\" tools:context=\"example.Jst.Com.Alertdialog.MainActivity\"> <Button android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:id=\"@+id/button\" android:text=\"Close app\" app:layout_constraintBottom_toBottomOf=\"parent\" app:layout_constraintLeft_toLeftOf=\"parent\" app:layout_constraintRight_toRightOf=\"parent\" app:layout_constraintTop_toTopOf=\"parent\" /> </android.Support.Constraint.ConstraintLayout> ________________________________________ Strings.Xml Optionally, you can shop the dialog message and name within the strings.Xml report. Record: strings.Xml <resources> <string name=\"app_name\">alert dialog</string> <string name=\"dialog_message\">welcome to alert conversation</string> 107 CU IDOL SELF LEARNING MATERIAL (SLM)

<string name=\"dialog_title\">jst alert dialog</string> </resources> ________________________________________ Interest magnificence Code to create and display the alert dialog. File: mainactivity.Java Import android.Content material.Dialoginterface; Import android.Os.Package; Import android.View.View; Import android.Widget.Button; Import android.App.Alertdialog; Import android.Widget.Toast; Public class mainactivity extends appcompatactivity button closebutton; alertdialog.Builder builder; @override included void oncreate(bundle savedinstancestate) exceptional.Oncreate(savedinstancestate); setcontentview(r.Format.Activity_main); closebutton = (button) findviewbyid(r.Identity.Button); builder = new alertdialog.Builder(this); closebutton.SetonClicklistener(new view.onClicklistener() @override public void onClick(view v) builder.Setmessage(r.String.Dialog_message) .Settitle(r.String.Dialogtitle); builder.Setmessage(\"do you need to shut this application ?\") .Setcancelable(fake) .Setpositivebutton(\"sure\", new dialoginterface.onClicklistener() public void onClick(Dialoginterface dialog, int identity) finish(); toast.Maketext(getapplicationcontext(),\"you pick sure movement for alertbox\", toast.Length_short).Show(); ) 108 .Setnegativebutton(\"no\", new dialoginterface.onClicklistener() CU IDOL SELF LEARNING MATERIAL (SLM)

alertbox\", public void onClick(Dialoginterface conversation, int identity) // action for 'no' button dialog.Cancel(); toast.Maketext(getapplicationcontext(),\"you pick out no action for toast.Length_short).Display(); ); alert dialog alert = builder.Create(); //setting the title manually alert.Settitle(\"alert_dialog\"); alert.Display(); ); : Figure 5.2:Alert Dialog Output 5.4 RATING BAR Android RatingBar can be used to get the score from the user. The rating returns a floating- value. 109 CU IDOL SELF LEARNING MATERIAL (SLM)

Android RatingBar presents the rating in stars. Android RatingBar is the subclass of absSeekbar class. The getRating() method is used to returns the score in number. Android RatingBar instance Let's examine the easy instance of a score bar in android. Activity_main.Xml File: activity_main.Xml <?Xml version=\"1.0\" encoding=\"utf-8\"?> <android.Support.Constraint.ConstraintLayout xmlns:android=\"http://schemas.Android.Com/apk/res/android\" xmlns:app=\"http://schemas.Android.Com/apk/res-auto\" xmlns:tools=\"http://schemas.Android.Com/tools\" android:layout_width=\"match_parent\" android:layout_height=\"match_parent\" > <Button android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:text=\"submit\" android:id=\"@+id/button\" app:layout_constraintBottom_toBottomOf=\"parent\" app:layout_constraintLeft_toLeftOf=\"parent\" app:layout_constraintRight_toRightOf=\"parent\" app:layout_constraintTop_toTopOf=\"parent\" app:layout_constraintVertical_bias=\"0.65\" /> <RatingBar android:id=\"@+id/RatingBar\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:layout_marginLeft=\"72dp\" android:layout_marginTop=\"60dp\" app:layout_constraintStart_toStartOf=\"parent\" app:layout_constraintTop_toTopOf=\"parent\" /> </android.Support.Constraint.ConstraintLayout> 110 ________________________________________ Activity class CU IDOL SELF LEARNING MATERIAL (SLM)

Code to display the score of the person. File: mainactivity.Java Import android.Os.Package; Import android.View.View; Import android.Widget.Button; Import android.Widget.RatingBar; Import android.Widget.Toast; Public class mainactivity extends appcompatactivity { RatingBar RatingBar; Button button; @override protected void oncreate(package deal savedinstancestate) super.Oncreate(savedinstancestate); setcontentview(r.layout.Activity_main); addlisteneronbuttonclick(); public void addlisteneronbuttonclick() RatingBar=(RatingBar)findviewbyid(r.Identity.RatingBar); button=(button)findviewbyid(r.Id.Button); //performing movement on button click on button.Setonclicklistener(new view.Onclicklistener() @override public void onclick(view arg0) //getting the score and showing it on the toast string rating=string.Valueof(RatingBar.GetRating()); toast.Maketext(getapplicationcontext(), rating, toast.Length_long).Show(); ); } } 111 CU IDOL SELF LEARNING MATERIAL (SLM)

Figure 5.3. Rating Bar Output 5.5 DATE & TIME PICKER Android Datepicker is a widget to pick dates. It permits you to choose dates with the aid of day, month, and yr. Like Datepicker , android also affords timepicker to pick time. The android. Widget.Datepicker is the subclass of framelayout class. Android datepicker instance Let's examine the easy instance of datepicker widget in android. Activity_main.Xml Document: activity_main.Xml <?Xml version=\"1.0\" encoding=\"utf-8\"?> <RelativeLayout xmlns:android=\"http://schemas.Android.Com/apk/res/android\" xmlns:app=\"http://schemas.Android.Com/apk/res-auto\" xmlns:tools=\"http://schemas.Android.Com/tools\" 112 CU IDOL SELF LEARNING MATERIAL (SLM)

android:layout_width=\"match_parent\" 113 android:layout_height=\"match_parent\" tools:context=\"example.Jst.Com.Datepicker.MainActivity\"> <TextView android:id=\"@+id/textView1\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:layout_above=\"@+id/button1\" android:layout_alignParentLeft=\"true\" android:layout_alignParentStart=\"true\" android:layout_marginBottom=\"102dp\" android:layout_marginLeft=\"30dp\" android:layout_marginStart=\"30dp\" android:text=\"\" /> <Button android:id=\"@+id/button1\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:layout_alignParentBottom=\"true\" android:layout_centerHorizontal=\"true\" android:layout_marginBottom=\"20dp\" android:text=\"Change Date\" /> <DatePicker android:id=\"@+id/datePicker\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:layout_above=\"@+id/textView1\" CU IDOL SELF LEARNING MATERIAL (SLM)

android:layout_centerHorizontal=\"true\" 114 android:layout_marginBottom=\"36dp\" /> </RelativeLayout> ________________________________________ Activity class File: mainactivity.Java Import android.Os.Package; Import android.View.View; Import android.Widget.Button; Import android.Widget.Datepicker; Import android.Widget.Textview; Public class mainactivity extends appcompatactivity Datepicker picker; Button dispdate; Textview textview1; @override included void oncreate(package savedinstancestate) exquisite.Oncreate(savedinstancestate); setcontentview(r.Layout.Activity_main); textview1=(textview)findviewbyid(r.Identity.Textview1); picker=(datepicker)findviewbyid(r.Id.Datepicker); dispdate=(button)findviewbyid(r.Identity.Button1); textview1.Settext(\"current date: \"+getcurrentdate()); dispdate.Setonclicklistener(new view.Onclicklistener() CU IDOL SELF LEARNING MATERIAL (SLM)

@override public void onclick(view view) textview1.Settext(\"change date: \"+getcurrentdate()); ); public string getcurrentdate() stringbuilder build=new stringbuilder();; build.Append((picker.Getmonth() + 1)+\"/\"); build.Append(picker.Getdayofmonth()+\"/\"); build.Append(picker.Getyear()); gobackbuild.Tostring(); } } Figure 5.4. Date Picker Output 115 CU IDOL SELF LEARNING MATERIAL (SLM)

Android timepicker widget is used to pick a date. It allows you to choose time by means of an hour and a minute. You can't pick time by means of seconds. The android. Widget.Timepicker is the subclass of framelayout class. Android timepicker example Instance of an android time picker. Activity_main.Xml Document: activity_main.Xml <?Xml version=\"1.0\" encoding=\"utf-8\"?> <RelativeLayout xmlns:android=\"http://schemas.Android.Com/apk/res/android\" xmlns:app=\"http://schemas.Android.Com/apk/res-auto\" xmlns:tools=\"http://schemas.Android.Com/tools\" android:layout_width=\"match_parent\" android:layout_height=\"match_parent\" tools:context=\"example.Jst.Com.Timepicker.MainActivity\"> <TextView android:id=\"@+id/textView1\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:layout_above=\"@+id/button1\" android:layout_alignParentLeft=\"true\" android:layout_alignParentStart=\"true\" android:layout_marginBottom=\"102dp\" android:layout_marginLeft=\"30dp\" android:layout_marginStart=\"30dp\" android:text=\"\" /> <Button android:id=\"@+id/button1\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:layout_alignParentBottom=\"true\" android:layout_centerHorizontal=\"true\" android:layout_marginBottom=\"20dp\" android:text=\"Change Time\" /> <TimePicker 116 CU IDOL SELF LEARNING MATERIAL (SLM)

android:id=\"@+id/timePicker\" 117 android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:layout_above=\"@+id/textView1\" android:layout_centerHorizontal=\"true\" android:layout_marginBottom=\"36dp\" /> </RelativeLayout> Activity class File: MainActivity.java Import android.View.View; Import android.Widget.Button; Import android.Widget.Textview; Import android.Widget.Timepicker; Public class mainactivity extends appcompatactivity { Textview textview1; Timepicker timepicker; Button changetime; @override protected void oncreate(package savedinstancestate) super.Oncreate(savedinstancestate); setcontentview(r.Layout.Activity_main); textview1=(textview)findviewbyid(r.Identification.Textview1); timepicker=(timepicker)findviewbyid(r.Id.Timepicker); //uncomment the below line of code for 24 hour view timepicker.Set.hourview(proper); changetime=(button)findviewbyid(r.Id.Button1); textview1.Settext(getcurrenttime()); changetime.Set.onclicklistener(new view.Onclicklistener() @override public void onclick(view view) textview1.Settext(getcurrenttime()); ); CU IDOL SELF LEARNING MATERIAL (SLM)

public string getcurrenttime() { string currenttime=\"present day time: \"+timepicker.Getcurrenthour()+\":\"+timepicker.Getcurrentminute(); go back currenttime; }}; Output: Figure 5.5. TimePicker Output 5.6 SUMMARY • The view is BaseClass. Lots of UI Controller/Widgets provided by Android, like Button, TextView, EditText, Checkbox, DatePicker, TimePicker, ProgressBar, etc. These all allow you to build GUI for Android App., to display a button on the screen you have to add it on the layout XML file of Activity. • A user interface (UI) control is a visual element on a computer screen that helps humans to interact with the underlying software. (They are also sometimes referred to as “widgets.”) Examples of UI controls are buttons, checkboxes, links, tab strips, 118 CU IDOL SELF LEARNING MATERIAL (SLM)

charts, and windows, but many more we probably use daily, but never gave them a second thought. • In addition to being visual elements, UI controls are technical building blocks with predefined attributes, methods, and values that establish a consistent visualization and interaction. They are made available in a so-called “control library” from which application developers choose the controls they need and then, if necessary, select the preconfigured attributes. For example, a developer might choose to create a form with a check already in a checkbox, instead of an empty box (which is the default). This might be to encourage the user to select that particular option. However, as the checkbox is enabled, the user can still uncheck the box if he or she wants to. “Checked” and “unchecked” are examples of attributes. 5.7 KEYWORDS • Android Toast-Displays information for the short duration of time. • Custom Toast-We are able to customize the toast, such as we can display image on the toast • ToggleButton-It has two states ON/OFF. • CheckBox- Let's see the application of simple food ordering. • AlertDialog-AlertDialog displays an alert dialog containing the message with OK and Cancel buttons. • Spinner-Spinner displays the multiple options, but only one can be selected at a time. • AutoCompleteTextView-Let's see the simple example of AutoCompleteTextView. • RatingBar- RatingBar displays the rating bar. 5.8LEARNING ACTIVITY 1. Create date time picker as 119 CU IDOL SELF LEARNING MATERIAL (SLM)

___________________________________________________________________________ ____________________________________________________________________ 5.9UNIT END QUESTIONS A. Descriptive Questions Short Questions 1. What is Toast? 2. Explain Spinner. 3. What is UI? 4. What is the use of Buttons? 5. Explain the Benefits of CheckBox. Long Questions 1. What’s your understanding of UI design? 2. How do you get to know UI design? 3. How to use the auto-complete text view 4. What is the use of Alert Dialog Box? 5. How Date & Time Picker can be used in different ways? B. Multiple Choice Questions 1. What is an Android platform? a. Android is a stack of the mobility solutions of any software b. It is a Google mobile device name c. It is a virtual machine d. None of these 2. What does Manifest.xml mean in the Android platform? a. It has the information about Android application layout b. It has information about the Android application activities c. It has the entire information about an Android application d. None of these 120 CU IDOL SELF LEARNING MATERIAL (SLM)

3. What does broadcast receiver refer to in an Android platform? a. It will react to the broadcast announcements b. It performs background functionalities as a service c. It can pass the data between the Android activities d. None of these 4. In an Android application, what is sleep mode? a. Switched off b. Airplane mode c. Only the radio interface layer and the alarm mode are active d. None of these 5. Which of the following Dialog boxes are supported by Android? 1) AlertDialog 2) ProgressDialog 3) DatePickerDialog 4) TimePickerDialog a. 1 and 2 b. 3 and 4 c. all of the above d. none of these Answers 1-a, 2-c, 3-a,[ 4-c,5-c 5.10 REFERENCES Reference Books ● Khoshafian, et al. (1990). Object orientation: Concepts, Languages, Databases. New York: John Wiley & Sons ● Thornsby, J. (May 2016). Android UI Design. Packt Publishing. Morris, J. (Feb 2011). Android User Interface Development - Beginner’s Guide. UK. Packt Websites • https://enos.itcollege.ee/~jpoial/allalaadimised/reading/Android-UI-Design.pdf 121 CU IDOL SELF LEARNING MATERIAL (SLM)

• https://www.techotopia.com/index.php/Understanding_Android_Views,_View_Gr oups_and_Layouts • https://www.tutorialspoint.com/software_architecture_design/user_interface.htm • https://www.wideskills.com/android/building-user-interface/listening-to-ui- notifications-in-android • https://developer.android.com/guide/fragments/lifecycle 122 CU IDOL SELF LEARNING MATERIAL (SLM)

UNIT – 6 : LAYOUT MANAGER, ADAPTOR Structure 6.0. Learning Objectives 6.1. Introduction 6.2. Layout Manager 6.3. Adaptor 6.3.1.Array Adaptor 6.3.2.Array List Adaptor 6.3.3.Base Adaptor Activities 6.4. Summary 6.5. Keywords 6.6.Learning Activity 6.7. Unit End Questions 6.8.References 6.0 LEARNING OBJECTIVES After studying this unit, you will be able to • Learn about implementation of Layout Manager • Implement Adaptors • Illustrate use of Array Adaptor • Implement use of Array List Adaptor • Evaluate use of Base Adaptor Activities 6.1 INTRODUCTION The layout means the arrangement of components within the container. In another way, we can say that placing the components at a particular position within the container. The task of layout the controls is done automatically by the Layout Manager. Adapters are abstract classes for receiving various events. The methods in these classes are empty. These classes exist as a convenience for creating listener objects. 6.2 LAYOUT MANAGER The layout is a basic building block of an Application, It is a visual structure for UI, It is associate with activity, fragments, dialogs, etc. 123 CU IDOL SELF LEARNING MATERIAL (SLM)

Declare UI elements in XML: Android provides XML vocabulary, you can quickly design UI layouts with the help of XML, in the same way, you create web pages in HTML. Let's create an XML layout that uses a vertical LinearLayout <?xml version=\"1.0\" encoding=\"utf-8\"?> <LinearLayoutxmlns:android=\"http://schemas.android.com/apk/res/android\" android:layout_width=\"match_parent\" android:layout_height=\"match_parent\" android:orientation=\"vertical\" > <TextViewandroid:id=\"@+id/tv\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:text=\"Hello, I am a TextView\" /> <Button android:id=\"@+id/btn\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:text=\"Hello, I am a Button\" /> </LinearLayout> After declare layout in XML, Lets know the way from which you can associate this xml layout with your activity: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); } The onCreate() callback method is called when your activity is launched. Types of Layouts 1.Linear Layout: This layout aligns all children in a single direction, vertically or horizontally. 2.Relative Layout: A relative Layout is a view group that displays a child's views in a relative position. 3.Table Layout: It displays its child views in rows and columns. 124 CU IDOL SELF LEARNING MATERIAL (SLM)

4.Absolute Layout: It gives you a feature to specify the exact location of its children. 5.Frame Layout: It is a placeholder on the screen for using display a single view. 6.ListView: ListView is a view group that displays a list of scrollable items. The list items are auto inserted into a list using an adapter. 7.GridView: GridView is a ViewGroup that displays a large number of items in a two- dimensional, scrollable grid. 8.RecycleView : An advance version of ListView is RecycleView. Just like ListView, it is used to display a large number of similar items on the screen. Attributes Attributes define visual properties of layout and each layout has a set of attributes. Some of the common attributes are listed below which are used in all types of Layouts: 1. ID: It uniquely identifies the View, Any View may have an integer Id which associates with it. Syntax of id is: android:id=\"@+id/my_button\" 2. Width: This is the width of the layout, the syntax for using this attribute is android: layout_width=”match_parent” 3.Height: This is the height of layout, the syntax for using this attribute is : android:layout_height=”match_parent” 4.Margin Top: It is used to give extra space from topside, the syntax for using this attribute is: android:layout_marginTop=”5dp” 5.Margin Bottom: It is used to give extra space from the bottom side, the syntax for using this attribute is: android:layout_marginBottom=”5dp” 6..Margin Left: It is used to give extra space from the left side, the syntax for using this attribute is: android:layout_marginLeft=”5dp” 7.Margin Right: It is used to give extra space from the right side, the syntax for using this attribute is: android:layout_marginRight=”5dp” 8.Padding Left: It is used to give padding from the left side for the layout, the syntax for using these attributes is: android:paddingLeft=”5dp” 9..Padding Right: It is used to give padding from the right side for the layout, syntax for using these attributes is: android:paddingRight=”5dp” 10.Padding Top: It is used to give padding from topside for the layout, the syntax for using these attributes is: android:paddingTop=”5dp” 11.Padding Bottom: It is used to give padding from the bottom side for the layout, the syntax for using these attributes is: android:paddingBottom=”5dp” 125 CU IDOL SELF LEARNING MATERIAL (SLM)

6.3 ADAPTOR 6.3.1 Array Adaptor In android, An adapter is a bridge between the UI component and data source that helps us to fill data in the UI component. It holds the data and sends the data to the adapter view then the view can take the data from the adapter view and shows the data on different views like a listview, gridview, spinner. • use ArrayAdapter, Whenever a list of a single item is backed by an array. • ArrayAdapter expects a Layout with a single TextView by default, please avoid ArrayAdapter and use custom adapters. code of ArrayAdapter in Android: ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects) In the above code snippet is the implementation of an ArrayAdapter. Let's discuss parameter in ArrayAdapter class: context: The first parameter is used to pass the context means the reference of the current class. getApplicationContext(), getActivity() can use in the place of this keyword. Below is the example code in which we set the current class reference in an adapter. ArrayAdapter arrayAdapter = new ArrayAdapter(this, int resource, int textViewResourceId, T[] objects); resource: The second parameter is resource id for list items in which you have a text view that can be used to set the layout(XML file) . setting the layout. ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.list_view_items, int textViewResourceId, T[] objects); textViewResourceId: The third parameter is textViewResourceId which is used to set the id of TextView where you want to display the actual text. example code: set the id(identity) of a text view. ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.list_view_items, R.id.textView, T[] objects); objects: 126 CU IDOL SELF LEARNING MATERIAL (SLM)

The fourth parameter is an array of objects, used to set the array of elements in the textView. set the object of an array. 6.3.2 Array List Adaptor Below is the example code in which we set the Animal array in the adapter to display the Animal name’s list. String animalList[] = {\"Lion\",\"Tiger\",\"Dog\",\"Monkey\",\"Elephant\",\"Camel\", \"Cat\"}; ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.list_view_items, R.id.textView, animalList); Example of an ArrayAdapter: Example 1: Below is the example, in which we display a list of animal names in a list view using a simple array adapter. Example of an ArrayAdapter: Example 1: Below is the example, in which we displays a list of animal names in a list view using simple array adapter. Below is the final output and code: Figure 6.1. Example of an ArrayAdapter Step 1: Create a new project and name it ArrayAdapterExample. Open Android Studio -> Select File -> New -> New Project. Fill and click \"Finish\" button. Step 2: Now open app -> res -> layout -> xml (or) activity_main.xml and add following code : 127 CU IDOL SELF LEARNING MATERIAL (SLM)

<RelativeLayoutxmlns:android=\"http://schemas.android.com/apk/res/android\" xmlns:tools=\"http://schemas.android.com/tools\" android:layout_width=\"match_parent\" android:layout_height=\"match_parent\" tools:context=\".MainActivity\"> <ListView android:id=\"@+id/simpleListView\" android:layout_width=\"match_parent\" android:layout_height=\"wrap_content\" android:divider=\"#000\" android:dividerHeight=\"2dp\"/> </RelativeLayout> Step 3: Create a new Activity activity_list_view.xml and add the below code <?xml version=\"1.0\" encoding=\"utf-8\"?> <LinearLayoutxmlns:android=\"http://schemas.android.com/apk/res/android\" android:layout_width=\"match_parent\" android:layout_height=\"match_parent\" android:orientation=\"vertical\"> <TextView android:id=\"@+id/textView\" android:layout_width=\"fill_parent\" android:layout_height=\"wrap_content\" android:layout_gravity=\"center\" android:padding=\"@dimen/activity_horizontal_margin\" android:textColor=\"#000\" /> </LinearLayout> Step 4: Now Open app -> java -> package -> MainActivity.java and add the below code. Here we will use ArrayAdapter to display the items in Listview. 128 CU IDOL SELF LEARNING MATERIAL (SLM)

package example.abhiandriod.arrayadapterexample; //use package of your project import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends AppCompatActivity { // Array of strings... ListViewsimpleList; String animalList[] = {\"Lion\",\"Tiger\",\"Dog\",\"Monkey\",\"Elephant\",\"Camel\", \"Cat\"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); simpleList = (ListView) findViewById(R.id.simpleListView); ArrayAdapter<String>arrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_list_view, R.id.textView, animalList); simpleList.setAdapter(arrayAdapter); } } } Now run the App in Emulator and you will see the below output:: 129 CU IDOL SELF LEARNING MATERIAL (SLM)

Figure 6.2 Output of an ArrayAdapter 6.3.3 Base Adaptor • It is a common base class of implementation of an Adapter that can be used in ListView, GridView, Spinner, etc. • Whenever you need a customized list in a ListView or customized grids in a GridView you create your own adapter and extend the base adapter in that. • Base Adapter can be extended to create a custom Adapter for list items. Note: ArrayAdapter an implementation of BaseAdapter. code of Custom Adapter when we extend the BaseAdapter : public class CustomAdapter extends BaseAdapter { @Override public int getCount() { return 0; } @Override 130 public Object getItem(int i) { return null; } CU IDOL SELF LEARNING MATERIAL (SLM)

@Override public long getItemId(int i) { return 0; } @Override public View getView(int i, View view, ViewGroupviewGroup) { return null; } In the above code snippet, we see the overridden methods of BaseAdapter used to set the data. From there we mainly used two functions getCount() and getView(). Let’s discuss all these functions in detail: 1. getCount(): The getCount() function returns the total number of items to be displayed in a list and counts the value from the array list size() method . @Override public int getCount() { int count=arrayList.size(); return count;//returns the total count to adapter } 2. getView(int i, View view, ViewGroupviewGroup): This function is automatically called when the list item view is ready to be displayed or about to be displayed. we set the layout for list items using LayoutInflater class add the data to the views like ImageView, TextView, etc. getView function’s example code @Override public View getView(int i, View view, ViewGroupviewGroup) { view = inflter.inflate(R.layout.activity_gridview, null);//set layout for displaying items 131 CU IDOL SELF LEARNING MATERIAL (SLM)

ImageView icon = (ImageView) view.findViewById(R.id.icon);//get id for image view icon.setImageResource(flags[i]);//set image of the item’s return view; } 3. getItem(int i): This function is used to Get the data item associated with the specified to obtain the corresponding data of the specific location in the collection of data items position in the data set. code in which we return the array list’s item according to the position. @Override public Object getItem(int i) { return arrayList.get(i); } 4. getItemId(int i): As for the getItemId (int position), it returns the corresponding to the position item ID. The methodreturns a long value of item position to the adapter. Below is the code @Override public long getItemId(int i) { return i; } BaseAdapter Example In Android Studio: Example 1: Example 1: BaseAdapter for displaying Animal images in grids. display animal images in the form of grids using a custom adapter.: 132 CU IDOL SELF LEARNING MATERIAL (SLM)

Figure 6.3 Example BaseAdapter Step 1: Create a new project and name it BaseAdapterExample. Step 2: open res -> layout -> activity_main.xml <?xml version=\"1.0\" encoding=\"utf-8\"?> <LinearLayoutxmlns:android=\"http://schemas.android.com/apk/res/android\" android:layout_width=\"match_parent\" android:layout_height=\"match_parent\" android:orientation=\"vertical\"> <GridView android:id=\"@+id/simpleGridView\" android:layout_width=\"fill_parent\" android:layout_height=\"wrap_content\" android:oofterDividersEnabled=\"false\" android:numColumns=\"3\" /> </LinearLayout> Step 3: Create a new Activity activity_gridview.xml inside layout and add the below code: <?xml version=\"1.0\" encoding=\"utf-8\"?> <LinearLayoutxmlns:android=\"http://schemas.android.com/apk/res/android\" 133 CU IDOL SELF LEARNING MATERIAL (SLM)

android:layout_width=\"match_parent\" android:layout_height=\"match_parent\" android:orientation=\"vertical\"> <ImageView android:id=\"@+id/icon\" android:layout_width=\"100dp\" android:layout_height=\"100dp\" android:scaleType=\"fitXY\" android:layout_margin=\"5dp\" android:layout_gravity=\"center_horizontal\" /> </LinearLayout> Step 3: Now open app -> java -> package -> MainActivity.java and add the below code. import android.app.Activity; import android.os.Bundle; import android.widget.GridView; public class MainActivity extends Activity { GridViewsimpleGrid; int animals[] = {R.drawable.animal13, R.drawable.animal14, R.drawable.animal15, R.drawable.animal16, R.drawable.animal17, R.drawable.animal18, R.drawable.animal15, R.drawable.animal16, R.drawable.animal17}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); simpleGrid = (GridView) findViewById(R.id.simpleGridView); CustomAdaptercustomAdapter = new CustomAdapter(getApplicationContext(), animals); 134 CU IDOL SELF LEARNING MATERIAL (SLM)

simpleGrid.setAdapter(customAdapter); } } Step 4: Create a new class CustomAdapter.java inside the package and add the following code import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; public class CustomAdapter extends BaseAdapter { Context context; int animals[]; LayoutInflaterinflter; public CustomAdapter(Context applicationContext, int[] animals) { this.context = applicationContext; this.animals = animals; inflter = (LayoutInflater.from(applicationContext)); } @Override public int getCount() { return animals.length; } @Override 135 public Object getItem(int i) { CU IDOL SELF LEARNING MATERIAL (SLM)

return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(int i, View view, ViewGroupviewGroup) { view = inflter.inflate(R.layout.activity_gridview, null); ImageView icon = (ImageView) view.findViewById(R.id.icon); icon.setImageResource(animals[i]); return view; } } Now run the App. Figure 6.4 Output BaseAdapter 136 CU IDOL SELF LEARNING MATERIAL (SLM)

6.4 SUMMARY • A layout manager is liable for measuring and positioning object perspectives inside a recycler view as well as figuring out the coverage for whilst to recycle item views which can be now not visible to the person. With the aid of converting the layout manager a recycler view can be used to put into effect a preferred vertically scrolling list, a uniform grid, staggered grids, horizontally scrolling collections, and more. Numerous stock format managers are furnished for preferred use. • If the layout manager specifies a default constructor or one with the signature (context, attribute set, int, int), the recycler view will instantiate and set the layout manager when being inflated. Maximum used houses can be then obtained from getproperties(context, attribute set, int, int). In case a layout manager specifies each constructor, the non- default constructor will take precedence. • a handful of the greater present-day format managers provide methods to override the scale set via the thing. Take a look at whether or not the layout manager you are the usage of permits you to specify component sizes. • make sure which you really want to set the element's specific size. Each swing thing has a distinct favoured length, depending on the font it uses and the appearance and sense. Because of this, it frequently does now not make sense to specify a swing component's precise length. • if the issue isn't always controlled with the aid of a format supervisor, you could set its size by invoking the set size or set bounds to approach it. In any other case, you want to provide size pointers and then make sure you're the use of a format manager that respects the size recommendations. • adapter pattern helps you to use an existing class to satisfy a patron class's desires. While a purchaser specifies its necessities in an interface, you could typically create a new magnificence that implements the interface and subclasses and present class. 6.5 KEYWORDS • android:baselineAligned-When set to false, prevents the layout from aligning its children's baselines. • android:baselineAlignedChildIndex-When a linear layout is part of another layout that is baseline aligned, it can specify which of its children to baseline align to (that is, which child TextView). • android:divider -rawable to use as a vertical divider between buttons. • android:gravity -Specifies how an object should position its content, on both the X and Y axes, within its own bounds. • android:measureWithLargestChild -When set to true, all children with a weight will be considered having the minimum size of the largest child. 137 CU IDOL SELF LEARNING MATERIAL (SLM)

• android:orientation -Should the layout be a column or a row? Use \"horizontal\" for a row, \"vertical\" for a column. • android:weightSum -Defines the maximum weight sum. 6.6 LEARNING ACTIVITY 1. What is the difference between Linear Layout and Relative Layout? Explain. ___________________________________________________________________________ ____________________________________________________________________ 2. Describe the role of AdapterView class in Spinner view. ___________________________________________________________________________ ____________________________________________________________________ 3. Write the XML code for the following layout in portrait view using Relative Layout: Figure 6.5 Relative Layout ___________________________________________________________________________ ____________________________________________________________________ 6.7 UNIT END QUESTIONS A. Descriptive Questions 138 Short Questions 1. What is Layout? 2. What is the role of Layout in the presentation? 3. What is Linear Layout? CU IDOL SELF LEARNING MATERIAL (SLM)

4. Explain Relative Layout? 139 5. What is an Adapter? Long Questions 1. What is an adapter class? 2. What is the benefit of using layout managers? 3. What are the different types of layout managers? 4. What is the process is used for changing the layout manager? 5. Explain with example different types of the adapter. B. Multiple Choice Questions 1. What is the default layout in android application? a. RelativeLayout b. TableLayout c. LinearLayout d. FrameLayout 2. To display text which control you will use? a. EditText b. TextView c. label d. None of these 3. What layout manager do Panels use by default? a. GridLayout b. FlowLayout c. no default layout manager d. the same layout manager as the Frame 4. How many GUI components can go into each cell with GridLayout? a. None b. 1 CU IDOL SELF LEARNING MATERIAL (SLM)

c. Only one Panel per cell. d. As many as you need. 5. What is a base adapter in android? a. Base Adapter is a common class for any adapter, which can we use for both ListView and spinner b. A kind of adapter c. Data storage space d. None of these Answers 1-a, 2-b, 3-b, 4-b,5-a 6.8 REFERENCES Reference Books ● Khoshafian, et al. (1990). Object orientation: Concepts, Languages, Databases. New York: John Wiley & Sons ● Thornsby, J. (May 2016). Android UI Design. Packt Publishing. Morris, J. (Feb 2011). Android User Interface Development - Beginner’s Guide. UK. Packt Publishing Websites • https://enos.itcollege.ee/~jpoial/allalaadimised/reading/Android-UI-Design.pdf • https://www.techotopia.com/index.php/Understanding_Android_Views,_View_Gr oups_and_Layouts • https://www.tutorialspoint.com/software_architecture_design/user_interface.htm • https://www.wideskills.com/android/building-user-interface/listening-to-ui- notifications-in-android • https://developer.android.com/guide/fragments/lifecycle 140 CU IDOL SELF LEARNING MATERIAL (SLM)

UNIT - 7: ANDROID MENU Structure 7..0 Learning Objectives 7..1 Introduction 7..2 Option Menu 7..3 Context Menu 7..4 Popup Menu 7..5 Tabbed Menu 7..6 Summary 7..7 Keyword 7..8 Learning Activity 7..9 Unit End Questions 7..10 References 7.0 LEARNING OBJECTIVES After studying this unit, you will be able to: • Define Menu and there functionality • Implement Menu • Implement Content menu • Describe use and working of popup Menu • Create Tabbed Menu 7.1 INTRODUCTION The menu is a part of the user interface (UI) component which is used to handle some common functionality around the application. Menus in our applications can provide a better and consistent user experience. We can use Menu APIs to represent user actions and other options in our application activities. Pictorial representation of using menus in the android application. 141 CU IDOL SELF LEARNING MATERIAL (SLM)

Figure 7.1 representation of using menus In android, we will define a menu during a separate XML document and use that enter our activities or fragments based on our necessities. Define an android menu in an XML document For all menu sorts, android presents a preferred XML format to stipulate menu items. As against constructing a menu in our hobby's code, we should always outline a menu and everyone its objects in an XML menu aid and cargo the menu useful resource as a menu item in our pastime or fragment. In android, to stipulate the menu, we would like to make a fresh folder menu inner of our undertaking aid directory (res/menu/) and upload a replacement XML record to create the menu with the next factors. Example of defining a menu in an XML file (menu_example.Xml). <?xml version=\"1.0\" encoding=\"utf-8\"?> 142 CU IDOL SELF LEARNING MATERIAL (SLM)

The element within the menu supports differing types of attributes to define the item’s behaviour and appearance. Following are the number of commonly used attributes in android applications. In case if we would like to feature a submenu within the menu item, then we'd like to feature an element because the child of an . Example of defining a submenu within the menu item. <?xml version=\"1.0\" encoding=\"utf-8\"?> <!-- \"file\" submenu --> Load Android Menu from an Activity we need to load the menu resource from our activity using MenuInflater.inflate() like as shown below. @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfomenuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_example, menu); } the above code we are calling our menu using MenuInflater.inflate() method within the sort of R.menu.menu_file_name. Here our XML file name is menu_example.xml so we used file name menu_example. Handle Android Menu Click Events In android, we will handle menu item click events using ItemSelected() event supported the menu type. Example of handling a context menu item click event using onContextItemSelected(). @Override 143 public boolean onContextItemSelected(MenuItem item) { CU IDOL SELF LEARNING MATERIAL (SLM)

switch (item.getItemId()) { case R.id.mail: // do something return true; case R.id.share: // do something return true; default: return super.onContextItemSelected(item); } } the above code, the getItemId() method will get the id of the chosen menu item supported that we will perform our actions. Element Description <menu> It’s a root element to define a Menu in XML file and it will hold one or more and elements. <item> It is used to create a menu item and it represents a single item on the menu. This element may contain a nested <menu> element in order to create a submenu. <group> It’s an optional and invisible for <item> elements. It is used to categorize the menu items so they share properties such as active state and visibility. In android, we will define a menu during a separate XML document and use that enter our activities or fragments based on our necessities. Define an android menu in an XML document For all menu sorts, android presents a preferred XML format to stipulate menu items. As against constructing a menu in our hobby's code, we should always outline a menu and everyone its objects in an XML menu aid and cargo the menu useful resource as a menu item in our pastime or fragment. 144 CU IDOL SELF LEARNING MATERIAL (SLM)

In android, to stipulate the menu, we would like to make a fresh folder menu inner of our undertaking aid directory (res/menu/) and upload a replacement XML record to create the menu with the next factors. Example of defining a menu in an XML file (menu_example.Xml). <?xml version=\"1.0\" encoding=\"utf-8\"?> The element within the menu supports differing types of attributes to define the item’s behaviour and appearance. Following are the a number of commonly used attributes in android applications. In case if we would like to feature a submenu within the menu item, then we'd like to feature an element because the child of an . Example of defining a submenu within the menu item. <?xml version=\"1.0\" encoding=\"utf-8\"?> <!-- \"file\" submenu --> Load Android Menu from an Activity we need to load the menu resource from our activity using MenuInflater.inflate() like as shown below. @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfomenuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_example, menu); } the above code we are calling our menu using MenuInflater.inflate() method within the sort of R.menu.menu_file_name. Here our XML file name is menu_example.xml so we used file name menu_example. Handle Android Menu Click Events In android, we will handle menu item click events using ItemSelected() event supported the menu type. Example of handling a context menu item click event using onContextItemSelected(). @Override 145 CU IDOL SELF LEARNING MATERIAL (SLM)

public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.mail: // do something return true; case R.id.share: // do something return true; default: return super.onContextItemSelected(item); } } the above code, the getItemId() method will get the id of the chosen menu item supported that we will perform our actions. 7.2 OPTION MENU Options Menu may be a primary collection of menu items for an activity and it's useful to implement actions that have a worldwide impact on the app, like Settings, Search, etc.. Figure 7.2 representation of Options menus 146 CU IDOL SELF LEARNING MATERIAL (SLM)

By using the choices Menu, we will combine multiple actions and other options that are relevant to our current activity. we will define items for the choices menu from either our Activity or Fragment class. In case, if we define items for the choices menu in both activity or fragment, then those items are going to be combined and display in UI. Create Android Options Menu in XML File In android, to define the choices menu, we'd like to make a replacement folder menu inside our project resource directory (res/menu/) and add a replacement XML (menu_example) file to create the menu. Example of defining a menu in an XML file (menu_example.xml). <?xml version=\"1.0\" encoding=\"utf-8\"?> Load Android Options Menu from an Activity To specify the choices menu for an activity, we'd like to override onCreateOptionsMenu() method and cargo the defined menu resource using MenuInflater.inflate() like as shown below. @Override public void onCreateOptionsMenu(ContextMenu menu, View v, ContextMenuInfomenuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_example, menu); } If you observe above code we are calling our menu using MenuInflater.inflate() method within the sort of R.menu.menu_file_name. Here our xml file name is menu_example.xml so we used file name menu_example. Handle Android Options Menu Click Events In android, we will handle options menu item click events using the onOptionsItemSelected() event method. Example of handling a options menu item click event using onOptionsItemSelected(). @Override 147 public boolean onOptionsItemSelected(MenuItem item) { CU IDOL SELF LEARNING MATERIAL (SLM)

switch (item.getItemId()) { case R.id.mail: // do something return true; case R.id.share: // do something return true; default: return super.onContextItemSelected(item); } } Android Options Menu Attributes Following are the a number of commonly used attributes associated with options menu control in android applications. Attribute Description android:id It is used to uniquely identify an element in application. android:icon It is used to set the item's icon from the drawable folder. android:title It is used to set the item's title android:showAsAction It is used to specify how the item should appear as an action item in the app bar. Android Options Menu Example Illustration of executing an Options Menu in the android application. Make another android application utilizing android studio and give names as OptionsMenu. In android, to characterize the choices menu, we need to make another envelope menu within our task asset index (res/menu/) and add another XML (options_menu.xml) document to assemble the menu. Presently open recently made XML (options_menu.xml) record and compose the code like as demonstrated beneath. 148 CU IDOL SELF LEARNING MATERIAL (SLM)

options_menu.xml <?xml version=\"1.0\" encoding=\"utf-8\"?> <menu xmlns:android=\"http://schemas.android.com/apk/res/android\" > <item android:id=\"@+id/search_item\" android:title=\"Search\"/> <item android:id=\"@+id/upload_item\" android:title=\"Upload\"/> <item android:id=\"@+id/copy_item\" android:title=\"Copy\"/> <item android:id=\"@+id/print_item\" android:title=\"Print\"/> <item android:id=\"@+id/share_item\" android:title=\"Share\"/> <item android:id=\"@+id/bookmark_item\" android:title=\"BookMark\"/> </menu> Whenever we are finished with the formation of the menu, we need to stack this menu XML asset from our movement utilizing the onCreateOptionsMenu() callback strategy, for that open principle action record MainActivity.java MainActivity.java bundle com.tutlane.optionsmenu; 149 import android.support.v7.app.AppCompatActivity; import android. os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; public class MainActivity broadens AppCompatActivity { @Override CU IDOL SELF LEARNING MATERIAL (SLM)

ensured void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.options_menu, menu); bring valid back; } @Override public boolean onOptionsItemSelected(MenuItem thing) { Toast.makeText(this, \"Chose Item: \" +item.getTitle(), Toast.LENGTH_SHORT).show(); switch (item.getItemId()) { case R.id.search_item: /do your code bring valid back; case R.id.upload_item: /do your code bring valid back; case R.id.copy_item: /do your code bring valid back; case R.id.print_item: /do your code bring valid back; case R.id.share_item: /do your code bring valid back; case R.id.bookmark_item: 150 CU IDOL SELF LEARNING MATERIAL (SLM)


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook