Android Programs Day 5 //Android Program to demonstrate the working of Options Menu. 1. Create a New Project. 2. Write the necessary codes in the MainActivity.java to create OptionMenu. 3. Add the oncreateoptionsmenu() in mainactivity.java after oncreate(). public boolean oncreateoptionsmenu(menu menu) super.oncreateoptionsmenu(menu); CreateMenu(menu); return super.oncreateoptionsmenu(menu); 4. Add oncreatemenu(menu) user-defined function. menu.add(0, 0, 0, "Item 1"): Arguements: 1: Group ID 2: Item ID 3: Order ID 4: Item Names private void CreateMenu(Menu menu) MenuItem mnu1 = menu.add(0, 0, 0, "Item 1"); MenuItem mnu2 = menu.add(0, 1, 1, "Item 2"); 5. Add onoptionsitemselected(menuitem). Any operation on items, will call this function and handle the events. public boolean onoptionsitemselected(menuitem item) return MenuChoice(item); 6. Add on MenuChoice(MenuItem) user-defined function. private boolean MenuChoice(MenuItem item)
switch (item.getitemid()) case 0: //Code Goes Here case 1: //Code Goes Here return false; 7. Run the code and test the working of OptionMenu. //MainActivity.java: import android.content.intent; import android.net.uri; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.toast; public class MainActivity extends AppCompatActivity protected void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); public boolean oncreateoptionsmenu(menu menu) super.oncreateoptionsmenu(menu); createmenu(menu); return super.oncreateoptionsmenu(menu); private void createmenu(menu menu) MenuItem menu1 = menu.add(0, 0, 0, "View A Contact"); MenuItem menu2 = menu.add(0, 1, 1, "Edit A Contact"); MenuItem menu3 = menu.add(0, 2, 2, "Dial A Contact"); MenuItem menu4 = menu.add(0, 3, 3, "View All");
public boolean onoptionsitemselected(menuitem item) return menuchoice(item); private boolean menuchoice(menuitem item) switch(item.getitemid()) case 0: Intent obj = new Intent(Intent.ACTION_VIEW); obj.setdata(uri.parse("content://contacts/people/1")); startactivity(obj); case 1: Intent obj1 = new Intent(Intent.ACTION_EDIT); obj1.setdata(uri.parse("content://contacts/people/1")); startactivity(obj1); case 2: Intent obj2 = new Intent(Intent.ACTION_DIAL); obj2.setdata(uri.parse("tel:+9876543210")); startactivity(obj2); case 3: Intent obj3 = new Intent(Intent.ACTION_VIEW); obj3.setdata(uri.parse("content://contacts/people/")); startactivity(obj3); //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="com.example.ibmsap.test_optionmenu.mainactivity"> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="hello World" android:textappearance="@android:style/textappearance.large " tools:layout_constraintleft_creator="1" tools:layout_constrainttop_creator="1" app:layout_constraintleft_toleftof="parent" app:layout_constraintright_torightof="parent" tools:layout_constraintright_creator="1" android:layout_margintop="117dp" app:layout_constrainttop_totopof="parent" /> </android.support.constraint.constraintlayout>
//Android Program to demonstrate the working of Context Menu. 1. Create a New Project. 2. Add a Button in the UI. Write the necessary codes in the MainActivity.java to create ContextnMenu. 3. Add a Listener to the Button by writing the following code. Button btn = (Button)findViewById(R.id.button); btn.setoncreatecontextmenulistener(this); 4. Add the oncreatecontextmenu() in mainactivity.java after oncreate(). public void oncreatecontextmenu(contextmenu menu, View v,contextmenu.contextmenuinfo menuinfo) super.oncreatecontextmenu(menu, v, menuinfo); CreateMenu(menu); 5. Add on CreateMenu(menu) user-defined function. menu.add(0, 0, 0, "Item 1"): Arguements: 1: Group ID 2: Item ID 3: Order ID 4: Item Names private void CreateMenu(Menu menu) MenuItem mnu1 = menu.add(0, 0, 0, "Item 1"); MenuItem mnu2 = menu.add(0, 1, 1, "Item 2"); 6. Add oncontextitemselected(menuitem). Any operation on items, will call this function and handle the events. public boolean oncontextitemselected(menuitem item) return MenuChoice(item); 7. Add on MenuChoice(MenuItem) user-defined function.
private boolean MenuChoice(MenuItem item) switch (item.getitemid()) case 0: //Code Goes Here case 1: //Code Goes Here return false; 7. Run the code and test the working of ContextMenu. //MainActivity.java: import android.content.intent; import android.net.uri; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.contextmenu; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.button; import android.widget.toast; public class MainActivity extends AppCompatActivity Button btn; protected void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); btn = (Button)findViewById(R.id.button); btn.setoncreatecontextmenulistener(this); public void oncreatecontextmenu(contextmenu menu, View v, ContextMenu.ContextMenuInfo menuinfo) super.oncreatecontextmenu(menu, v, menuinfo); createmenu(menu);
private void createmenu(menu menu) MenuItem menu1 = menu.add(0, 0, 0, "View A Contact"); MenuItem menu2 = menu.add(0, 1, 1, "Edit A Contact"); MenuItem menu3 = menu.add(0, 2, 2, "Dial A Contact"); MenuItem menu4 = menu.add(0, 3, 3, "View All"); public boolean oncontextitemselected(menuitem item) return menuchoice(item); private boolean menuchoice(menuitem item) switch(item.getitemid()) case 0: Intent obj = new Intent(Intent.ACTION_VIEW); obj.setdata(uri.parse("content://contacts/people/1")); startactivity(obj); case 1: Intent obj1 = new Intent(Intent.ACTION_EDIT); obj1.setdata(uri.parse("content://contacts/people/1")); startactivity(obj1); case 2: Intent obj2 = new Intent(Intent.ACTION_DIAL); obj2.setdata(uri.parse("tel:+9876543210")); startactivity(obj2); case 3: Intent obj3 = new Intent(Intent.ACTION_VIEW); obj3.setdata(uri.parse("content://contacts/people/")); startactivity(obj3); //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="com.example.ibmsap.test_optionmenu.mainactivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="context Menu" tools:layout_constrainttop_creator="1" tools:layout_constraintright_creator="1" tools:layout_constraintbottom_creator="1" app:layout_constraintbottom_tobottomof="parent" app:layout_constraintright_torightof="parent" tools:layout_constraintleft_creator="1" app:layout_constraintleft_toleftof="parent" app:layout_constrainttop_totopof="parent" app:layout_constrainthorizontal_bias="0.417" app:layout_constraintvertical_bias="0.498" /> </android.support.constraint.constraintlayout>
//Android Program to demonstrate the Menu Creation using XML File. 1. Create a new project. 2. Create a Directory "menu" in /res folder. 3. Create a Menu resource file in /res/menu folder and name it as menu.xml 4. Add few <item> as buttons. //menu.xml: <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu1" android:title="menu 1" /> <item android:id="@+id/menu2" android:title="menu 2" /> <item android:id="@+id/menu3" android:title="menu 3" /> </menu> 5. Add oncreateoptionsmenu(menu) and use MenuInflater object to populate the menu isng menu.xml file. public boolean oncreateoptionsmenu(menu menu) MenuInflater menuinflater = getmenuinflater(); menuinflater.inflate(r.menu.menu, menu); return super.oncreateoptionsmenu(menu);
6. Add onoptionsitemselected(menuitem). Any operation on items, will call this function and handle the events. public boolean onoptionsitemselected(menuitem item) return MenuChoice(item); //MainActivity.java: import android.content.intent; import android.net.uri; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; public class MainActivity extends AppCompatActivity protected void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); public boolean oncreateoptionsmenu(menu menu) MenuInflater obj = getmenuinflater(); obj.inflate(r.menu.menu, menu); return super.oncreateoptionsmenu(menu); public boolean onoptionsitemselected(menuitem item) return menuchoice(item); private boolean menuchoice(menuitem item) switch(item.getitemid()) case R.id.Menu1: Intent obj = new Intent(Intent.ACTION_VIEW); obj.setdata(uri.parse("content://contacts/people/1")); startactivity(obj);
case R.id.Menu2: Intent obj1 = new Intent(Intent.ACTION_EDIT); obj1.setdata(uri.parse("content://contacts/people/1")); startactivity(obj1); case R.id.Menu3: Intent obj2 = new Intent(Intent.ACTION_DIAL); obj2.setdata(uri.parse("tel:+9876543210")); startactivity(obj2); case R.id.Menu4: Intent obj3 = new Intent(Intent.ACTION_VIEW); obj3.setdata(uri.parse("content://contacts/people/")); startactivity(obj3); //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="com.example.ibmsap.test_menuxml.mainactivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello World!" app:layout_constraintbottom_tobottomof="parent" app:layout_constraintleft_toleftof="parent" app:layout_constraintright_torightof="parent" app:layout_constrainttop_totopof="parent" /> </android.support.constraint.constraintlayout> //menu.xml: <?xml version="1.0" encoding="utf-8"?> <menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu1" android:title="view A Contact"/> <item android:id="@+id/menu2" android:title="edit A Contact"/> <item android:id="@+id/menu3" android:title="dial A Contact"/> <item android:id="@+id/menu4" android:title="view All Contacts"/> </menu>
//Android Program to demonstrate the Sub-Menu Creation using XML File. 1. Create a new project. 2. Create a Directory "menu" in /res folder. 3. Create a Menu resource file in /res/menu folder and name it as menu.xml 4. To add Sub menu follow these steps. i. Create an <item> tag in the menu.xml file. ii. Create a <menu> tag inside the <item> tag. This will act as Main menu 1. iii. Create few <item> tags in the <menu> tag. Give proper id and titles. These will act as Sub-menu 1. iv. Repeat the steps i to iv to create one more sub-menu. //menu.xml: <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/mainmenu1" android:title="main Menu 1"> <menu android:title="sub Menu1"> <item android:id="@+id/menu1" android:title="view A Contact"/> <item android:id="@+id/menu2" android:title="edit A Contact"/> </menu> </item> </menu>
5. Add oncreateoptionsmenu(menu) and use MenuInflater object to populate the menu isng menu.xml file. public boolean oncreateoptionsmenu(menu menu) MenuInflater menuinflater = getmenuinflater(); menuinflater.inflate(r.menu.menu, menu); return super.oncreateoptionsmenu(menu); 6. Add onoptionsitemselected(menuitem). Any operation on items, will call this function and handle the events. public boolean onoptionsitemselected(menuitem item) return MenuChoice(item); //MainActivity.java: import android.content.intent; import android.net.uri; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; public class MainActivity extends AppCompatActivity protected void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); public boolean oncreateoptionsmenu(menu menu) MenuInflater obj = getmenuinflater(); obj.inflate(r.menu.menu, menu); return super.oncreateoptionsmenu(menu); public boolean onoptionsitemselected(menuitem item) return menuchoice(item);
private boolean menuchoice(menuitem item) switch(item.getitemid()) case R.id.Menu1: Intent obj = new Intent(Intent.ACTION_VIEW); obj.setdata(uri.parse("content://contacts/people/1")); startactivity(obj); case R.id.Menu2: Intent obj1 = new Intent(Intent.ACTION_EDIT); obj1.setdata(uri.parse("content://contacts/people/1")); startactivity(obj1); case R.id.Menu3: Intent obj2 = new Intent(Intent.ACTION_DIAL); obj2.setdata(uri.parse("tel:+9876543210")); startactivity(obj2); case R.id.Menu4: Intent obj3 = new Intent(Intent.ACTION_VIEW); obj3.setdata(uri.parse("content://contacts/people/")); startactivity(obj3); //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="com.example.ibmsap.test_menuxml.mainactivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello World!" app:layout_constraintbottom_tobottomof="parent" app:layout_constraintleft_toleftof="parent" app:layout_constraintright_torightof="parent" app:layout_constrainttop_totopof="parent" /> </android.support.constraint.constraintlayout>
//menu.xml: <?xml version="1.0" encoding="utf-8"?> <menu xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/mainmenu1" android:title="main Menu 1"> <menu android:title="sub Menu1"> <item android:id="@+id/menu1" android:title="view A Contact"/> <item android:id="@+id/menu2" android:title="edit A Contact"/> </menu> </item> <item android:id="@+id/mainmenu2" android:title="main Menu 2"> <menu android:title="sub Menu2"> <item android:id="@+id/menu3" android:title="dial A Contact"/> <item android:id="@+id/menu4" android:title="view All Contacts"/> </menu> </item> </menu>