Advertisement
Guest User

Menu

a guest
Jan 23rd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. Menu:
  2. ========
  3.  
  4. MainActivity.java
  5. ==================
  6. package com.example.android.mymenu;
  7.  
  8. import android.content.Context;
  9. import android.content.DialogInterface;
  10. import android.content.Intent;
  11. import android.support.v7.app.AlertDialog;
  12. import android.support.v7.app.AppCompatActivity;
  13. import android.os.Bundle;
  14. import android.view.Menu;
  15. import android.view.MenuInflater;
  16. import android.view.MenuItem;
  17. import android.widget.ListView;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20.  
  21. public class MainActivity extends AppCompatActivity {
  22.  
  23. Context context;
  24. ListView myList;
  25.  
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.activity_main);
  30.  
  31. setPointer();
  32.  
  33. }
  34.  
  35. private void setPointer()
  36. {
  37. this.context=this;
  38. }
  39.  
  40. @Override
  41. public boolean onCreateOptionsMenu(Menu menu) {
  42. //create MenuInflater object
  43. MenuInflater inflater=getMenuInflater();
  44. //inflate the menu
  45. inflater.inflate(R.menu.menu_main,menu);
  46. return super.onCreateOptionsMenu(menu);
  47.  
  48. }
  49.  
  50. @Override
  51. public boolean onOptionsItemSelected(MenuItem item) {
  52. switch (item.getItemId())
  53. {
  54. case R.id.itemMain:
  55. Toast.makeText(context, R.string.mainPage, Toast.LENGTH_SHORT).show();
  56. break;
  57.  
  58. case R.id.itemAccess:
  59. Toast.makeText(context, R.string.accessPage, Toast.LENGTH_SHORT).show();
  60. Intent intent = new Intent(this,Accesories.class);
  61. //intent.putExtra("userName",getUser);
  62. startActivity(intent);
  63. finish();
  64. break;
  65.  
  66. case R.id.itemInvite:
  67. Toast.makeText(context, R.string.invitePage, Toast.LENGTH_SHORT).show();
  68. break;
  69.  
  70. case R.id.itemProf:
  71. Toast.makeText(context, R.string.profPage, Toast.LENGTH_SHORT).show();
  72. break;
  73.  
  74. case R.id.itemSitting:
  75. Toast.makeText(context, R.string.sitPage, Toast.LENGTH_SHORT).show();
  76. break;
  77.  
  78. /*//create Alert dialog builder to design Alert Dialog
  79. AlertDialog.Builder builder = new AlertDialog.Builder(context);
  80. //set the title
  81. builder.setTitle("About the application...");
  82. //creating a new view of TextView
  83. TextView txtView = new TextView(context);
  84. //setting text size
  85. txtView.setTextSize(60);
  86. //setting the text inside
  87. txtView.setText("Adam is the king!!!");
  88.  
  89. builder.setView(txtView);
  90. //positive button to dismiss the dialog by pressing OK
  91. builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  92. @Override
  93. public void onClick(DialogInterface dialogInterface, int i) {
  94. dialogInterface.dismiss();
  95. }
  96. });
  97. //show the builder...
  98. builder.show();*/
  99. }
  100. return super.onOptionsItemSelected(item);
  101. }
  102. }
  103.  
  104.  
  105.  
  106. activity_main.xml
  107. ==================
  108. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  109. android:layout_width="match_parent"
  110. android:layout_height="match_parent"
  111. android:orientation="vertical">
  112.  
  113. <ListView
  114. android:layout_width="match_parent"
  115. android:layout_height="match_parent"
  116. android:id="@+id/lstFriends"/>
  117. </LinearLayout>
  118.  
  119.  
  120.  
  121. CustomMenu.java
  122. ==================
  123. package com.example.android.mymenu;
  124.  
  125. import android.content.Context;
  126. import android.os.Bundle;
  127. import android.support.v7.app.AppCompatActivity;
  128. import android.view.Menu;
  129. import android.view.MenuItem;
  130. import android.widget.Toast;
  131.  
  132. /**
  133. * Created by android on 19/01/2017.
  134. */
  135.  
  136. public class CustomMenu extends AppCompatActivity {
  137.  
  138. private int groupId=1;
  139. int homeId= Menu.FIRST;
  140. int profileId = Menu.FIRST+1;
  141. int searchId = Menu.FIRST+2;
  142. int dealId = Menu.FIRST+3;
  143. int helpId = Menu.FIRST+4;
  144. int contactus = Menu.FIRST+5;
  145. Context context;
  146.  
  147. @Override
  148. protected void onCreate(Bundle savedInstanceState) {
  149. super.onCreate(savedInstanceState);
  150. setContentView(R.layout.activity_custom_menu);
  151. this.context=this;
  152. }
  153.  
  154. /*@Override
  155. public boolean onCreateOptionsMenu(Menu menu) {
  156. //adding items to our menu
  157. menu.add(groupId,homeId,homeId,"Home");
  158. menu.add(groupId,profileId,profileId,"Profile");
  159. menu.add(groupId,searchId,searchId,"Search");
  160. menu.add(groupId,dealId,dealId,"Deal");
  161. menu.add(groupId,helpId,helpId,"Help");
  162. menu.add(groupId,contactus,contactus,"Contact Us");
  163. return super.onCreateOptionsMenu(menu);
  164. }
  165.  
  166. @Override
  167. public boolean onOptionsItemSelected(MenuItem item) {
  168. switch (item.getItemId())
  169. {
  170. case 1:
  171. Toast.makeText(context, "Home:"+homeId, Toast.LENGTH_SHORT).show();
  172. break;
  173. case 2:
  174. Toast.makeText(context,"Profile:"+profileId,Toast.LENGTH_SHORT).show();
  175. break;
  176. case 3:
  177. Toast.makeText(context, "Search:"+searchId, Toast.LENGTH_SHORT).show();
  178. break;
  179. case 4:
  180. Toast.makeText(context, "Deal:"+dealId, Toast.LENGTH_SHORT).show();
  181. break;
  182. case 5:
  183. Toast.makeText(context, "Help:"+ helpId, Toast.LENGTH_SHORT).show();
  184. break;
  185. case 6:
  186. Toast.makeText(context, "Contact Us:"+contactus, Toast.LENGTH_SHORT).show();
  187. break;
  188. }
  189. return super.onOptionsItemSelected(item);
  190. }*/
  191. }
  192.  
  193.  
  194.  
  195. activity_custom_menu
  196. ======================
  197. <?xml version="1.0" encoding="utf-8"?>
  198. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  199. xmlns:tools="http://schemas.android.com/tools"
  200. android:layout_width="match_parent"
  201. android:layout_height="match_parent"
  202. android:paddingBottom="@dimen/activity_vertical_margin"
  203. android:paddingLeft="@dimen/activity_horizontal_margin"
  204. android:paddingRight="@dimen/activity_horizontal_margin"
  205. android:paddingTop="@dimen/activity_vertical_margin"
  206. tools:context=".CustomMenu">
  207.  
  208.  
  209. </RelativeLayout>
  210.  
  211.  
  212.  
  213. menu_main.xml (res --> menu --> menu_main)
  214. ==============
  215. <?xml version="1.0" encoding="utf-8"?>
  216. <menu xmlns:android="http://schemas.android.com/apk/res/android"
  217. android:layout_height="50sp"
  218. android:layout_width="50sp">
  219.  
  220. <item
  221. android:id="@+id/itemMain"
  222. android:title="@string/main"/>
  223.  
  224. <item
  225. android:id="@+id/itemAccess"
  226. android:title="@string/access"/>
  227.  
  228. <item
  229. android:id="@+id/itemInvite"
  230. android:title="@string/invited"/>
  231.  
  232. <item
  233. android:id="@+id/itemProf"
  234. android:title="@string/prof"/>
  235.  
  236. <item
  237. android:id="@+id/itemSitting"
  238. android:title="@string/sitting"/>
  239.  
  240. </menu>
  241.  
  242.  
  243.  
  244. strings.xml
  245. ============
  246. <resources>
  247.  
  248. <string name="app_name">MyMenu</string>
  249. <string name="main">ראשי</string>
  250. <string name="invited">מוזמנים</string>
  251. <string name="access">אביזרים</string>
  252. <string name="prof">בעלי מקצוע</string>
  253. <string name="sitting">הושבה</string>
  254. <string name="mainPage">עובר לעמוד הראשי</string>
  255. <string name="accessPage">עובר לעמוד האביזרים</string>
  256. <string name="invitePage">עובר לעמוד המוזמנים</string>
  257. <string name="profPage">עובר לעמוד בעלי המקצוע</string>
  258. <string name="sitPage">עובר לעמוד ההושבה</string>
  259.  
  260. </resources>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement